package com.lovo;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.ListSelectionModel;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
@SuppressWarnings("serial")
public class MyFrame extends JFrame implements ItemListener {
private JLabel helloLabel; // 标签
private JButton okButton, cancelButton; // 按钮
private JRadioButton[] fontsRadio = new JRadioButton[3]; // 单选按钮
private JCheckBox boldCheckBox, italicCheckBox; // 复选框
private JTextField nameField; // 文本框
private JComboBox<String> provComboBox, cityComboBox; // 下拉列表(组合框)
private JList<String> fruitList;
private JMenu fileMenu, helpMenu;
private JMenuBar bar;
private JPopupMenu popupMenu;
private int fontStyle = Font.PLAIN; // 默认字体样式
private String fontFamily = "黑体"; // 默认字体
private int fontSize = 36; // 默认字体大小
// 保存所有省的数组
private static String[] provNames = {"北京", "四川省", "广东省", "湖南省", "河南省", "河北省", "山东省"};
// 保存每个省的城市的二维数组(锯齿数组)
private static String[][] cities = {
{"东城区", "西城区", "海淀区", "朝阳区", "宣武区"},
{"成都", "绵阳", "自贡", "德阳", "乐山", "广元", "攀枝花", "西昌", "巴中", "南充", "内江", "甘孜", "阿坝"},
{"广州", "深圳", "肇庆", "东莞"},
{"长沙", "株洲", "怀化", "张家界"},
{"郑州", "洛阳", "许昌"},
{"石家庄", "保定", "邢台", "邯郸"},
{"济南", "烟台", "德州", "青岛"}
};
private static String[] listData = {"红色", "黄色", "绿色", "蓝色", "粉色", "橙色"};
private static Color[] colors = {Color.RED, Color.YELLOW, Color.GREEN, Color.BLUE, Color.PINK, Color.ORANGE};
public MyFrame() {
this.setSize(450, 600);
this.setIconImage(getToolkit().getImage("RC.GIF"));
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
initComponents();
}
// 初始化窗口控件的方法
private void initComponents() {
// 初始化标签
helloLabel = new JLabel("", JLabel.CENTER);
helloLabel.setIcon(new ImageIcon("RC.GIF"));
helloLabel.setText("欢迎来朗沃学习");
helloLabel.setToolTipText("这是一个标签");
helloLabel.setFont(new Font(fontFamily, fontStyle, fontSize));
this.add(helloLabel, BorderLayout.NORTH);
JPanel buttonPanel = new JPanel(); // 放按钮的面板
// 初始化按钮
okButton = new JButton("确定");
okButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String prov = provComboBox.getSelectedItem().toString();
String city = cityComboBox.getSelectedItem().toString();
JOptionPane.showMessageDialog(null, prov + "-" + city);
}
});
okButton.setIcon(new ImageIcon("RC.gif"));
okButton.setRolloverIcon(new ImageIcon("BC.gif"));
cancelButton = new JButton("取消");
cancelButton.setToolTipText("<html><div style='color:green;font-size:1cm;'>你好啊</div></html>");
cancelButton.setIcon(new ImageIcon("RN.gif"));
cancelButton.setRolloverIcon(new ImageIcon("BN.gif"));
buttonPanel.add(okButton);
buttonPanel.add(cancelButton);
this.add(buttonPanel, BorderLayout.SOUTH);
JPanel radioPanel = new JPanel(); // 放单选按钮的面板
radioPanel.setBorder(new TitledBorder("选择字体")); // 设置面板的边框
ButtonGroup group = new ButtonGroup();
String[] favNames = {"微软雅黑", "华文中宋", "隶书"};
// 初始化单选按钮
for(int i = 0; i < fontsRadio.length; i++) {
fontsRadio[i] = new JRadioButton(favNames[i]);
fontsRadio[i].addItemListener(this);
group.add(fontsRadio[i]); // 加到同一个按钮组中的单选按钮才是互斥的(只能选中其一)
radioPanel.add(fontsRadio[i]);
}
JPanel checkboxPanel = new JPanel(); // 放复选框的面板
checkboxPanel.setBorder(new TitledBorder("粗体/斜体")); // 设置面板的边框
// 初始化复选框
boldCheckBox = new JCheckBox("粗体");
boldCheckBox.addItemListener(this);
italicCheckBox = new JCheckBox("斜体");
italicCheckBox.addItemListener(this);
checkboxPanel.add(boldCheckBox);
checkboxPanel.add(italicCheckBox);
final JPanel centerPanel = new JPanel(); // 放窗口中央的面板
centerPanel.add(radioPanel);
centerPanel.add(checkboxPanel);
// 初始化文本框
nameField = new JTextField("请输入你的名字", 20);
// nameField.setHorizontalAlignment(JTextField.RIGHT); // 设置从右向左输入内容
// nameField.setEditable(false); // 设置文本框不可编辑
nameField.addFocusListener(new FocusListener() {
@Override
public void focusLost(FocusEvent e) {
if(nameField.getText().trim().equals("")) {
nameField.setText("请输入你的名字");
}
}
@Override
public void focusGained(FocusEvent e) {
if(nameField.getText().equals("请输入你的名字")) {
nameField.setText("");
}
}
});
nameField.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, nameField.getText());
}
});
centerPanel.add(nameField);
// 初始化省的下拉列表
provComboBox = new JComboBox<String>();
// 向省下拉列表添加数据
for (int i = 0; i < provNames.length; i++) {
provComboBox.addItem(provNames[i]);
}
// 初始化城市的下拉列表
cityComboBox = new JComboBox<String>();
cityComboBox.setMaximumRowCount(5);
// 向城市下拉列表添加数据
for (int i = 0; i < cities[0].length; i++) {
cityComboBox.addItem(cities[0][i]);
}
// 下拉列表选中项的监听器仍然是ItemListener
provComboBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
// 取省下拉列表选中项的索引
int index = provComboBox.getSelectedIndex();
// 删除下拉列表原来的所有项
cityComboBox.removeAllItems();
// 向下拉列表添加新元素
for(int i = 0; i < cities[index].length; i++) {
cityComboBox.addItem(cities[index][i]);
}
}
});
centerPanel.add(provComboBox);
centerPanel.add(cityComboBox);
// 初始化列表
fruitList = new JList<String>(listData);
fruitList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
fruitList.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
int index = fruitList.getSelectedIndex();
centerPanel.setBackground(colors[index]);
}
});
fruitList.setVisibleRowCount(4);
fruitList.setFixedCellHeight(50);
fruitList.setFixedCellWidth(100);
fruitList.setFont(new Font(fontFamily, fontStyle, fontSize));
centerPanel.add(new JScrollPane(fruitList));
this.add(centerPanel);
bar = new JMenuBar();
fileMenu = new JMenu("File");
fileMenu.setMnemonic('F'); // 为菜单设置加速键(Alt+字母)
JMenu newMenu = new JMenu("New");
JMenuItem item1 = new JMenuItem("Java");
JMenuItem item2 = new JMenuItem("C#");
JMenuItem item3 = new JMenuItem("C++");
JMenuItem item4 = new JMenuItem("Swift");
newMenu.add(item1);
newMenu.add(item2);
newMenu.add(item3);
newMenu.add(item4);
JMenuItem exitItem = new JMenuItem("Exit");
// 为菜单项设置快捷键
exitItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,
KeyEvent.CTRL_MASK | KeyEvent.ALT_MASK));
exitItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(JOptionPane.showConfirmDialog(null, "Are you sure?") == 0) {
System.exit(0);
}
}
});
fileMenu.add(newMenu);
fileMenu.add(new JSeparator());
fileMenu.add(exitItem);
helpMenu = new JMenu("Help");
bar.add(fileMenu);
bar.add(helpMenu);
this.setJMenuBar(bar);
// 初始化右键菜单
popupMenu = new JPopupMenu();
// 添加菜单项
JMenuItem redItem = new JMenuItem("红色");
redItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
centerPanel.setBackground(Color.RED);
}
});
JMenuItem yellowItem = new JMenuItem("黄色");
yellowItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
centerPanel.setBackground(Color.YELLOW);
}
});
popupMenu.add(redItem);
popupMenu.add(yellowItem);
popupMenu.add(new JSeparator());
popupMenu.add(exitItem);
// 通过鼠标事件来触发右键菜单
this.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
if(e.isPopupTrigger()) { // 是否触发右键菜单
popupMenu.show((Component) e.getSource(), e.getX(), e.getY());
}
}
});
}
public static void main(String[] args) {
new MyFrame().setVisible(true);
}
@Override
public void itemStateChanged(ItemEvent e) {
Object source = e.getSource(); // 获得事件源对象
if(source == boldCheckBox || source == italicCheckBox) { // 复选框
fontStyle = (boldCheckBox.isSelected()? Font.BOLD : Font.PLAIN) |
(italicCheckBox.isSelected()? Font.ITALIC : Font.PLAIN);
}
else { // 单选按钮
if(e.getStateChange() == ItemEvent.SELECTED) {
fontFamily = ((JRadioButton)source).getText();
}
}
helloLabel.setFont(new Font(fontFamily, fontStyle, fontSize)); // 重新为标签设置字体
}
}
GUI窗口
最新推荐文章于 2024-05-09 19:33:04 发布