繁体   English   中英

按钮后如何使JMenuBar带消失?

[英]How can I make the JMenuBar strape dissapear after the buttons?

在菜单项之后,如何使JMenuBar条纹消失? 我想在框架的中间放置第二个菜单,这种条纹看起来很奇怪。

我试图将背景设置为Panel.background,但是它不起作用。

import java.awt.*;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.UIManager;

public class SwingMenuDemo extends JPanel {

   public SwingMenuDemo(){
        setLayout(null);

        JMenuBar menuBar = new JMenuBar();
        menuBar.setBackground(UIManager.getColor("Panel.background"));
        menuBar.setBounds(0, 0, 450, 25);

        add(menuBar);

        JButton btn1 = new JButton("Menu1");
        menuBar.add(btn1);

        JButton btn2 = new JButton("Menu2");
        menuBar.add(btn2);

        JButton btn3 = new JButton("Menu3");
        menuBar.add(btn3);
   }

   private static void createAndShowGUI() {
       JFrame frame = new JFrame("SwingMenuDemo");

       frame.setPreferredSize(new Dimension(400,400));
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(),BoxLayout.PAGE_AXIS));
       frame.getContentPane().add(new SwingMenuDemo());
       frame.pack();
       frame.setVisible(true);
   }

   public static void main(String[] args) {
       javax.swing.SwingUtilities.invokeLater(new Runnable() {
           public void run() {
               createAndShowGUI();
           }
       });
   }

}

在此处输入图片说明

如果要创建几个JButton的“菜单”,请不要使用JMenu,也不要使用null布局和setBounds(...) (通常应避免使用后者)。 而是嵌套JPanels,每个JPanels使用其自己的布局管理器以允许简单地创建复杂的GUI。

例如,您可以创建一个JPanel来保存按钮,即所谓的menuPanel,为其提供new GridLayout(1, 0)布局,这意味着它将在1行中包含一个组件网格,并且在列数上是可变的(这就是0表示)。 然后把你的按钮。

然后将该JPanel放入另一个使用例如FlowLayout.LEADING, 0, 0)作为其布局的JPanel中-它会将其所有组件推到左侧。

然后,使主GUI使用BorderLayout,并将上面的面板添加到BorderLayout.PAGE_START位置的顶部。 例如:

在此处输入图片说明

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.*;

@SuppressWarnings("serial")
public class SwingMenuDemo2 extends JPanel {
    private static final String[] MENU_TEXTS = {"Menu 1", "Menu 2", "Menu 3"};
    private static final int PREF_W = 400;
    private static final int PREF_H = PREF_W;

    public SwingMenuDemo2() {
        JPanel menuPanel = new JPanel(new GridLayout(1, 0));
        for (String menuText : MENU_TEXTS) {
            menuPanel.add(new JButton(menuText));
        }

        JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.LEADING, 0, 0));
        topPanel.add(menuPanel);

        setLayout(new BorderLayout());
        add(topPanel, BorderLayout.PAGE_START);
    }

    @Override
    public Dimension getPreferredSize() {
        if (isPreferredSizeSet()) {
            return super.getPreferredSize();
        }
        return new Dimension(PREF_W, PREF_H);
    }

    private static void createAndShowGui() {
        SwingMenuDemo2 mainPanel = new SwingMenuDemo2();

        JFrame frame = new JFrame("Swing Menu Demo 2");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

你可以这样做

menuBar.setBorderPainted(false);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM