GUI
AWT
Frame 窗口
import java.awt.*;
class TestFrame {
public static void main(String[] args) {
// 创建窗口
Frame frame = new Frame("GUI");
// 设置可见性
frame.setVisible(true);
// 设置窗口大小
frame.setSize(400, 400);
// 设置窗口颜色
frame.setBackground(Color.LIGHT_GRAY);
// 设置窗口弹出位置
frame.setLocation(200, 200);
// 设置窗口大小固定
frame.setResizable(false);
}
}
Panel 面板
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
// Panel : 可以看成一个空间,但是不能单独存在
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(Color.darkGray);
//panel 设置坐标,相对于 frame
panel.setBounds(50, 50, 400, 400);
panel.setBackground(Color.CYAN);
// 将面板添加到窗口上
frame.add(panel);
frame.setVisible(true);
// 监听事件,监听窗口关闭事件
// WindowAdapter 是一个抽象类,实现了 WindowListener 接口
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
布局
流式布局
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());
// 设置为流式布局(靠左)
frame.setLayout(new FlowLayout(FlowLayout.LEFT));
frame.setSize(200, 200);
// 添加按钮
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setVisible(true);
}
}
布局
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 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);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
表格布局
import java.awt.*;
public class TestGridLayout {
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.setLayout(new GridLayout(3, 2));
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
frame.add(button5);
frame.add(button6);
frame.pack();// 自动布局
frame.setSize(200, 200);
frame.setVisible(true);
}
}
画笔
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestPaint {
public static void main(String[] args) {
new MyPaint().loadFrame();
}
}
class MyPaint extends Frame {
public void loadFrame() {
setBounds(200, 200, 800, 600);
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
@Override
public void paint(Graphics g) {
g.setColor(Color.CYAN);
// 画矩形
g.drawRect(80, 80, 50, 50);
g.setColor(Color.GRAY);
// 画填充圆
g.fillOval(130, 130, 50, 50);
}
}
事件监听
按钮监听
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) {
Frame frame = new Frame();
Button button = new Button("button");
// 按钮设置监听
button.addActionListener(new MyActionListener());
frame.add(button, BorderLayout.CENTER);
// 设置窗口关闭
windowClose(frame);
frame.setSize(200, 200);
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("DeBug");
}
}
文本域监听
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 TestText01 {
public static void main(String[] args) {
new MyFrame();
}
}
class MyFrame extends Frame {
public MyFrame() {
// 文本域
TextField textField = new TextField();
add(textField);
// 监听文本框输入的文字
MyActionListener2 myActionListener2 = new MyActionListener2();
// 按下 enter 键就会触发这个输入框中的事件
textField.addActionListener(myActionListener2);
// 设置替换编码
textField.setEchoChar('*');
setVisible(true);
pack();
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyActionListener2 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
TextField field = (TextField) e.getSource();// 得到一些资源,返回一个 Object 对象
System.out.println(field.getText());// 获取输入框中的一些文本
field.setText("");// 设置每次按下 enter 键文本内容清空
}
}
鼠标监听事件
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
// 鼠标监听事件
public class TestMouseListener {
public static void main(String[] args) {
new MyFrame("draw");
}
}
class MyFrame extends Frame {
// 画画需要画笔,需要监听鼠标当前的位置,需要集合存储位置
private List<Point> points;
public MyFrame(String title) {
super(title);
setBounds(200, 200, 800, 600);
setVisible(true);
points = new ArrayList<>();
// 添加鼠标监听器
addMouseListener(new MyMouseListener());
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
@Override
public void paint(Graphics g) {
// 将全部点绘制
Iterator<Point> iterator = points.iterator();
while (iterator.hasNext()) {
Point point = iterator.next();
g.setColor(Color.BLUE);
g.fillOval(point.x, point.y, 10, 10);
}
}
// 添加一个点到集合中
public void addPoint(Point point) {
points.add(point);
}
private class MyMouseListener extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
MyFrame frame = (MyFrame) e.getSource();
frame.addPoint(new Point(e.getX(), e.getY()));
// 每次点击鼠标都需要重画一遍
frame.repaint();
}
}
}
窗口监听事件
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() {
setVisible(true);
setBounds(400, 400, 200, 200);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
@Override
public void windowActivated(WindowEvent e) {
WindowFrame frame = (WindowFrame) e.getSource();
frame.setTitle("被激活了");
}
});
}
}
键盘监听事件
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(600, 200, 800, 600);
setVisible(true);
addKeyListener(new KeyAdapter() {
// 键盘按下
@Override
public void keyPressed(KeyEvent e) {
// 得到当前按下的键是哪一个
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_UP) {
System.out.println("你按下了上键");
}
}
});
}
}
Swing
JFrame 窗口
import javax.swing.*;
import java.awt.*;
public class JFrameDemo {
public void init() {
JFrame frame = new JFrame("MyFirstJFrame");
frame.setBounds(600, 200, 800, 600);
frame.setVisible(true);
// 得到容器
Container contentPane = frame.getContentPane();
contentPane.setBackground(Color.PINK);
// 标签组件
JLabel jLabel = new JLabel("Welcome to SYU");
frame.add(jLabel);
// 设置文字水平居中
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
// 设置窗口关闭
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JFrameDemo().init();
}
}
JDialog 弹窗
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class DialogDemo extends JFrame {
public DialogDemo() {
setVisible(true);
setBounds(600, 600, 800, 600);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
// 获得容器
Container contentPane = getContentPane();
contentPane.setLayout(null);
JButton jButton = new JButton("按钮");
jButton.setBounds(30, 30, 200, 50);
contentPane.add(jButton);
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 弹窗
new MyDialog();
}
});
}
public static void main(String[] args) {
new DialogDemo();
}
}
class MyDialog extends JDialog {
public MyDialog() {
setVisible(true);
setBounds(100, 100, 500, 500);
Container contentPane = getContentPane();
contentPane.setLayout(null);
JLabel jLabel = new JLabel("Hello World");
jLabel.setSize(300, 300);
contentPane.add(jLabel);
}
}
Icon 标签
import javax.swing.*;
import java.awt.*;
public class IconDemo extends JFrame implements Icon {
private int width;
private int height;
public IconDemo() {
}
public IconDemo(int width, int height) {
this.width = width;
this.height = height;
}
public void init() {
IconDemo iconDemo = new IconDemo(15, 15);
// 图标放在标签上,也可以放在按钮上
JLabel jLabel = new JLabel("iconTest", iconDemo, SwingConstants.CENTER);
Container contentPane = getContentPane();
contentPane.add(jLabel);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new IconDemo().init();
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.fillOval(x, y, width, height);
}
@Override
public int getIconWidth() {
return width;
}
@Override
public int getIconHeight() {
return height;
}
}
ImageIcon 标签
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class ImageIconDemo extends JFrame {
public ImageIconDemo() {
JLabel jLabel = new JLabel("ImageIcon");
// 获取图片地址
URL url = ImageIconDemo.class.getResource("");
ImageIcon imageIcon = new ImageIcon(url);
jLabel.setIcon(imageIcon);
// 设置水平居中
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
Container contentPane = getContentPane();
contentPane.add(jLabel);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new ImageIconDemo();
}
}
面板
JPanel
import javax.swing.*;
import java.awt.*;
public class JPanelDemo extends JFrame {
public JPanelDemo() {
Container contentPane = getContentPane();
// 后面 2 个参数:间距
contentPane.setLayout(new GridLayout(2, 1, 10, 10));
JPanel jPanel1 = new JPanel(new GridLayout(1, 3));
jPanel1.add(new JButton("1"));
jPanel1.add(new JButton("1"));
jPanel1.add(new JButton("1"));
contentPane.add(jPanel1);
setVisible(true);
setSize(800, 600);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JPanelDemo();
}
}
JScroll
import javax.swing.*;
import java.awt.*;
public class JScrollDemo extends JFrame {
public JScrollDemo() {
Container contentPane = getContentPane();
// 文本域
JTextArea jTextArea = new JTextArea(20, 50);
jTextArea.setText("Hello World");
// JScroll 面板
JScrollPane jScrollPane = new JScrollPane(jTextArea);
contentPane.add(jScrollPane);
setVisible(true);
setBounds(400, 400, 800, 600);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollDemo();
}
}
按钮
图片按钮
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo01 extends JFrame {
public JButtonDemo01() {
Container contentPane = getContentPane();
URL url = JButtonDemo01.class.getResource("pk.png");
ImageIcon imageIcon = new ImageIcon(url);
JButton jButton = new JButton();
jButton.setIcon(imageIcon);
// 当鼠标放在按钮上时的提示
jButton.setToolTipText("这是一个带图片的按钮");
contentPane.add(jButton);
setVisible(true);
setBounds(500, 200, 800, 600);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo01();
}
}
单选按钮
import javax.swing.*;
import java.awt.*;
public class JButtonDemo02 extends JFrame {
public JButtonDemo02() {
Container contentPane = getContentPane();
// 单选按钮
JRadioButton jRadioButton01 = new JRadioButton("jRadioButton01");
JRadioButton jRadioButton02 = new JRadioButton("jRadioButton02");
JRadioButton jRadioButton03 = new JRadioButton("jRadioButton03");
// 按钮分组,一个组只能选择一个单选按钮
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(jRadioButton01);
buttonGroup.add(jRadioButton02);
buttonGroup.add(jRadioButton03);
contentPane.add(jRadioButton01, BorderLayout.CENTER);
contentPane.add(jRadioButton02, BorderLayout.NORTH);
contentPane.add(jRadioButton03, BorderLayout.SOUTH);
setVisible(true);
setBounds(500, 200, 800, 600);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo02();
}
}
多选按钮
import javax.swing.*;
import java.awt.*;
public class JButtonDemo03 extends JFrame {
public JButtonDemo03() {
Container contentPane = getContentPane();
// 多选按钮
Checkbox checkbox01 = new Checkbox("checkbox01");
Checkbox checkbox02 = new Checkbox("checkbox02");
contentPane.add(checkbox01, BorderLayout.NORTH);
contentPane.add(checkbox02, BorderLayout.SOUTH);
setVisible(true);
setBounds(500, 200, 800, 600);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo03();
}
}
框
下拉框
import javax.swing.*;
import java.awt.*;
public class TestComboBoxDemo01 extends JFrame {
public TestComboBoxDemo01() {
Container contentPane = getContentPane();
// 下拉框
JComboBox status = new JComboBox();
status.addItem(null);
status.addItem("正在热映");
status.addItem("已下架");
status.addItem("即将上映");
contentPane.add(status);
setVisible(true);
setBounds(500, 200, 800, 600);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboBoxDemo01();
}
}
列表框
import javax.swing.*;
import java.awt.*;
public class TestComboBoxDemo02 extends JFrame {
public TestComboBoxDemo02() {
Container contentPane = getContentPane();
// 生成列表的内容
String[] contents = {"1", "2", "3"};
// 列表中需要放入内容
JList jList = new JList(contents);
contentPane.add(jList);
setVisible(true);
setBounds(500, 200, 800, 600);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboBoxDemo02();
}
}
文本框
import javax.swing.*;
import java.awt.*;
public class TestTextDemo01 extends JFrame {
public TestTextDemo01() {
Container contentPane = getContentPane();
// 文本框
JTextField jTextField1 = new JTextField("Hello");
JTextField jTextField2 = new JTextField("World");
contentPane.add(jTextField1, BorderLayout.EAST);
contentPane.add(jTextField2, BorderLayout.WEST);
setVisible(true);
setBounds(500, 200, 800, 600);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestTextDemo01();
}
}
密码框
import javax.swing.*;
import java.awt.*;
public class TestTextDemo02 extends JFrame {
public TestTextDemo02() {
Container contentPane = getContentPane();
// 密码框
JPasswordField jPasswordField = new JPasswordField();
// 设置密码显示符
jPasswordField.setEchoChar('*');
contentPane.add(jPasswordField);
setVisible(true);
setBounds(500, 200, 800, 600);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestTextDemo02();
}
}
文本域
见 JScroll