如何删除JMenu项目中的空间 [英] How to remove the space in JMenu Items

查看:109
本文介绍了如何删除JMenu项目中的空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我有这个

这是我的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仍然了解不多.我仍在学习有关内容,因此我正在对程序进行Trial-Error. 我尝试使用UIManager或UILayout,但仍然对我不起作用,或者我仍然不知道如何使用它. 我真的很想了解有关GUI和Java的更多信息,请帮助我.任何意见,评论,建议均被接受并受到赞赏.

I am still new and still have a small knowledge about Java and GUI. I am still learning about it so I am doing Trial-Error on my program. I tried using UIManager, or UILayout, but still not working for me or I still dont know how to use it. I really want to learn more about GUI and Java, please help me. Any comments, remarks, suggestions are accepted and well-appreciated.

MyFrame1:

至于我要针对这种输出,请.见下一张图片.

As for the output I am aiming for this kind, pls. see next picture.

MyDesireOutput:

MyDesireOutput:

另外,如果您发现有bufferedReader,我正在练习读取带有字符串的"1.txt",并将其作为标签或(仍然不知道)放在菜单栏中...

Also if you notice there's a bufferedReader, I am practicing to read a "1.txt" with a String, and putting it as label or (still dont know about it) in the menu bar...

推荐答案

首先,您必须了解这些内容

First you must know these

JMenuBar:

菜单栏的实现.您将JMenu对象添加到菜单栏 构造菜单.

An implementation of a menu bar. You add JMenu objects to the menu bar to construct a menu.

JMenu:

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

An implementation of a menu -- a popup window containing JMenuItems that is displayed when the user selects an item on the JMenuBar.

JMenuItem:

菜单中项目的实现.

An implementation of an item in a menu.


因此,将您的JMenuItem添加到JMenu,然后将此JMenu添加到JMenuBar.


So add your JMenuItems to JMenu, later add this JMenu to 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


输出:

这篇关于如何删除JMenu项目中的空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆