GUI编程
目标:了解MVC架构、了解监听、制作自己的一些小工具
1.是什么?
图形用户界面
它包括哪些部分?
组件:
- 窗口
- 弹窗
- 面板
- 按钮
- 文本框
- 列表框
- 监听事件
- 鼠标事件
- 键盘事件
2. 怎么用?
GUI核心技术:Swing、AWT
2.1 AWT
-
概念:抽象的窗口工具,包含很多类和窗口,
-
元素:含有窗口、按钮等组件
3.元素的使用:
-
Frame是最底层容器,Panel是一个容器但是不能单独存在要放在其他容器里面
注意:若要显示Frame背景颜色,一般还要设置 Frame的布局为null,即
setLayout(null)
-
设置内容一般在Panel里面
-
布局管理器
-
流式布局(FlowLayout)
import java.awt.*; public class First { public static void main(String[] args) { Frame frame = new Frame(); frame.setBackground(Color.red); frame.setBounds(100,100,400,400); frame.setVisible(true); Button button1 = new Button("btn1"); Button button2 = new Button("btn2"); Button button3 = new Button("btn3"); frame.add(button1); frame.add(button2); frame.add(button3); frame.setLayout(new FlowLayout(FlowLayout.CENTER)); } }
-
边界布局(BorderLayout)
import java.awt.*; public class First { public static void main(String[] args) { Frame frame = new Frame(); frame.setBackground(Color.red); frame.setSize(400,400); Button button1 = new Button("btn1"); Button button2 = new Button("btn2"); Button button3 = new Button("btn3"); frame.add(button1,BorderLayout.EAST); frame.add(button2,BorderLayout.CENTER); frame.add(button3,BorderLayout.WEST); frame.setVisible(true); } }
-
表格布局
import java.awt.*; public class First { public static void main(String[] args) { Frame frame = new Frame(); frame.setBackground(Color.red); frame.setSize(400,400); Button button1 = new Button("btn1"); Button button2 = new Button("btn2"); Button button3 = new Button("btn3"); frame.setLayout(new GridLayout(3,1)); frame.add(button1); frame.add(button2); frame.add(button3); frame.pack();//自动填充 frame.setVisible(true); } }
-
例题:
代码实现:
import java.awt.*;
public class First {
public static void main(String[] args) {
Frame frame = new Frame();
frame.setSize(400,400);
frame.setLayout(new GridLayout(2,1));
Panel panel1 = new Panel(new BorderLayout());//先为面板定义边界布局,才会自动填充
Panel panel2 = new Panel(new BorderLayout());
frame.add(panel1);
frame.add(panel2);
Panel panel11 = new Panel();
panel11.setLayout(new GridLayout(2,1));
Panel panel22 = new Panel();
panel22.setLayout(new GridLayout(2,2));
panel1.add(panel11,BorderLayout.CENTER);
panel2.add(panel22,BorderLayout.CENTER);
Button btn1 = new Button("btn1");
panel1.add(btn1,BorderLayout.WEST);
Button btn2 = new Button("btn2");
Button btn3 = new Button("btn3");
panel11.add(btn2);
panel11.add(btn3);
Button btn4 = new Button("btn4");
panel1.add(btn4,BorderLayout.EAST);
Button btn5 = new Button("btn5");
panel2.add(btn5,BorderLayout.WEST);
for (int i = 6; i <= 9; i++) {
panel22.add(new Button("btn"+i));
}
Button btn10 = new Button("btn10");
panel2.add(btn10,BorderLayout.EAST);
frame.setVisible(true);
}
}
-
监听
-
事件监听(actionListener)
-
窗口(windowListener)
-
键盘(keyListener)监听
例子:输入框事件监听
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class First { public static void main(String[] args) { MyFrame myFrame = new MyFrame(); } static class MyFrame extends Frame{ //创建对象、实现方法在无参构造中实现(面向过程) public MyFrame(){ TextField textField = new TextField(); add(textField); MyListener myListener = new MyListener(); textField.addActionListener(myListener); setVisible(true); pack(); //开启自适应 } } static class MyListener implements ActionListener{ public void actionPerformed(ActionEvent e) { TextField textField = (TextField) e.getSource(); //getSource获取对象 System.out.println(textField.getText()); //getText设置文本框 textField.setText(""); } } }
-
效果截图:
2.2Swing
①. 在swing中窗口只有变成容器才能设置颜色(JFrame)
未变成容器(设置为蓝色)
import javax.swing.*;
import java.awt.*;
public class First {
public static void main(String[] args) {
MyFrame myFrame = new MyFrame();
}
static class MyFrame extends JFrame {
//创建对象、实现方法在无参构造中实现
public MyFrame(){
JLabel label = new JLabel("我是小发");
this.setVisible(true);
this.setBackground(Color.cyan);
this.add(label);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.pack();
}
}
}
截图
变成容器后
import javax.swing.*;
import java.awt.*;
public class First {
public static void main(String[] args) {
MyFrame myFrame = new MyFrame();
}
static class MyFrame extends JFrame {
//创建对象、实现方法在无参构造中实现
public MyFrame(){
JLabel label = new JLabel("我是小发");
label.setHorizontalAlignment(SwingConstants.CENTER);//设置标签水平居中
this.setVisible(true);
Container contentPane = this.getContentPane();//变成容器
contentPane.setBackground(Color.cyan);
contentPane.add(label);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//退出并关闭窗口
this.pack();
}
}
}
截图:
②. 弹窗(JDialog)
与JFrame类似,不过可以不用设置关闭事件
③. 标签(JLable)
new Jlable(“xxx”)
标签上可以放图标和图片(ImageIcon)
package com;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class First extends JFrame{
public First(){
JLabel label = new JLabel("我是小发");
label.setHorizontalAlignment(SwingConstants.CENTER);//设置标签水平居中
URL resource = First.class.getResource("1.jpg");//找到当前类路径下的图片,放在同一包下,放一同放在src无效
ImageIcon imageIcon = new ImageIcon(resource);
label.setIcon(imageIcon);//为标签添加图片
this.setVisible(true);
Container contentPane = this.getContentPane();//变成容器
contentPane.setBackground(Color.cyan);
contentPane.add(label);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//退出并关闭窗口
this.pack();
}
public static void main(String[] args) {
First myFrame = new First();
}
}
截图:
④. 面板(JPane)和滚动面板(JScrollPane)
用法和Panel类似
⑤. 按钮
普通按钮(JButton)
单选按钮(JRadioButton)
除了 new JRadioButton外,还要new ButtonGroup
复选按钮(JCheckBox)
⑥. 列表
下拉框(JComboBox)
列表框(JList)
⑦. 文本框
文本框(JTextField)
密码框(JPasswordField)
文本域(JTextArea)