本周主要还是学习图形界面
各种容器使用方法
如下代码:
package text10;
import java.awt.*;
import java.io.File;
import javax.swing.*;
public class Text extends JFrame{
public Text() {
this.setTitle("窗口");
Container cont=this.getContentPane();
JLabel la=new JLabel("标签");
JButton ba=new JButton("按钮");
/*在窗体中添加面板*/
JPanel panel = new JPanel();
// panel.add(ba);
// panel.add(la);
//分割面板 JSplitPane
/* JSplitPane sp=new JSplitPane(JSplitPane.VERTICAL_SPLIT,ba,new JButton("s"));
JSplitPane splitPane=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,la,sp);
splitPane.setDividerSize(10); //设置的分隔符宽度
splitPane.setDividerLocation(50); //设置分隔条位置
splitPane.setOneTouchExpandable(true); //设置快速展开折叠分隔条
this.add(splitPane);
this.pack();
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
*/
//在面板中设置多个选项卡 JTabbbedPane
/* JTabbedPane tp=new JTabbedPane(JTabbedPane.BOTTOM);
tp.addTab("文件",ba);
String path="d:"+File.separator+"游戏素材"+File.separator+"星星.png";
ImageIcon image=new ImageIcon(path);
tp.addTab("图片", image,panel.add(la));//添加的图片是在菜单栏的图标,而不是他的内容
cont.add(tp);
this.pack();
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
*/
//显示内容加滚动条JScrollPane
/* String path="d:"+File.separator+"游戏素材"+File.separator+"星星.png";
Icon image=new ImageIcon(path);
panel.add(new JLabel(image));
JScrollPane sp=new JScrollPane(panel,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
cont.add(sp);
this.pack();
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
*/
//在一个窗体内出现一个或多个子窗体,JDesktopPane规定父窗体;JInternalFrame规定子窗体
this.setLayout(new BorderLayout());
JDesktopPane dp=new JDesktopPane();
cont.add(dp,BorderLayout.CENTER);
cont.add(la,BorderLayout.SOUTH);
JInternalFrame in=new JInternalFrame("小窗体",true,true,true,true);
String path="d:"+File.separator+"游戏素材"+File.separator+"星星.png";
Icon image=new ImageIcon(path);
panel.add(new JLabel(image));
in.setBounds(10, 10, 100, 100);
in.add(panel);
in.setVisible(true);
dp.add(in);
this.pack();
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] arge) {
new Text();
}
}
运行截图
一
二
三
四
(以上代码主要是测试本周所学的一些其他容器的用法)
计算机代码以及关于监视器的用法
package text11;
import java.awt.*;
import java.awt.event.ActionEvent;
import javax.swing.*;
public class Jisuanji extends JFrame implements Action{
Container cont;
JPanel pan,pan1,pan2,pan3,pan4;
JTabbedPane tp;
JTextArea ta;
JButton bt,bt1,bt2;
JButton[] but=new JButton[20];
String str[]= {"7","8","9","/","sqrt","4","5","6","*","%","1","2","3","-","1/x","0","+/-",".","+","="};
public Jisuanji() {
this.setTitle("计算机");
this.setBounds(100,100, 310, 370);
this.setResizable(false);
cont=this.getContentPane();
pan=new JPanel(new FlowLayout(FlowLayout.RIGHT));
pan1=new JPanel();
pan2=new JPanel();
pan3=new JPanel();
pan4=new JPanel(new GridLayout(4,5,0,10));
tp=new JTabbedPane(JTabbedPane.TOP);
ta=new JTextArea(5,28);
ta.setEditable(false);
bt=new JButton("Backspace");
bt1=new JButton("CE");
bt2=new JButton("C");
ta.setLineWrap(true);
pan.add(ta);
pan3.add(bt);
pan3.add(bt1);
pan3.add(bt2);
pan.add(pan3);
for(int i=0;i<20;i++){
but[i] =new JButton(""+str[i]);
pan4.add(but[i]);
}
pan.add(pan4);
tp.addTab("编辑(E)",pan);
tp.addTab("查看(V)",pan1);
tp.addTab("帮助(H)",pan2);
cont.add(tp);
for(int i=0;i<20;i++) {
but[i].addActionListener(this);
}
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new Jisuanji();
}
public void actionPerformed(ActionEvent e) {
for(int i=0;i<20;i++){
if(e.getSource()==but[i])
ta.append(str[i]);
}
}
@Override
public Object getValue(String key) {
// TODO Auto-generated method stub
return null;
}
@Override
public void putValue(String key, Object value) {
// TODO Auto-generated method stub
}
}
运行结果
还有学习了监听适配器对于窗口,本周还是主要就是对图形界面进行了进一步的学习