0
点赞
收藏
分享

微信扫一扫

《Java 核心技术 卷1》 笔记 第九章 Swing 用户界面组件(8)

进击的包籽 2022-02-26 阅读 72

9.5 菜单

通常对于数据操控方式,除了按钮,选择器,下拉列表的方式,通常还有下来菜单。

9.5.1 菜单创建

为窗体添加菜单:

JMenuBar menuBar = new JMenuBar();

Frame.setJMenuBar(menuBar);

为菜单添加菜单项:

JMenuItem pasteItem = new JMenuItem("Paste");

editMenu.add(pasteItem);

为菜单添加分隔线:

editMenu.addSeparator();

为菜单添加子菜单:

JMenuItem item = editMenu.add("Paste");

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

public class Main {
    public static  void main(String[] args) {
        Main solution = new Main();

        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                MenuFrame frame = new MenuFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        });
    }
}

class MenuFrame extends JFrame implements ActionListener {
    private JTextField text;

    public MenuFrame(){
        setTitle("BorderTest");
        setSize(450,450);

        JMenuBar bar = new JMenuBar();
        JMenu fileMenu = new JMenu("file");
        JMenuItem copyItem = fileMenu.add("copy");
        JMenuItem pasteItem = fileMenu.add("paste");
        JMenuItem cutItem = fileMenu.add("cut");
        JMenu otherMenu = new JMenu("other");
        JMenuItem nothingItem = otherMenu.add("nothing");
        fileMenu.add(otherMenu);
        bar.add(fileMenu);

        copyItem.addActionListener(this);
        pasteItem.addActionListener(this);
        cutItem.addActionListener(this);
        nothingItem.addActionListener(this);

        setJMenuBar(bar);

        text = new JTextField();
        text.setEditable(false);
        add(text,BorderLayout.NORTH);
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        text.setText(((JMenuItem)e.getSource()).getText());

    }
}

效果:选择对应选项后,文本框会设置为当前选择的值

9.5.2 菜单项中的图标

JMenuItem 加图标:

JMenuItem copyItem = new JMenuItem("copy",new ImageIcon("src/resource/p1.png"));

JMenu 加图标:

JMenu fileMenu = new JMenu(new AbstractAction("file",new ImageIcon("src/resource/p4.png")) {

            @Override

            public void actionPerformed(ActionEvent e) {

            }

        });

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

public class Main {
    public static  void main(String[] args) {
        Main solution = new Main();

        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                MenuFrame frame = new MenuFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        });
    }
}

class MenuFrame extends JFrame implements ActionListener {
    private JTextField text;

    public MenuFrame(){
        setTitle("BorderTest");
        setSize(450,450);

        JMenuBar bar = new JMenuBar();
        JMenu fileMenu = new JMenu(new AbstractAction("file",new ImageIcon("src/resource/p4.png")) {
            @Override
            public void actionPerformed(ActionEvent e) {

            }
        });
//        resource/p1.png
        JMenuItem copyItem = new JMenuItem("copy",new ImageIcon("src/resource/p1.png"));
        JMenuItem pasteItem = new JMenuItem("paste",new ImageIcon("src/resource/p2.png"));
        JMenuItem cutItem = new JMenuItem("cut",new ImageIcon("src/resource/p3.png"));
        JMenu otherMenu = new JMenu(new AbstractAction("other",new ImageIcon("src/resource/p6.png")) {
            @Override
            public void actionPerformed(ActionEvent e) {

            }
        });

        JMenuItem nothingItem = new JMenuItem("nothingItem",new ImageIcon("src/resource/p5.png"));
        otherMenu.add(nothingItem);

        fileMenu.add(copyItem);
        fileMenu.add(pasteItem);
        fileMenu.add(cutItem);
        fileMenu.add(otherMenu);
        bar.add(fileMenu);

        copyItem.addActionListener(this);
        pasteItem.addActionListener(this);
        cutItem.addActionListener(this);
        nothingItem.addActionListener(this);

        setJMenuBar(bar);

        text = new JTextField();
        text.setEditable(false);
        add(text,BorderLayout.NORTH);
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        text.setText(((JMenuItem)e.getSource()).getText());

    }
}

效果:

图片来源:https://www.iconfont.cn/collections/detail?spm=a313x.7781069.1998910419.dc64b3430&cid=29753

 相关内容:选择 《Java核心技术 卷1》查找相关笔记

 喜欢的话,点个赞吧~!平时做题,以及笔记内容将更新到公众号。

举报

相关推荐

0 条评论