一、演示
1 外形
2 编辑
3 颜色
4 男女和是否已婚
5 鼠标右击,改变风格
二、代码
package learn;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.http.WebSocket;
public class swingComponentDemo {
JFrame f = new JFrame("测试Swing基本组件");
//声明菜单
JMenuBar menu = 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 cannelComment = new JMenuItem("取消注释");
JTextArea ta = new JTextArea();
String[] colors = {"绿色","红色","蓝色"};
JList<String> colorList = new JList<String>(colors);
JComboBox<String> colorSelected = new JComboBox<>();
ButtonGroup bg = new ButtonGroup();
JRadioButton male = new JRadioButton("男",false);
JRadioButton female = new JRadioButton("女",true);
JCheckBox isMarried = new JCheckBox("是否已婚",true);
//声明右键菜单
JPopupMenu jPopupMenu = new JPopupMenu();
ButtonGroup poupButtonBg = new ButtonGroup();
JRadioButtonMenuItem metal = new JRadioButtonMenuItem("metal风格");
JRadioButtonMenuItem nimbus= new JRadioButtonMenuItem("nimbus风格");
JRadioButtonMenuItem windows = new JRadioButtonMenuItem("windows风格",true);
JRadioButtonMenuItem windoesClassic = new JRadioButtonMenuItem("windows经典风格");
//初始化界面
public void init()
{
//组装视图
JTextField tf = new JTextField(40);
JButton ok = new JButton("确定");
//组装底部
JPanel bottomJPanel = new JPanel();
bottomJPanel.add(tf);
bottomJPanel.add(ok);
f.add(bottomJPanel, BorderLayout.SOUTH);
JPanel selectPanel = new JPanel();
colorSelected.addItem("绿色");
colorSelected.addItem("红色");
colorSelected.addItem("蓝色");
selectPanel.add(colorSelected);
bg.add(male);
bg.add(female);
selectPanel.add(male);
selectPanel.add(female);
selectPanel.add(isMarried);
Box leftTop = Box.createVerticalBox();
leftTop.add(ta);
leftTop.add(selectPanel);
Box top = Box.createHorizontalBox();
top.add(leftTop);
top.add(colorList);
f.add(top);
formatMenu.add(comment);
formatMenu.add(cannelComment);
editMenu.add(auto);
editMenu.addSeparator();
editMenu.add(copy);
editMenu.add(paste);
editMenu.addSeparator();
editMenu.add(formatMenu);
menu.add(fileMenu);
menu.add(editMenu);
f.setJMenuBar(menu);
poupButtonBg.add(metal);
poupButtonBg.add(nimbus);
poupButtonBg.add(windows);
poupButtonBg.add(windoesClassic);
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String actionCommend = e.getActionCommand();
try {
changeFlavor(actionCommend);
} catch (Exception ex) {
ex.printStackTrace();
}
}
};
metal.addActionListener(listener);
nimbus.addActionListener(listener);
windows.addActionListener(listener);
windoesClassic.addActionListener(listener);
jPopupMenu.add(metal);
jPopupMenu.add(nimbus);
jPopupMenu.add(windows);
jPopupMenu.add(windoesClassic);
ta.setComponentPopupMenu(jPopupMenu);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
private void changeFlavor(String commend) throws Exception
{
switch(commend)
{
case "metal风格":
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
break;
case "nimbus风格":
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
break;
case "windows风格":
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
break;
case "windows经典风格":
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
break;
}
SwingUtilities.updateComponentTreeUI(f.getContentPane());
SwingUtilities.updateComponentTreeUI(menu);
SwingUtilities.updateComponentTreeUI(jPopupMenu);
}
public static void main(String[] args) {
new swingComponentDemo().init();
}
}