AWT
画笔
可以通过重写Frame类中的paint(Graphics g)方法来绘制图形。
class Paint extends Frame {
public void loadFrame() {
setBounds(200, 200, 300, 300);
setVisible(true);
}
@Override
public void paint(Graphics g) {
g.setColor(Color.CYAN);
g.fill3DRect(130, 190, 30, 40, true);
g.setColor(Color.GRAY);
g.drawOval(80, 90, 100, 100);
// 画笔使用规范:使用后还原为初始颜色
g.setColor(Color.BLACK);
}
}
鼠标监听
class MouseFrame extends Frame {
ArrayList<Point> points = new ArrayList<>();
public MouseFrame(String title) {
super(title);
setBounds(200, 200, 300, 300);
// 添加鼠标监听器
this.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
points.add(new Point(e.getX(), e.getY()));
MouseFrame source = (MouseFrame) e.getSource();
source.repaint(); // 再次调用paint函数
}
});
setVisible(true);
}
@Override
public void paint(Graphics g) {
// 通过for-each循环画出points中所有的点
for (Point point : points) {
g.fillOval(point.x, point.y, 10, 10);
}
}
}
窗口监听
class WindowFrame extends Frame {
public void loadFrame() {
setBounds(200, 200, 300, 300);
setBackground(Color.GRAY);
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
setVisible(false);
System.exit(0); // 0:正常退出
}
});
}
}
键盘监听
class KeyListener extends Frame {
public void loadFrame() {
setBounds(200, 200, 300, 300);
setVisible(true);
this.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
// getKetChar:获取按下的字符,非字符键无响应
System.out.println(e.getKeyChar());
// getKeyChar:获取按下的键对应的编码
System.out.println(e.getKeyCode());;
}
});
}
}
Swing
Swing是对AWT的改良和扩展。
窗口
(内容区)背景颜色要通过容器来设置
JFrame jf = new JFrame("JFrame");
// 通过容器来设置
jf.getContentPane().setBackground(Color.cyan);
jf.setBounds(200, 200, 300, 300);
// 添加文字
JLabel jl = new JLabel("JLabel");
// 设置居中
jl.setHorizontalAlignment(SwingConstants.CENTER);
jl.setVerticalAlignment(SwingConstants.TOP);
jf.add(jl);
jf.setVisible(true);
// 关闭事件
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
弹窗
JDialog自带关闭事件,不用显示定义
setBounds(200, 200, 300, 300);
setVisible(true);
getContentPane().setBackground(Color.ORANGE);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
// JFrame的控件要放在contentpane容器中
Container contentPane = getContentPane();
JButton button = new JButton("123");
button.setBounds(50, 50, 100, 100);
button.addActionListener(e -> {
JDialog jDialog = new JDialog();
jDialog.setVisible(true);
jDialog.setBounds(200, 300, 300, 100);
// JDialog自带关闭事件,不用显示定义
// jDialog.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container = jDialog.getContentPane();
JLabel warning = new JLabel("Warning!!!!!");
warning.setHorizontalAlignment(SwingConstants.CENTER);
container.add(warning);
});
contentPane.add(button);
Icon
图标可以放在按钮或标签中(只能添加到Swing中的控件上,如:JButton、JLabel等),图标分为绘制的图形icon和加载的图片icon。
图形icon:
Container container = getContentPane();
setBounds(200, 200, 300, 300);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Icons icon = new Icons(15, 15);
JButton button = new JButton("123", icon);
button.setBounds(20, 20, 60, 30);
JLabel label = new JLabel("JLabel", icon, SwingConstants.CENTER);
container.add(button);
container.add(label);
setVisible(true);
图片Icon:
// 获取当前类的路径下的资源
URL url = this.getClass().getResource("logo.png");
// 创建ImageIcon
assert url != null;
ImageIcon imageIcon = new ImageIcon(url);
// 将Icon添加到JLabel上,设置相关属性
JLabel jLabel = new JLabel("JLabel");
jLabel.setIcon(imageIcon);
jLabel.setVerticalAlignment(SwingConstants.CENTER);
// 在内容区域添加JLabel,设置相关属性
Container contentPane = getContentPane();
contentPane.add(jLabel);
setVisible(true);
pack();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
面板
面板分为普通面板JPanel
和可以滚动的面板JScrollPane
。
普通面板:
// 网格布居:当添加的组件数超过其容纳个数时,会自动优化以容纳这些组件
Container container = getContentPane();
setLayout(new GridLayout(3, 1, 10, 10));
// 创建面板
JPanel panel1 = new JPanel(new GridLayout(2, 1));
JPanel panel2 = new JPanel(new GridLayout(1, 2));
JPanel panel3 = new JPanel(new GridLayout(2, 3));
JPanel panel4 = new JPanel(new GridLayout(2, 1));
JPanel panel5 = new JPanel(new GridLayout(3, 1));
JPanel panel6 = new JPanel(new GridLayout(2, 1));
// 添加组件
{...}
pack();
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
滚动面板:
Container container = getContentPane();
// JScrollPane在创建时直接加入组件
JScrollPane scrollPane = new JScrollPane(new JTextArea("Hello\nWorld", 30, 50));
container.add(scrollPane);
setVisible(true);
setBounds(200, 200, 300, 300);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
滚动面板效果:
Button
按钮包括普通按钮,单选按钮和复选按钮。按钮上都可以添加icon。
为按钮添加icon:
// 带图标的按钮
JButton jButton = new JButton("JButton");
jButton.setToolTipText("ImageIcon");
jButton.setIcon(imageIcon);
panel1.add(jButton);
单选按钮(RadioButton):单选按钮实现单选效果要通过放置在一个按钮组中实现:
// 单选框:JRadioButton
JRadioButton jRadioButton1 = new JRadioButton("JRadioButton1");
JRadioButton jRadioButton2 = new JRadioButton("JRadioButton2");
JRadioButton jRadioButton3 = new JRadioButton("JRadioButton3");
// 单选需要将单选按钮放置在同一个分组中实现
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(jRadioButton1);
buttonGroup.add(jRadioButton2);
buttonGroup.add(jRadioButton3);
panel2.add(jRadioButton1);
panel2.add(jRadioButton2);
panel2.add(jRadioButton3);
复选框(CheckBox):复选框不用放置在按钮组中
JCheckBox jCheckBox1 = new JCheckBox("JCheckBox1");
JCheckBox jCheckBox2 = new JCheckBox("JCheckBox2");
JCheckBox jCheckBox3 = new JCheckBox("JCheckBox3");
panel3.add(jCheckBox1);
panel3.add(jCheckBox2);
panel3.add(jCheckBox3);
列表
列表包括下拉框和列表框,都是用于单选的。
下拉框(ComboBox):通过addItem()
方法添加内容。
// 下拉框
JComboBox<String> messages = new JComboBox<>();
messages.addItem(null);
messages.addItem("Peaky Blinder");
messages.addItem("Harry Porter");
messages.addItem("Venom");
panel1.add(messages);
列表框(JList):通过在创建列表的时候传入数组来传入内容,可以传入静态数组,也可以传入动态数组,后续添加内容。
// 列表框
// 静态生成内容
String[] content = {"demo1", "demo2", "demo3"};
JList<String> strList1 = new JList<>(content);
panel2.add(strList1);
// 动态添加内容
String[] contents = new String[4];
JList<String> strList2 = new JList<>(contents);
panel3.add(strList2);
contents[0] = "1234";
contents[1] = "2234";
contents[2] = "3234";
contents[3] = "4234";
文本框
Swing提供了单行文本、密码框及多行文本。
单行文本:
// 单行文本框
JTextField text1 = new JTextField("Default Text", 20);
container.add(text1);
密码框:
// 密码框JPasswordField
JPasswordField passwordField = new JPasswordField();
container.add(passwordField);
多行文本:多行文本可以添加JScrollPane
来实现下拉的效果。
// 文本域JTextArea
JTextArea TextArea = new JTextArea("?????", 50, 20);
JScrollPane scrollPane = new JScrollPane(TextArea);
container.add(scrollPane);