如何删除JMenu项目

时间:2016-01-30 10:32:34

标签: java swing user-interface jpanel jmenuitem

截至目前,我有这个

这是我MyFrame1的源代码:

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Color;
import java.awt.Color.*;
import java.awt.Font;
import java.awt.Font.*;
import java.io.*;
import java.io.BufferedReader;
import java.io.FileReader;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

public class Test
{
    public static void main(String[] args)
    {
        new Test();
    }

    public Test()
    {
        String line = "";

        EventQueue.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                try
                {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } 
                catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e)
                {
                    e.printStackTrace();
                }

                JMenuBar mBar = new JMenuBar();

                //creating new JMenuItem
                JMenuItem mHelp = new JMenuItem("Help");
                JMenuItem mCredits = new JMenuItem("Credits");
                JMenuItem mExit = new JMenuItem("Exit");

                /*try
                {
                    BufferedReader br = new BufferedReader(new FileReader("1.txt"));
                    line  = br.readLine();

                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }*/
                JLabel jUser = new JLabel("User is: " );

                mHelp.setOpaque(false);
                mHelp.setForeground(Color.DARK_GRAY);
                mHelp.setFont(new Font("Verdana", Font.PLAIN,12));
                mCredits.setOpaque(false);
                mCredits.setForeground(Color.DARK_GRAY);
                mCredits.setFont(new Font("Verdana", Font.PLAIN,12));
                mExit.setOpaque(false);
                mExit.setForeground(Color.DARK_GRAY);
                mExit.setFont(new Font("Verdana", Font.PLAIN,12));

                mBar.add(mHelp);
                mBar.add(mCredits);
                mBar.add(mExit);
                mBar.add(jUser);
                //mBar.add(line);

                JFrame frame = new JFrame("MYFRAME");
                frame.setJMenuBar(mBar);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
                frame.setResizable(false);
            }
        });
    }

    public class TestPane extends JPanel
    {
        public TestPane()
        {
            setBorder(new EmptyBorder(20, 20, 20, 20));
            setLayout(new GridLayout(3, 3, 60, 60));
            add(makeButton("Account Code"));
            add(makeButton("Unit Details"));
            add(makeButton("Item Details"));
            add(makeButton("Clearing"));
            add(makeButton("Search"));
            add(makeButton("Exit"));
        }

        protected JButton makeButton(String text)
        {
            JButton btn = new JButton(text);
            btn.setFont(new Font("Verdana", Font.PLAIN,18));
            btn.setMargin(new Insets(30, 30, 30, 30));
            btn.setBackground(Color.blue);
            btn.setOpaque(true);
            btn.setBorderPainted(false);
            return btn;
        }
    }
}

我还是新手,对Java和GUI仍然知之甚少。我还在学习它,所以我在我的程序上做了试错。  我尝试使用UIManager或UILayout,但仍然没有为我工作或我仍然不知道如何使用它。 我真的想了解更多关于GUI和Java的信息,请帮助我。任何评论,评论,建议都被接受和赞赏。

MyFrame1:

MyFrame1

至于我的目标是这种输出,请。见下图。

MyDesireOutput:

MyDesireOutput

另外如果您注意到有一个bufferedReader,我正在练习读取带有String的“1.txt”,并将其作为标签或(仍然不知道)在菜单栏中...

2 个答案:

答案 0 :(得分:3)

首先你必须知道这些

JMenuBar:

  

菜单栏的实现。您将JMenu对象添加到菜单栏   构建菜单。

JMenu:

  

菜单的实现 - 包含JMenuItems的弹出窗口   当用户选择JMenuBar上的项目时显示。

JMenuItem:

  

菜单中项目的实现。

然后将JMenuItem添加到JMenu,稍后将此JMenu添加到JMenuBar

//creating a menu `Options`
JMenu menu = new JMenu("Options");

//creating menu items 
JMenuItem mHelp = new JMenuItem("Help");
JMenuItem mCredits = new JMenuItem("Credits");
JMenuItem mExit = new JMenuItem("Exit");

//adding all menu items to menu
menu.add(mHelp);
menu.add(mCredits);
menu.add(mExit);

//adding menu to menu bar               
mBar.add(menu);

//aligning label to right corner of window                
mBar.add(Box.createHorizontalGlue());
mBar.add(jUser);//label

<强>输出:

enter image description here

答案 1 :(得分:2)

您应该将JMenuItem添加到JMenu个对象,然后将JMenu添加到JMenuBar

JMenuBar mBar = new JMenuBar();

            //creating new JMenuItem
            JMenuItem mHelp = new JMenuItem("Help");
            JMenu help = new JMenu("Help");
            help.add(mHelp);

            JMenuItem mCredits = new JMenuItem("Credits");
            JMenu credits = new JMenu("Credits");
            credits.add(mCredits);

            JMenuItem mExit = new JMenuItem("Exit");
            JMenu exit = new JMenu("Exit");
            exit.add(exit);
            /*try
            {
                BufferedReader br = new BufferedReader(new FileReader("1.txt"));
                line  = br.readLine();

            }
            catch(Exception e)
            {
                e.printStackTrace();
            }*/
            JLabel jUser = new JLabel("User is: " );

            mHelp.setOpaque(false);
            mHelp.setForeground(Color.DARK_GRAY);
            mHelp.setFont(new Font("Verdana", Font.PLAIN,12));
            mCredits.setOpaque(false);
            mCredits.setForeground(Color.DARK_GRAY);
            mCredits.setFont(new Font("Verdana", Font.PLAIN,12));
            mExit.setOpaque(false);
            mExit.setForeground(Color.DARK_GRAY);
            mExit.setFont(new Font("Verdana", Font.PLAIN,12));

            mBar.add(help);
            mBar.add(credits);
            mBar.add(exit);

但是向JLabel添加JMenuBar并不是一个好主意。如果您希望在问题中描述类似于您的内容,则可能需要将JPanel添加到框架的north区域,然后将用户标签添加到FlowLayout.TRAILING区域那个小组:

mBar.add(help);
            mBar.add(credits);
            mBar.add(exit);
            //mBar.add(jUser);
            //mBar.add(line);

            JPanel statusPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING));
            statusPanel.add(jUser);
            statusPanel.add(new JLabel("Loen Seto"));

            JFrame frame = new JFrame("MYFRAME");
            frame.setJMenuBar(mBar);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(statusPanel, BorderLayout.NORTH);
            frame.add(new TestPane(), BorderLayout.CENTER);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
            frame.setResizable(false);

祝你好运