GUI编程
组件:
-
窗口
-
弹窗
-
面板
-
文本框
-
列表框
-
按钮
-
图片
-
监听事件
-
鼠标事件
-
键盘事件
GUI的的核心技术:Swing/ AWT
一.AWT
介绍:抽象的窗口工具,包含很多的类和接口
元素:窗口,按钮,文本框
1.1组件和容器
- Frame
- 顶级窗口
package com.wangxu.lesson01;
import java.awt.*;
//GUI的第一个界面
public class TestFrame {
public static void main(String[] args) {
//Frame对象 看源码
Frame frame = new Frame("我的第一个JAVA图形界面窗口");
//设置可见性
frame.setVisible(true);
//设置窗口大小
frame.setSize(400,400);
//设置背景颜色
frame.setBackground(new Color(40, 101, 153));
//弹出的初始位置
frame.setLocation(200,200);
//设置大小固定
frame.setResizable(false);
}
}
-
Panel
-
Panel无法单独显示,必须添加到容器中
package com.wangxu.lesson01; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; public class TestPanel { public static void main(String[] args) { Frame frame = new Frame(); //布局 Panel panel = new Panel(); //设置布局 frame.setLayout(null); frame.setBounds(300,300,500,500); frame.setBackground(new Color(34, 60, 224)); //panel 设置坐标,相对于frame panel.setBounds(50,50,400,400); panel.setBackground(new Color(20, 125, 73)); frame.add(panel); frame.setVisible(true); //监听事件,监听窗口关闭事件 //适配器模式 frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } }
2.布局管理器
流式布局
package com.wangxu.lesson01;
import java.awt.*;
public class TestFlowLayout {
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(FlowLayout.LEFT));
frame.setSize(200,200);
//把按钮添加上去
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setVisible(true);
}
}
东南西北中
package com.wangxu.lesson01;
import java.awt.*;
public class TestBorderLayout {
public static void main(String[] args) {
Frame frame = new Frame();
Button east = new Button("East");
Button west = new Button("West");
Button north = new Button("North");
Button south= new Button("South");
Button center= new Button("Center");
frame.add(east,BorderLayout.EAST);
frame.add(west,BorderLayout.WEST);
frame.add(north,BorderLayout.NORTH);
frame.add(south,BorderLayout.SOUTH);
frame.add(center,BorderLayout.CENTER);
frame.setSize(600,600);
frame.setVisible(true);
}
}
表格布局
package com.wangxu.lesson01;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestGridLayout {
public static void main(String[] args) {
Frame frame = new Frame("TestGridLayout");
Button btn1 = new Button("btn1");
Button btn2 = new Button("btn2");
Button btn3 = new Button("btn3");
Button btn4= new Button("btn4");
Button btn5= new Button("btn5");
Button btn6= new Button("btn6");
frame.setLayout(new GridLayout(3,2));
frame.add(btn1);
frame.add(btn2);
frame.add(btn3);
frame.add(btn4);
frame.add(btn5);
frame.add(btn6);
frame.setSize(400,400);
frame.pack();
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setVisible(true);
}
}
如图,其代码实现为
package com.wangxu.lesson01;
import java.awt.*;
public class TEST {
public static void main(String[] args) {
Frame frame = new Frame();
frame.setSize(400,300);
frame.setLocation(400,300);
frame.setLayout(new GridLayout(2,1));
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));
P1.add(new Button("East-1"),BorderLayout.EAST);
P1.add(new Button("West-1"),BorderLayout.WEST);
P2.add(new Button("P2-btn-1"));
P2.add(new Button("P2-btn-2"));
P1.add(P2,BorderLayout.CENTER);
P3.add(new Button("East-2"),BorderLayout.EAST);
P3.add(new Button("West-2"),BorderLayout.WEST);
for (int i = 0; i < 4; i++) {
P4.add(new Button("for-"+i));
}
P3.add(P4,BorderLayout.CENTER);
frame.add(P1);
frame.add(P3);
}
}
3.事件监听
package com.wangxu.Lesson02;
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 TestActionEvent {
public static void main(String[] args) {
//按下按钮触发事件
Button button = new Button();
Frame frame = new Frame();
MyActionListener myActionListener = new MyActionListener();
button.addActionListener(myActionListener);
frame.add(button,BorderLayout.CENTER);
frame.pack();
WindowClose(frame);
frame.setVisible(true);
}
//关闭窗口事件
private static void WindowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
//事件监听
class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("aaaa");
}
}
多个按钮共享一个事件
package com.wangxu.Lesson02;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestActionTwo {
public static void main(String[] args) {
//两个按钮实现同一个监听
// 开始 停止
Frame frame = new Frame("开始-停止");
Button button1 = new Button("start");
Button button2 = new Button("Stop");
//可以显示的定义触发会返回的命令,如果不显示定义,则会走默认的值
//可以多个按钮去只写一个监听类
button2.setActionCommand("button2-stop");
MyMonitor myMonitor = new MyMonitor();
button1.addActionListener(myMonitor);
button2.addActionListener(myMonitor);
frame.add(button1,BorderLayout.NORTH);
frame.add(button2,BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
}
class MyMonitor implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//获取按钮的信息
System.out.println("按钮被点击了:msg"+e.getActionCommand());
}
}
4.输入框TextField监听
package com.wangxu.Lesson02;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestField01 {
public static void main(String[] args) {
//启动
new MyFrame();
}
}
class MyFrame extends Frame{
public MyFrame(){
TextField textField = new TextField();
add(textField);
//监听这个文本框输入的东西
MyActionListen2 myActionListen2 = new MyActionListen2();
//按下Enter就会触发输入框事件
textField.addActionListener(myActionListen2);
//设置替换编码
textField.setEchoChar('*');
setVisible(true);
pack();
}
}
class MyActionListen2 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
TextField field = (TextField)e.getSource();//获得一些资源,返回一个对象
System.out.println(field.getText());
field.setText("");
}
}
简易计算器
package com.wangxu.Lesson03;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Claculator {
public static void main(String[] args) {
new Clac();
}
}
//计算器类
class Clac extends Frame{
public Clac(){
//3个文本框
TextField num1 = new TextField(10);
TextField num2 = new TextField(10);
TextField num3 = new TextField(10);
//1个按钮
Button button = new Button("=");
button.addActionListener(new MyClacListener(num1,num2,num3));
//1个标签
Label label = new Label("+");
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
}
//监听器类
class MyClacListener implements ActionListener{
//获取3个变量
private TextField num1,num2,num3;
public MyClacListener(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 com.wangxu.Lesson03;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Claculator {
public static void main(String[] args) {
new Clac().loadFrame();
}
}
//计算器类
class Clac extends Frame{
TextField num1,num2,num3;
public void loadFrame(){
//3个文本框
num1 = new TextField(10);
num2 = new TextField(10);
num3 = new TextField(10);
Button button = new Button("=");
Label label = new Label("+");
button.addActionListener(new MyClacListener(this));
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
}
//监听器类
class MyClacListener implements ActionListener{
//获取计算器这个对象
Clac clac = null;
public MyClacListener(Clac clac){
this.clac=clac;
}
@Override
public void actionPerformed(ActionEvent e) {
//1.获得数字
int n1 = Integer.parseInt(clac.num1.getText());
int n2 = Integer.parseInt(clac.num2.getText());
//2.将这个值运算后放到第三个框
clac.num3.setText(""+(n1+n2));
//3.清除前两个框
clac.num1.setText("");
clac.num2.setText("");
}
}
内部类
- 优化代码
package com.wangxu.Lesson03;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Claculator {
public static void main(String[] args) {
new Clac().loadFrame();
}
}
//计算器类
class Clac extends Frame{
TextField num1,num2,num3;
public void loadFrame(){
//3个文本框
num1 = new TextField(10);
num2 = new TextField(10);
num3 = new TextField(10);
Button button = new Button("=");
Label label = new Label("+");
button.addActionListener(new MyClacListener());
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
private class MyClacListener implements ActionListener{
@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("");
}
}
}
//监听器类
class MyClacListener implements ActionListener{
//获取计算器这个对象
Clac clac = null;
public MyClacListener(Clac clac){
this.clac=clac;
}
@Override
public void actionPerformed(ActionEvent e) {
//1.获得数字
int n1 = Integer.parseInt(clac.num1.getText());
int n2 = Integer.parseInt(clac.num2.getText());
//2.将这个值运算后放到第三个框
clac.num3.setText(""+(n1+n2));
//3.清除前两个框
clac.num1.setText("");
clac.num2.setText("");
}
}
5.画笔
package com.wangxu.Lesson03;
import java.awt.*;
public class Paint {
public static void main(String[] args) {
new MyPaint().loadFrame();
}
}
class MyPaint extends Frame{
public void loadFrame(){
setBounds(200,200,600,400);
setVisible(true);
}
//画笔
@Override
public void paint(Graphics g) {
//画笔需要有颜色,可以画画
g.setColor(Color.cyan);
g.drawOval(100,100,100,100);//空心圆
g.fillOval(100,100,100,100);//实心圆
g.setColor(Color.GREEN);
g.fillRect(150,200,200,200);
//画笔用完将他还原为最初的颜色
}
}
6.鼠标监听
package com.wangxu.Lesson03;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Iterator;
//测试鼠标监听
public class TestMouseListener {
public static void main(String[] args) {
new MyFrame("画图");
}
}
class MyFrame extends Frame{
ArrayList points;
public MyFrame(String title) {
super(title);
setBounds(200,200,400,300);
//存鼠标点击的点
points = new ArrayList<>();
setVisible(true);
//鼠标监听器
this.addMouseListener(new MyMouseListener());
}
@Override
public void paint(Graphics g) {
//监听鼠标
Iterator iterator = points.iterator();
while (iterator.hasNext()){
Point point = (Point) iterator.next();
g.setColor(Color.GREEN);
g.fillOval(point.x,point.y,10,10);
}
}
//添加一个点到界面上
public void addPaint(Point point){
points.add(point);
}
//适配器模式
private class MyMouseListener extends MouseAdapter{
//鼠标点击
@Override
public void mousePressed(MouseEvent e) {
MyFrame myFrame = (MyFrame) e.getSource();
myFrame.addPaint(new Point(e.getX(),e.getY()));
//每次点击鼠标都要重画一遍
myFrame.repaint();
}
}
}
7.窗口监听
package com.wangxu.Lesson03;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestWindow {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame{
public WindowFrame() {
setBackground(Color.BLUE);
setBounds(100,100,200,200);
setVisible(true);
//addWindowFocusListener(new MyWindowListener());
this.addWindowListener(
new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.out.println("你点击了X");
}
}
);
}
class MyWindowListener extends WindowAdapter{
@Override
public void windowClosing(WindowEvent e) {
setVisible(false);//隐藏窗口
System.exit(0);//正常退出
}
}
}
8.键盘监听
package com.wangxu.Lesson03;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class TestKeyListener {
public static void main(String[] args) {
new KeyFrame();
}
}
class KeyFrame extends Frame{
public KeyFrame() {
setBounds(1,2,300,400);
setVisible(true);
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (KeyCode== KeyEvent.VK_UP){
System.out.println("你按下了上");
}
}
});
}
}
二.Swing
2.1窗口
package com.wangxu.Lesson04;
import javax.swing.*;
import java.awt.*;
public class JframeDeno02 {
public static void main(String[] args) {
new MyJframe2().inint();
}
}
class MyJframe2 extends JFrame{
public void inint(){
this.setVisible(true);
this.setBounds(10,10,200,300);
//设置文字Jlabel
JLabel jLabel = new JLabel("欢迎");
this.add(jLabel);
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
//容器实例化
Container container = getContentPane();
container.setBackground(Color.GREEN);
}
}
2.2弹窗
package com.wangxu.Lesson04;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class DialogDemo extends JFrame {
public DialogDemo() {
this.setVisible(true);
this.setSize(700,500);
//JFrame
Container container = this.getContentPane();
//绝对布局
container.setLayout(null);
//按钮
JButton button = new JButton("点击弹出一个对话框");
button.setBounds(30,30,200,50);
//点击按钮的时候弹出一个弹窗
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//弹窗
new MyDialogDemo();
}
});
container.add(button);
}
public static void main(String[] args) {
new DialogDemo();
}
}
//弹窗的窗口
class MyDialogDemo extends JDialog{
public MyDialogDemo() {
this.setVisible(true);
this.setBounds(100,100,500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container = this.getContentPane();
container.add(new Label("学习java"));
}
}
2.3面板
package com.wangxu.Lesson05;
import com.wangxu.Lesson04.JFrameDemo;
import javax.swing.*;
import java.awt.*;
public class JPanelDemo extends JFrame {
public JPanelDemo() {
Container container = this.getContentPane();
container.setLayout(new GridLayout(2,1,10,10));
JPanel panel = new JPanel(new GridLayout(1,3));
JPanel panel2 = new JPanel(new GridLayout(1,2));
JPanel panel3 = new JPanel(new GridLayout(2,3));
JPanel panel4 = new JPanel(new GridLayout(3,2));
panel.add(new JButton("1"));
panel.add(new JButton("1"));
panel.add(new JButton("1"));
panel2.add(new JButton("2"));
panel2.add(new JButton("2"));
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"));
container.add(panel);
container.add(panel2);
container.add(panel3);
container.add(panel4);
this.setVisible(true);
this.setSize(500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JFrameDemo();
}
}
JScrollPanel
package com.wangxu.Lesson05;
import javax.swing.*;
import java.awt.*;
public class JScrollDemo extends JFrame {
public JScrollDemo() {
Container container = this.getContentPane();
//文本域
JTextArea textArea = new JTextArea(20, 50);
textArea.setText("欢迎学习");
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
this.setVisible(true);
this.setBounds(100,100,300,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollDemo();
}
}