2016-01-30 62 views
1

截至目前,我有这个如何删除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。我仍然在学习它,所以我正在对我的程序进行试错。 我尝试过使用UIManager或UILayout,但仍然不适合我,或者我仍然不知道如何使用它。 我真的想了解更多关于GUI和Java的知识,请帮助我。任何评论,评论,建议都会被接受和赞赏。

MyFrame1:

MyFrame1

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

MyDesireOutput:

MyDesireOutput

此外,如果你发现有一个BufferedReader,我在练阅读“的1.txt”用字符串,并把它作为标签或(仍然不知道这件事)在菜单栏中...

+0

[如何使用菜单](http://docs.oracle.com/javase/tutorial/uiswing/components/ menu.html) – MadProgrammer

回答

2

您应该将您的JMenuItem s添加到JMenu对象,然后将您的JMenu s添加到您的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); 

但添加JLabelJMenuBar是不是一个好主意。如果你想拥有像你描述你的问题,你可能要一个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); 

好运气

+0

太好了,谢谢。我怎样才能将JPanel做到我的框架的北部地区,然后在该面板的东部添加一个jlabel? 你能告诉我如何? –

3

首先你必须知道这些

的JMenuBar:

的implementa一个菜单栏。您将JMenu对象添加到菜单栏 以构建菜单。

JMenu的:

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

的JMenuItem:

在菜单中的项目的实施。


所以添加JMenuItem s到JMenu,随后将这项JMenuJMenuBar



输出:

enter image description here