GUI编程
1、简介
GUI的核心技术:Swing AWT
组件
- 窗口
- 弹框
- 面板
- 文本框
- 列表框
- 按钮
- 图片
- 监听事件
- 鼠标
- 键盘事件
2、AWT
2.1、AWT介绍
2.2、组件和容器
2.2.1、Frame
问题:发现窗口关闭不掉,停止java运行。
package AWTDemo;
import java.awt.*;
//GUI的第一个界面
public class Demo1 {
public static void main(String[] args) {
Frame frame = new Frame();
frame.setBackground(Color.BLACK);
frame.setVisible(true);
frame.setTitle("小程序");
frame.setBounds(200,200,600,600);
// frame.setSize(500,500);
// frame.setLocation(200,200);
}
}
2.2.2、Panel
package AWTDemo;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Demo2 {
public static void main(String[] args) {
Frame frame = new Frame();
frame.setBackground(Color.BLACK);
frame.setVisible(true);
frame.setTitle("小程序");
frame.setBounds(200,200,600,600);
frame.setLayout(null);
Panel panel = new Panel();
panel.setBackground(Color.BLUE);
panel.setVisible(true);
panel.setBounds(50,50,100,200);
frame.add(panel);
//监听事件
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
2.3、布局管理器
- 流式布局
package AWTDemo;
import java.awt.*;
public class Demo3 {
public static void main(String[] args) {
Frame frame = new Frame();
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");
//设置为流式布局
//frame.setLayout(new FlowLayout());
frame.setLayout(new FlowLayout(FlowLayout.LEFT));
frame.setSize(200,200);
//添加按钮
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setVisible(true);
}
}
- 东西南北东
package AWTDemo;
import java.awt.*;
public class Demo4 {
public static void main(String[] args) {
Frame frame = new Frame();
frame.setSize(500,500);
frame.setVisible(true);
frame.setLayout(new BorderLayout());
Button east = new Button("east");
Button west = new Button("west");
Button south = new Button("south");
Button north = new Button("north");
Button center = new Button("center");
frame.add(east,BorderLayout.EAST);
frame.add(west,BorderLayout.WEST);
frame.add(south,BorderLayout.SOUTH);
frame.add(north,BorderLayout.NORTH);
frame.add(center,BorderLayout.CENTER);
}
}
- 表格布局
package AWTDemo;
import java.awt.*;
public class Demo5 {
public static void main(String[] args) {
Frame frame = new Frame();
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");
Button button4 = new Button("button4");
Button button5 = new Button("button5");
Button button6 = new Button("button6");
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
frame.add(button5);
frame.add(button6);
frame.setVisible(true);
frame.setLayout(new GridLayout(2,3));
//frame.setSize(500,500);
frame.pack();//Java函数,自动排布
}
}
练习:
package AWTDemo;
import java.awt.*;
public class Test1 {
public static void main(String[] args) {
Frame frame = new Frame();
frame.setLayout(new GridLayout(2,1));
frame.setSize(300,400);
frame.setLocation(500,500);
frame.setBackground(Color.BLACK);
frame.setVisible(true);
Panel p1 = new Panel(new BorderLayout());
Panel p2 = new Panel(new GridLayout(2,1));
Panel p3 = new Panel(new BorderLayout());
Panel p4 = new Panel(new GridLayout(2,2));
frame.add(p1);
frame.add(p3);
p1.add(new Button("1"),BorderLayout.WEST);
p2.add(new Button("2"));
p2.add(new Button("3"));
p1.add(p2,BorderLayout.CENTER);
p1.add(new Button("4"),BorderLayout.EAST);
p3.add(new Button("5"),BorderLayout.WEST);
for (int i = 6; i < 10; i++) {
p4.add(new Button(i+""));
}
p3.add(p4,BorderLayout.CENTER);
p3.add(new Button("10"),BorderLayout.EAST);
}
}
总结:
- Frame是一个顶级窗口
- Panel无法单独显示,必须添加到某个容器中。
- 布局管理器
- 流式
- 东西南北中
- 表格
- 大小,定位,背景颜色,可见性,监听!
2.4、事件监听
package AWTDemo;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Demo6 {
public static void main(String[] args) {
Frame frame = new Frame();
frame.setVisible(true);
Button button = new Button("test");
//添加按钮按下监听器;使用匿名内部类,可以减少类名
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("测试");
System.out.println(e.getActionCommand());
}
});
frame.add(button,BorderLayout.CENTER);
frame.pack();
//调用抽取方法
WindowClose(frame);
}
//抽取方法
public static void WindowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
多个按钮共享一个事件监听器
package AWTDemo;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Demo7 {
public static void main(String[] args) {
Frame frame = new Frame();
frame.setVisible(true);
frame.setLayout(new GridLayout(1, 2));
Button button1 = new Button("Start");
Button button2 = new Button("Stop");
button1.setActionCommand("Stop->Start");
button2.setActionCommand("Start->Stop");
MyActionListener1 myActionListener1 = new MyActionListener1();
button1.addActionListener(myActionListener1);
button2.addActionListener(myActionListener1);
frame.add(button1);
frame.add(button2);
frame.pack();
}
}
class MyActionListener1 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Msg: "+e.getActionCommand());//或者输入框的文本
}
}
2.5、输入框TextField 监听事件
package AWTDemo;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Demo8 {
public static void main(String[] args) {
//启动!
new MyFrame();
}
}
class MyFrame extends Frame{
public MyFrame(){
TextField textField = new TextField();
add(textField);
//监听文本框输入的文字
MyActionListener myActionListener = new MyActionListener();
//按下Enter,就会触发输入框事件
textField.addActionListener(myActionListener);
//设置替换字符
textField.setEchoChar('*');
setVisible(true);
pack();
}
}
class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
TextField textField = (TextField) e.getSource();
System.out.println(textField.getText());//或者输入框的文本
textField.setText("");
}
}
2.6、简单计算器(组合和内部类的使用)
package AWTDemo;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//简易计算器
public class Test2 {
public static void main(String[] args) {
Calculator calculator = new Calculator();
}
}
//计算器类
class Calculator extends Frame {
public Calculator() {
//3个单行文本框
TextField num1 = new TextField(10);
TextField num2 = new TextField(10);
TextField num3 = new TextField(10);
//1个标签
Label label = new Label("+");
//1个按钮
Button button = new Button("=");
CalculatorListener calculatorListener = new CalculatorListener(num1,num2,num3);
button.addActionListener(calculatorListener);
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
setVisible(true);
pack();
}
}
//监听器
class CalculatorListener implements ActionListener{
private TextField num1,num2,num3;
public CalculatorListener(TextField num1,TextField num2,TextField num3) {
this.num1=num1;
this.num2=num2;
this.num3=num3;
}
@Override
public void actionPerformed(ActionEvent e) {
//1、获取相加的两个数
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
//2、计算,把结果放在第三个文本框
num3.setText(""+(n1+n2));
//3、清除前面两个文本框内容
num1.setText("");
num2.setText("");
}
}
组合改变后的代码(面向对象的写法)
package AWTDemo;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//简易计算器
public class Test2 {
public static void main(String[] args) {
new Calculator().loadFrame();
}
}
//计算器类
class Calculator extends Frame {
//属性
TextField num1,num2,num3;
//方法
public void loadFrame() {
//3个单行文本框
num1 = new TextField(10);
num2 = new TextField(10);
num3 = new TextField(10);
//1个标签
Label label = new Label("+");
//1个按钮
Button button = new Button("=");
CalculatorListener calculatorListener = new CalculatorListener(this);
button.addActionListener(calculatorListener);
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
setVisible(true);
pack();
}
}
使用内部类后的写法,简化了代码
内部类的最大好处:访问外部的属性和方法
package AWTDemo;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//简易计算器
public class Test2 {
public static void main(String[] args) {
new Calculator().loadFrame();
}
}
//计算器类
class Calculator extends Frame {
//属性
TextField num1,num2,num3;
//方法
public void loadFrame() {
//3个单行文本框
num1 = new TextField(10);
num2 = new TextField(10);
num3 = new TextField(10);
//1个标签
Label label = new Label("+");
//1个按钮
Button button = new Button("=");
button.addActionListener(new CalculatorListener());
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
setVisible(true);
pack();
}
//监听器
private class CalculatorListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//1、获取相加的两个数
//2、计算,把结果放在第三个文本框
//3、清除前面两个文本框内容
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
num3.setText(""+(n1+n2));
num1.setText("");
num2.setText("");
}
}
}
2.7、画笔
package AWTDemo;
import java.awt.*;
public class Demo9 {
public static void main(String[] args) {
new MyPaint().LoadFrame();
}
}
class MyPaint extends Frame{
//画板窗口
public void LoadFrame(){
setVisible(true);
setBounds(200,200,600,600);
}
@Override
public void paint(Graphics g) {
//super.paint(g);
g.setColor(Color.RED);
g.fillRect(50,50,100,200);//实心矩形
g.drawOval(250,250,100,100);//圆
g.fillOval(350,350,100,100);//实心圆
}
}
2.8、鼠标监听
鼠标在画板上画点
package AWTDemo;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
public class Demo9 {
public static void main(String[] args) {
new MyPaint("画图");
}
}
class MyPaint extends Frame{
ArrayList<Point> points;
//画板
public MyPaint(String title) {
super(title);
setBounds(200,200,600,600);
points=new ArrayList<>();
setVisible(true);
this.addMouseListener(new MyMouseListencer());
}
//画笔,把点显示出来.其实点就是实心圆
@Override
public void paint(Graphics g) {
Iterator<Point> iterator = points.iterator();
while (iterator.hasNext()){
Point next = iterator.next();
g.fillOval(next.x,next.y,10,10);
g.setColor(Color.BLACK);
}
}
//添加点到集合的方法
public void addPoint(Point point){
points.add(point);
}
//鼠标点击事件,生成点
private class MyMouseListencer extends MouseAdapter{
@Override
public void mouseClicked(MouseEvent e) {
MyPaint myPaint = (MyPaint) e.getSource();
myPaint.addPoint(new Point(e.getX(), e.getY()));
//每次点击鼠标要重画
myPaint.repaint();
}
}
}
2.9、窗口监听
跟上面类似
2.10、键盘监听
跟上面类似
3、Swing
属于对AWT的加强,封装,有很多更强的功能,能简化开发。
3.1、JFrame
package swingdemo;
import javax.swing.*;
import java.awt.*;
public class Demo1 {
public static void main(String[] args) {
new Demo1().init();
}
public void init(){
JFrame frame = new JFrame("这是一个JFrame窗口");
frame.setVisible(true);
frame.setBounds(100,100,200,200);
JLabel jLabel = new JLabel("这是一个JLable");
//直接设置背景颜色不行,需要设置容器颜色
frame.setBackground(Color.red);
frame.add(jLabel);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
设置容器背景色,加便签水平居中
package swingdemo;
import javax.swing.*;
import java.awt.*;
public class Demo2 {
public static void main(String[] args) {
new MyJframe().init();
}
}
class MyJframe extends JFrame{
public void init(){
this.setVisible(true);
this.setBounds(200,200,200,200);
//获取容器
Container contentPane = this.getContentPane();
//设置容器背景
contentPane.setBackground(Color.red);
JLabel jLabel = new JLabel("这是一个JLable");
this.add(jLabel);
//设置水平居中
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
}
}
3.2、JDialog 弹窗
package swingdemo;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//主窗口
public class Demo3 extends JFrame {
public Demo3(){
this.setVisible(true);
this.setSize(700,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//JFrame 放东西,容器
Container contentPane = this.getContentPane();
//绝对布局
contentPane.setLayout(null);
JButton jButton = new JButton("点击弹出一个对话框");
jButton.setBounds(30,30,200,50);
//点击按钮弹窗
jButton.addActionListener(new ActionListener() { //监听器
@Override
public void actionPerformed(ActionEvent e) {
new MyDialog();
}
});
contentPane.add(jButton);
}
public static void main(String[] args) {
new Demo3();
}
}
//弹窗的窗口
class MyDialog extends JDialog{
public MyDialog(){
this.setVisible(true);
this.setBounds(100,100,500,500);
Container container = this.getContentPane();
container.setLayout(null);
container.add(new Label("学JAVA"));
}
}
3.3、JLable
Icon
/**
* Author:ckvsok
* Date:2021/3/13
**/
package swingdemo;
import javax.swing.*;
import java.awt.*;
//图标,需要实现类,JFrame继承
public class Demo4 extends JFrame implements Icon {
private int width;
private int height;
public Demo4() {
}
public Demo4(int width,int height){
this.width=width;
this.height=height;
}
public void init(){
Demo4 icon = new Demo4(20, 20);
//图标可以放在标签上,也可以放在按钮上
JLabel jLabel = new JLabel("icon",icon,SwingConstants.CENTER);
Container contentPane = this.getContentPane();
contentPane.add(jLabel);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Demo4().init();
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.fillOval(x,y,width,height);
}
@Override
public int getIconWidth() {
return this.width;
}
@Override
public int getIconHeight() {
return this.height;
}
}
ImageIcon
/**
* Author:ckvsok
* Date:2021/3/13
**/
package swingdemo;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class Demo5 extends JFrame {
public static void main(String[] args) {
new Demo5();
}
public Demo5() {
//获取图片的地址
URL url = Demo5.class.getResource("ck.png");
ImageIcon imageIcon = new ImageIcon(url);
JLabel label = new JLabel("IconImage");
label.setIcon(imageIcon);
Container contentPane = getContentPane();
contentPane.add(label);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setBounds(120,156,222,222);
}
}
3.4、JPanel
/**
* Author:ckvsok
* Date:2021/3/13
**/
package swingdemo;
import javax.swing.*;
import java.awt.*;
public class Demo6 extends JFrame {
public static void main(String[] args) {
new Demo6();
}
public Demo6(){
Container contentPane = getContentPane();
contentPane.setLayout(new GridLayout(2,2,10,15));//后面的参数意思是间距
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
panel1.setLayout(new GridLayout(1,2));
panel2.setLayout(new GridLayout(2,1));
panel3.setLayout(new GridLayout(1,3));
panel4.setLayout(new GridLayout(3,2));
panel1.add(new JButton("1"));
panel1.add(new JButton("1"));
panel2.add(new JButton("2"));
panel2.add(new JButton("2"));
panel3.add(new JButton("3"));
panel3.add(new JButton("3"));
panel3.add(new JButton("3"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
contentPane.add(panel1);
contentPane.add(panel2);
contentPane.add(panel3);
contentPane.add(panel4);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setBounds(123,157,851,450);
}
}
JScrollPane
/**
* Author:ckvsok
* Date:2021/3/13
**/
package swingdemo;
import javax.swing.*;
import java.awt.*;
public class Demo7 extends JFrame {
public static void main(String[] args) {
new Demo7();
}
public Demo7(){
Container container = getContentPane();
//文本域
JTextArea textArea = new JTextArea(10, 5);
textArea.setText("Input your story");
//Scroll面板
JScrollPane jScrollPane = new JScrollPane(textArea);
container.add(jScrollPane);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setBounds(123,157,851,450);
}
}
3.5、JButton
图片按钮
/**
* Author:ckvsok
* Date:2021/3/13
**/
package swingdemo;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class Demo8 extends JFrame {
public static void main(String[] args) {
new Demo8();
}
public Demo8(){
Container container = getContentPane();
//图片按钮
URL url = Demo8.class.getResource("ck.png");
ImageIcon icon = new ImageIcon(url);
JButton button = new JButton(icon);
container.add(button);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setBounds(100,100,500,800);
}
}
单选框
/**
* Author:ckvsok
* Date:2021/3/13
**/
package swingdemo;
import javax.swing.*;
import java.awt.*;
public class Demo9 extends JFrame {
public static void main(String[] args) {
new Demo9();
}
public Demo9() {
Container container = getContentPane();
container.setLayout(new FlowLayout());
//单选框
JRadioButton radioButton1 = new JRadioButton("1");
JRadioButton radioButton2 = new JRadioButton("2");
//单选框需要分组,只能选择一个
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(radioButton1);
buttonGroup.add(radioButton2);
container.add(radioButton1);
container.add(radioButton2);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setBounds(100, 100, 500, 800);
}
}
多选框
/**
* Author:ckvsok
* Date:2021/3/13
**/
package swingdemo;
import javax.swing.*;
import java.awt.*;
public class Demo10 extends JFrame {
public static void main(String[] args) {
new Demo10();
}
public Demo10() {
Container container = getContentPane();
container.setLayout(new FlowLayout());
//多选框
JCheckBox checkBox1 = new JCheckBox("1");
JCheckBox checkBox2 = new JCheckBox("2");
container.add(checkBox1);
container.add(checkBox2);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setBounds(100, 100, 500, 800);
}
}
3.6、列表
下拉框
/**
* Author:ckvsok
* Date:2021/3/13
**/
package swingdemo;
import javax.swing.*;
import java.awt.*;
public class Demo11 extends JFrame {
public Demo11(){
Container container = getContentPane();
container.setLayout(new FlowLayout());
//下拉框
JComboBox comboBox = new JComboBox();
//设置默认值
comboBox.addItem(null);
comboBox.addItem("1");
comboBox.addItem("2");
comboBox.addItem("3");
container.add(comboBox);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setBounds(100, 100, 500, 800);
}
public static void main(String[] args) {
new Demo11();
}
}
列表框
/**
* Author:ckvsok
* Date:2021/3/13
**/
package swingdemo;
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
public class Demo12 extends JFrame {
public Demo12(){
Container container = getContentPane();
container.setLayout(new FlowLayout());
//列表框
JList list = new JList();
Vector vector = new Vector();
list.setListData(vector);
vector.add("111111111");
vector.add("222222222");
vector.add("333333333");
container.add(list);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setBounds(100, 100, 500, 800);
}
public static void main(String[] args) {
new Demo12();
}
}
3.7、文本框
文本框
密码框
文本域
/**
* Author:ckvsok
* Date:2021/3/13
**/
package swingdemo;
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
public class Demo13 extends JFrame {
public Demo13(){
Container container = getContentPane();
container.setLayout(new FlowLayout());
//文本框 单行
JTextField textField = new JTextField("文本框");
//密码框
JPasswordField passwordField = new JPasswordField("密码框");
//文本域, 多行 可以换行
JTextArea textArea = new JTextArea("文本域");
container.add(textField);
container.add(passwordField);
container.add(textArea);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setBounds(100, 100, 500, 800);
}
public static void main(String[] args) {
new Demo13();
}
}