参考教学视频:秦疆
GUI
组件:窗口、弹框、面板、文本框、列表框、按钮、图片、监听事件、鼠标、键盘事件、破解工具
1. 简介
Gui的核心:Swing AWT
-
界面不美观
-
需要jre环境
2.AWT
awt介绍:
-
包含了很多的接口和类
-
元素:窗口、按钮、文本框
-
java.awt.*
-
组件Component
-
button、TextArea、Label... 存放在容器中
-
容器Container
-
Window
-
Frame
-
Dialog
-
-
面板Panel
-
Applet
-
-
-
组件和容器
-
Frame
Frame f = new Frame("JAVA图形界面窗口");
f.setVisible(true);//设置可见
f.setBackground(Color.yellow);//背景色
// f.setSize(400, 400);//设置大小
// f.setLocation(200,200);//初始位置
f.setResizable(false);//设置大小固定
f.setBounds(200,200,400,400);//等价设置大小及初始位置
-
Panel
Frame f = new Frame();
f.setLayout(null);
f.setBounds(200, 200, 400, 400);
f.setBackground(Color.gray);
Panel p = new Panel();
p.setBounds(50,50,100,100);//panel设置坐标,相对于frame
p.setBackground(new Color(193,15,60));
f.add(p);
f.setVisible(true);
//监听事件
//监听窗口关闭事件,适配器模式
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
//窗口关闭时退出程序
System.exit(1);
// super.windowClosing(e);
}
});
-
布局管理器
-
流式布局
-
东南西北中布局
-
表格布局
事件监听
addActionListener();
public void actionPerformed(ActionEvent e){
//e.getActionCommand();获取按钮信息
}
组合+内部类
-
内部类
class Calculator extends Frame{
TextField t1,t2,t3;
public Calculator() {
// TextField t1 = new TextField(10);
// TextField t2 = new TextField(10);
// TextField t3 = new TextField(10);
t1 = new TextField(10);
t2 = new TextField(10);
t3 = new TextField(10);
Button b = new Button("=");
// b.addActionListener(new MyCalculatorListener(t1,t2,t3));
b.addActionListener(new MyCalculatorListener());
Label l = new Label("+");
setLayout(new FlowLayout());
add(t1);
add(l);
add(t2);
add(b);
add(t3);
pack();
setVisible(true);
}
private class MyCalculatorListener implements ActionListener{
// private TextField t1;
// private TextField t2;
// private TextField t3;
// Calculator c = null;
// public MyCalculatorListener(TextField t1,TextField t2,TextField t3) {
// public MyCalculatorListener(Calculator c) {
//// this.t1 = t1;
//// this.t2 = t2;
//// this.t3 = t3;
// this.c = c;
// }
@Override
public void actionPerformed(ActionEvent e) {
// int i1 = Integer.parseInt(t1.getText());
// int i2 = Integer.parseInt(t2.getText());
// t3.setText(i1+i2+"");
// t1.setText("");
// t2.setText("");
int i1 = Integer.parseInt(t1.getText());
int i2 = Integer.parseInt(t2.getText());
t3.setText(i1+i2+"");
t1.setText("");
t2.setText("");
}
}
}
鼠标监听
mouseListener
窗口监听
windowListener
键盘监听
keyListener
3.Swing
窗口、面板
JFrame jf = new JFrame("JFrame窗口");
jf.setVisible(true);
jf.setBounds(100,100,400,600);
Container c = jf.getContentPane();
c.setBackground(Color.gray);
JLabel jl = new JLabel("JLabel窗口");
jl.setHorizontalAlignment(SwingConstants.CENTER);
jf.add(jl);
//关闭事件
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
弹窗
public TestDialog() {
class NewDialog extends JDialog{
public NewDialog() {
this.setVisible(true);
this.setBounds(100,100,500,500);
Container c = this.getContentPane();
c.setLayout(null);
c.add(new Label("测试"));
}
}
this.setVisible(true);
this.setSize(700,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container c = this.getContentPane();
c.setLayout(null);
JButton jb = new JButton("点击弹窗");
jb.setBounds(30,30,200,50);
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new NewDialog();
}
});
c.add(jb);
}
标签、面板、按钮、列表、文本框
-
图标icon、imageIcon
-
滚动条 JScrollPanel
Container c = getContentPane();
JTextArea jta = new JTextArea(20,50);
jta.setText("测试");
JScrollPane js = new JScrollPane(jta);
c.add(js);
this.setVisible(true);
this.setBounds(100,100,300,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
帧
如果时间片足够小,就是动画,一秒30帧 60帧。连起来就是动画,拆开就是静态的图片
标签:JAVA,语言,Day8,t2,t3,t1,add,new,TextField From: https://www.cnblogs.com/-Gin/p/18129564