【JAVA|Swing】测试基本组件

一、演示

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();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ོ栖落

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值