How to remove the space in JMenu Items

2019-08-11 09:26发布

As of now, I have this

And this is my source code for 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;
        }
    }
}

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:

MyFrame1

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

MyDesireOutput:

MyDesireOutput

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...

2条回答
唯我独甜
2楼-- · 2019-08-11 09:56

You should add your JMenuItems to JMenu objects and then add your JMenus to your 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);

But adding a JLabel to JMenuBar is not a good idea. If you want to have something like you depicted in you question, you may want to add a JPanel to the north region of your frame, and then add the User label to the FlowLayout.TRAILING region of that panel:

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

Good Luck

查看更多
孤傲高冷的网名
3楼-- · 2019-08-11 10:00

First you must know these

JMenuBar:

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

JMenu:

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.


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

Output:

enter image description here

查看更多
登录 后发表回答