Java快速上手之实验五
1. 编写JButtonDemo.java。
(1)使用水平布局放置2个按钮,其中按钮2为图标按钮(使用ImageIcon)。
(2)当按下按钮时弹出相应的对话框。
2. 编写JRadioButtonDemo.java。根据所选按钮改变背景色。
3. 编写JCheckBoxDemo.java。根据所选按钮改变标签的效果。
4. 编写JComboBoxDemo.java。在标签中显示所选列表项内容。
5.编写ColorDialogDemo.java,点击按钮弹出颜色对话框,并用选中的颜色作为窗口的背景色。
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;public class JButtonDemo {public static void main(String[] args) {// 创建主窗口JFrame frame = new JFrame("测试按钮类");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize(300, 150);frame.setLayout(new FlowLayout()); // 水平布局// 创建按钮1JButton button1 = new JButton("按钮1");button1.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// 弹出对话框JOptionPane.showMessageDialog(frame, "你按了:按钮1", "消息", JOptionPane.INFORMATION_MESSAGE);}});// 创建按钮2(图标按钮)ImageIcon icon = new ImageIcon("java-icon.png"); JButton button2 = new JButton("按钮2", icon);button2.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// 弹出对话框JOptionPane.showMessageDialog(frame, "你按了:按钮2", "消息", JOptionPane.INFORMATION_MESSAGE);}});// 将按钮添加到窗口frame.add(button1);frame.add(button2);// 设置窗口可见frame.setVisible(true);}
}2.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;public class JRadioButtonDemo {public static void main(String[] args) {// 创建主窗口JFrame frame = new JFrame("单选框");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize(300, 200);frame.setLayout(new BorderLayout());// 创建面板,用于显示背景颜色JPanel colorPanel = new JPanel();colorPanel.setBackground(Color.RED); // 默认背景颜色为红色frame.add(colorPanel, BorderLayout.CENTER);// 创建单选按钮JRadioButton redButton = new JRadioButton("红色", true); // 默认选中JRadioButton greenButton = new JRadioButton("绿色");JRadioButton blueButton = new JRadioButton("蓝色");// 将单选按钮添加到按钮组,确保只能单选ButtonGroup group = new ButtonGroup();group.add(redButton);group.add(greenButton);group.add(blueButton);// 创建面板用于放置单选按钮JPanel buttonPanel = new JPanel();buttonPanel.setLayout(new FlowLayout());buttonPanel.add(redButton);buttonPanel.add(greenButton);buttonPanel.add(blueButton);frame.add(buttonPanel, BorderLayout.SOUTH);// 添加事件监听器,实现背景颜色更改redButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {colorPanel.setBackground(Color.RED);}});greenButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {colorPanel.setBackground(Color.GREEN);}});blueButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {colorPanel.setBackground(Color.BLUE);}});// 设置窗口可见frame.setVisible(true);}
}3.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;public class JCheckBoxDemo {public static void main(String[] args) {// 创建主窗口JFrame frame = new JFrame("复选框");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize(400, 200);frame.setLayout(new BorderLayout());// 创建标签JLabel label = new JLabel("请注意观察字体的变化", JLabel.CENTER);label.setFont(new Font("宋体", Font.PLAIN, 20)); // 初始字体label.setOpaque(true); // 设置背景颜色可见label.setBackground(Color.YELLOW); // 设置背景颜色frame.add(label, BorderLayout.CENTER);// 创建复选框JCheckBox boldCheckBox = new JCheckBox("粗体");JCheckBox italicCheckBox = new JCheckBox("斜体", true); // 默认选中// 创建面板用于放置复选框JPanel checkBoxPanel = new JPanel();checkBoxPanel.add(boldCheckBox);checkBoxPanel.add(italicCheckBox);frame.add(checkBoxPanel, BorderLayout.SOUTH);// 添加复选框的监听器ItemListener listener = new ItemListener() {@Overridepublic void itemStateChanged(ItemEvent e) {// 初始化字体样式int style = Font.PLAIN;if (boldCheckBox.isSelected()) {style += Font.BOLD;}if (italicCheckBox.isSelected()) {style += Font.ITALIC;}// 更新标签字体样式label.setFont(new Font("宋体", style, 20));}};// 为复选框添加监听器boldCheckBox.addItemListener(listener);italicCheckBox.addItemListener(listener);// 设置窗口可见frame.setVisible(true);}
}4.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;public class JComboBoxDemo {public static void main(String[] args) {// 创建主窗口JFrame frame = new JFrame("下拉列表");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize(300, 150);frame.setLayout(new FlowLayout()); // 使用流式布局// 创建标签,用于显示选择结果JLabel label = new JLabel("你选择了:上海");label.setOpaque(true);label.setBackground(Color.YELLOW); // 设置背景颜色frame.add(label);// 创建下拉列表String[] cities = {"上海", "北京", "广州", "深圳"};JComboBox<String> comboBox = new JComboBox<>(cities);frame.add(comboBox);// 为下拉列表添加事件监听器comboBox.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// 获取用户选择的项String selectedCity = (String) comboBox.getSelectedItem();// 更新标签内容label.setText("你选择了:" + selectedCity);}});// 设置窗口可见frame.setVisible(true);}
}5.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;public class ColorDialogDemo {public static void main(String[] args) {// 创建主窗口JFrame frame = new JFrame("带颜色对话框的窗口");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize(400, 300);frame.setLayout(new BorderLayout());// 设置初始背景颜色frame.getContentPane().setBackground(Color.BLUE);// 创建按钮JButton button = new JButton("打开颜色对话框");button.setFont(new Font("宋体", Font.PLAIN, 16));frame.add(button, BorderLayout.CENTER);// 添加按钮的事件监听button.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// 弹出颜色选择对话框Color selectedColor = JColorChooser.showDialog(frame, "选择颜色", frame.getContentPane().getBackground());if (selectedColor != null) {// 设置选中颜色为背景色frame.getContentPane().setBackground(selectedColor);}}});// 设置窗口可见frame.setVisible(true);}
}