ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

learning java swing 基本组件用法

2019-08-02 16:00:53  阅读:329  来源: 互联网

标签:case JRadioButtonMenuItem java swing break add learning JMenuItem new


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;

public class SwingComponent {
    JFrame f = new JFrame("test");
    Icon okIcon = new ImageIcon("ico/ok.png");

    JButton ok = new JButton("确认", okIcon);

    JRadioButton male  = new JRadioButton("男", true);
    JRadioButton female = new JRadioButton("女", false);

    ButtonGroup bg = new ButtonGroup();

    JCheckBox married =  new  JCheckBox("是否已婚", false);

    String[]  colors = new String[]{"红色","绿色","蓝色"};

    JComboBox<String>  colorChooser = new JComboBox<>(colors);

    JList<String> colorList = new JList<>(colors);

    JTextArea ta  = new JTextArea(8, 20);

    JTextField name = new JTextField(40);

    JMenuBar mb = new JMenuBar();

    JMenu file = new JMenu("文件");

    JMenu edit = new JMenu("编辑");

    Icon newIcon  =  new ImageIcon("ico/new.png");
    JMenuItem newItem = new JMenuItem("新建", newIcon);

    Icon saveIcon = new ImageIcon("ico/save.png");
    JMenuItem saveItem = new JMenuItem("保存", saveIcon);

    Icon exitIcon = new ImageIcon("ico/exit.png");
    JMenuItem exitItem = new JMenuItem("退出",exitIcon);

    JCheckBoxMenuItem autoWrap = new JCheckBoxMenuItem("自动换行");

    JMenuItem copyItem  = new JMenuItem("复制", new ImageIcon("ico/copy.png"));

    JMenuItem pasteItem  = new JMenuItem("粘贴", new ImageIcon("ico/paste.png"));

    JMenu format = new JMenu("格式");

    JMenuItem commentItem  = new JMenuItem("注释");
    JMenuItem cancelItem = new JMenuItem("取消注释");

    JPopupMenu pop  = new JPopupMenu();

    ButtonGroup flavorGroup = new ButtonGroup();

    JRadioButtonMenuItem metalItem  = new JRadioButtonMenuItem("Metal风格", true);
    JRadioButtonMenuItem nimbusItem = new JRadioButtonMenuItem("Nimbus风格");
    JRadioButtonMenuItem windowsItem  = new JRadioButtonMenuItem("Windows风格");
    JRadioButtonMenuItem classicItem = new JRadioButtonMenuItem("Windows经典风格");
    JRadioButtonMenuItem motifItem  = new JRadioButtonMenuItem("Motif风格");


    public void  init(){
        var botton = new JPanel();
        botton.add(name);
        botton.add(ok);
        f.add(botton, BorderLayout.SOUTH);

        var checkPannel  = new JPanel();
        checkPannel.add(colorChooser);
        checkPannel.add(male);
        checkPannel.add(female);
        checkPannel.add(married);
        bg.add(male);
        bg.add(female);

        var topLeft = Box.createVerticalBox();
        var taJsp = new JScrollPane(ta);
        topLeft.add(taJsp);
        topLeft.add(checkPannel);

        var top  = Box.createHorizontalBox();

        top.add(topLeft);
        top.add(colorList);
        f.add(top);

        newItem.setAccelerator(KeyStroke.getKeyStroke('N', InputEvent.CTRL_DOWN_MASK));
        newItem.addActionListener(e -> ta.append("用户单击了 新建 菜单"));

        file.add(newItem);
        file.add(saveItem);
        file.add(exitItem);

        edit.add(autoWrap);
        edit.addSeparator();
        edit.add(copyItem);
        edit.add(pasteItem);

        commentItem.setToolTipText("将程序代码注释起来");

        format.add(commentItem);
        format.add(cancelItem);

        edit.add(new JMenuItem("-"));
        edit.add(format);

        mb.add(file);
        mb.add(edit);

        f.setJMenuBar(mb);

        flavorGroup.add(metalItem);
        flavorGroup.add(nimbusItem);
        flavorGroup.add(windowsItem);
        flavorGroup.add(classicItem);
        flavorGroup.add(motifItem);

        pop.add(metalItem);
        pop.add(nimbusItem);
        pop.add(windowsItem);
        pop.add(classicItem);
        pop.add(motifItem);

        ActionListener flavorListener = e -> {
          try {
              switch (e.getActionCommand()){
                  case "Metal风格":
                      changeFlavor(1);
                      break;
                  case "Nimbus风格":
                      changeFlavor(2);
                      break;
                  case "Windows风格":
                      changeFlavor(3);
                      break;
                  case "Windows经典风格":
                      changeFlavor(4);
                      break;
                  case "Motif风格":
                      changeFlavor(5);
                      break;

              }
          } catch (IllegalAccessException ex) {
              ex.printStackTrace();
          } catch (InstantiationException ex) {
              ex.printStackTrace();
          } catch (UnsupportedLookAndFeelException ex) {
              ex.printStackTrace();
          } catch (ClassNotFoundException ex) {
              ex.printStackTrace();
          }
        };

        metalItem.addActionListener(flavorListener);
        nimbusItem.addActionListener(flavorListener);
        windowsItem.addActionListener(flavorListener);
        classicItem.addActionListener(flavorListener);
        motifItem.addActionListener(flavorListener);
        ta.setComponentPopupMenu(pop);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        f.pack();
        f.setVisible(true);
    }

    private void changeFlavor(int flavor) throws ClassNotFoundException, UnsupportedLookAndFeelException, InstantiationException, IllegalAccessException {

        switch (flavor){
            case 1:
                UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
                break;
            case 2:
                UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
                break;
            case 3:
                UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                break;
            case 4:
                UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
                break;
            case 5:
                UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
                break;
        }

        SwingUtilities.updateComponentTreeUI(f.getContentPane());
        SwingUtilities.updateComponentTreeUI(mb);
        SwingUtilities.updateComponentTreeUI(pop);
    }

    public static void main(String[] args) {
        new SwingComponent().init();
    }

}

output:

标签:case,JRadioButtonMenuItem,java,swing,break,add,learning,JMenuItem,new
来源: https://www.cnblogs.com/lianghong881018/p/11289061.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有