package package01;
import jdk.internal.icu.text.UnicodeSet;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class SwingDemo {
JFrame f = new JFrame("测试基本组件");
//声明菜单相关的主键
JMenuBar menuBar = new JMenuBar();//菜单条
JMenu fileMenu = new JMenu("文件");//文件菜单
JMenu editMenu = new JMenu("编辑");//编辑菜单
JMenuItem auto = new JMenuItem("自动换行");//自动换行菜单项
JMenuItem copy = new JMenuItem("复制");//复制菜单项
JMenuItem paste = new JMenuItem("粘贴");//粘贴菜单项
JMenu formatMenu = new JMenu("格式");//格式菜单
JMenuItem comment = new JMenuItem("注释");//注释菜单项
JMenuItem cancelComment = new JMenuItem("取消注释");//取消注释菜单项
//声明文本域
JTextArea ta = new JTextArea(8, 20);//8行20列
String[] colors = {"红色", "绿色", "蓝色"};
//声明列表框
JList<String> colorList = new JList<String>(colors);
//声明选择相关按钮
//下拉选择框
JComboBox<String> colorSelcet = new JComboBox<String>();
//单选框,要放到
ButtonGroup bg = new ButtonGroup();
JRadioButton male = new JRadioButton("男", false);//默认不选
JRadioButton womale = new JRadioButton("女", true);//默认选中
//勾选框
JCheckBox isMarried = new JCheckBox("是否已婚", true);
//声明底部
JTextField tf = new JTextField(40);
JButton ok = new JButton("确定");
//右键菜单按钮组
ButtonGroup poupButtonBg = new ButtonGroup();
//声明右键菜单
JPopupMenu jPopupMenuItem = new JPopupMenu();
JRadioButtonMenuItem metalItem = new JRadioButtonMenuItem("Metal 风格");
JRadioButtonMenuItem nimbusItem = new JRadioButtonMenuItem("Nimbus 风格");
JRadioButtonMenuItem windowsItem = new JRadioButtonMenuItem("Windows 风格", true);//默认选中
JRadioButtonMenuItem windowsClassicItem = new JRadioButtonMenuItem("Windows 经典风格");
JRadioButtonMenuItem motifItem = new JRadioButtonMenuItem("Motif 风格");
//初始化界面
public void init() {
//组装视图
//组装底部
JPanel buttomPanel = new JPanel();
buttomPanel.add(tf);
buttomPanel.add(ok);
f.add(buttomPanel, BorderLayout.SOUTH);
//组装选择相关的组件
JPanel selectPanel = new JPanel();
//给colorSelcet添加颜色选项
colorSelcet.addItem("蓝色");
colorSelcet.addItem("红色");
colorSelcet.addItem("绿色");
selectPanel.add(colorSelcet);
bg.add(male);
bg.add(womale);
selectPanel.add(male);
selectPanel.add(womale);
selectPanel.add(isMarried);
//组件文本域和选择相关主键
Box topleft = Box.createVerticalBox();
topleft.add(ta);
topleft.add(selectPanel);
//组装顶部
Box top = Box.createHorizontalBox();
top.add(topleft);
top.add(colorList);
f.add(top);
//组装顶部菜单
formatMenu.add(comment);
formatMenu.add(cancelComment);
editMenu.add(auto);
editMenu.addSeparator();//添加横杆
editMenu.add(copy);
editMenu.add(paste);
editMenu.addSeparator();//添加横杆
editMenu.add(formatMenu);
//把编辑和文件组装到菜单条中
menuBar.add(fileMenu);
menuBar.add(editMenu);
//显示
f.setJMenuBar(menuBar);
//组装右键菜单
poupButtonBg.add(metalItem);
poupButtonBg.add(nimbusItem);
poupButtonBg.add(windowsItem);
poupButtonBg.add(windowsClassicItem);
poupButtonBg.add(motifItem);
//点击效果
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//当前选择的是哪一个风格
String actionCommand = e.getActionCommand();
//修改外观
try {
changeFlavor(actionCommand);
} catch (Exception ex) {
ex.printStackTrace();
}
}
};
metalItem.addActionListener(listener);
nimbusItem.addActionListener(listener);
windowsItem.addActionListener(listener);
windowsClassicItem.addActionListener(listener);
motifItem.addActionListener(listener);
jPopupMenuItem.add(metalItem);
jPopupMenuItem.add(nimbusItem);
jPopupMenuItem.add(windowsItem);
jPopupMenuItem.add(windowsClassicItem);
jPopupMenuItem.add(motifItem);
//不需要再监听鼠标事件了
ta.setComponentPopupMenu(jPopupMenuItem);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
//修改外观的方法:改变界面的风格
private void changeFlavor(String command) throws Exception{
switch (command){
case "Metal 风格":
UIManager.setLookAndFeel("java.swing.plaf.metal.MetalLookAndFell");
break;
case "Nimbus 风格":
UIManager.setLookAndFeel("java.swing.plaf.nimbus.NimbusLookAndFell");
break;
case "Windows 风格":
UIManager.setLookAndFeel("java.swing.plaf.windows.WindowsLookAndFell");
break;
case "Windows 经典风格":
UIManager.setLookAndFeel("java.swing.plaf.windowsClassic.WindowsClassicLookAndFell");
break;
case "Motif 风格":
UIManager.setLookAndFeel("java.swing.plaf.motif.MotifLookAndFell");
break;
}
//刷新组件的外观
SwingUtilities.updateComponentTreeUI(f.getContentPane());
SwingUtilities.updateComponentTreeUI(menuBar);
SwingUtilities.updateComponentTreeUI(jPopupMenuItem);
}
//运行程序
public static void main(String[] args) {
new SwingDemo().init();
}
}
Swing入门:组件的基本使用
最新推荐文章于 2023-09-14 11:30:30 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)