Java 为什么我';我在jmenubar旁边有个白色区域?

Java 为什么我';我在jmenubar旁边有个白色区域?,java,swing,jpanel,jmenuitem,jmenubar,Java,Swing,Jpanel,Jmenuitem,Jmenubar,我试图创建一个对话框,其中包含两个splitPanel和一个菜单栏。两个splitPanel都包含jlist 但是当我运行代码时,菜单栏旁边会出现一个白色区域。在下面的图片中,组织旁边会创建一个白色区域 以下是我的代码部分: public class HistList extends JPanel { JTable table = new JTable(); JMenuItem remove = new JMenuItem("Remove"); JScrollPane scrollPa

我试图创建一个对话框,其中包含两个splitPanel和一个菜单栏。两个splitPanel都包含jlist

但是当我运行代码时,菜单栏旁边会出现一个白色区域。在下面的图片中,组织旁边会创建一个白色区域

以下是我的代码部分:

   public class HistList extends JPanel {
JTable table = new JTable();
 JMenuItem remove = new JMenuItem("Remove");
JScrollPane scrollPane = new JScrollPane();
MyTableModel model = new MyTableModel();

HistList() {
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
        ex.printStackTrace();
    }
    String[] title = {" ", "Time", "Name", "Location"};
    Object[][] data = {
        {false, "01:22:16", "Google", "http://www.google.com"},
        {false, "01:22:16", "Yahoo - login", "https://login.yahoo.com/?.src=ym&.intl=us&.lang=en-US&.done=https%3a//mail.yahoo.com"},
        {false, "01:22:29", "Oracle | Integrated Cloud Applications and Platform Services", "https://www.oracle.com/index.html"}
    };
    for (int i = 0; i < data.length; i++) {
        model.addRow(data[i]);
    }
    this.table = new JTable(model);
    this.table.setShowGrid(false);
    this.scrollPane = new JScrollPane(this.table);
    Dimension size = new Dimension(510, 380);
    this.scrollPane.setPreferredSize(size);
    this.scrollPane.getViewport().setBackground(Color.WHITE);
    JTextField name = new JTextField();
    name.setPreferredSize(new Dimension(scrollPane.getWidth(), 25));
    JTextField locationText = new JTextField();
    locationText.setPreferredSize(new Dimension(scrollPane.getWidth(), 25));
    JPanel textPane = new JPanel();
    JLabel nameLabel = new JLabel("Name:");
    JPanel textPane1 = new JPanel();
    textPane1.setLayout(new BoxLayout(textPane1, BoxLayout.X_AXIS));
    textPane1.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
    textPane1.add(Box.createHorizontalGlue());
    textPane1.add(nameLabel);
    textPane1.add(Box.createRigidArea(new Dimension(21, 0)));
    textPane1.add(name);
    JLabel locationLabel = new JLabel("Location:");
    JPanel textPane2 = new JPanel();
    textPane2.setLayout(new BoxLayout(textPane2, BoxLayout.X_AXIS));
    textPane2.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
    textPane2.add(Box.createHorizontalGlue());
    textPane2.add(locationLabel);
    textPane2.add(Box.createRigidArea(new Dimension(5, 0)));
    textPane2.add(locationText);
    textPane.setLayout(new BoxLayout(textPane, BoxLayout.Y_AXIS));
    textPane.setBorder(BorderFactory.createEmptyBorder(0, 2, 2, 2));
    textPane.add(Box.createVerticalGlue());
    textPane.add(textPane1);
    textPane.add(Box.createRigidArea(new Dimension(0, 5)));
    textPane.add(textPane2);
    JPanel contentPane = new JPanel();
    contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
    contentPane.add(scrollPane);
    contentPane.setBorder(BorderFactory.createEmptyBorder(0, 2, 2, 2));
    contentPane.add(Box.createVerticalGlue());
    contentPane.add(Box.createRigidArea(new Dimension(0, 5)));
    contentPane.add(textPane);
    JPanel pane = new JPanel(new BorderLayout());
    pane.setBorder(BorderFactory.createEmptyBorder(15, 0, 0, 0));
    pane.setBackground(Color.WHITE);
    String[] dates = {"12/15/2016", "12/30/2016"};
    JList list;
    list = new JList(dates);
    list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
    JLabel label = new JLabel("Browsing Date");
    pane.add(label, BorderLayout.NORTH);
    pane.add(list, BorderLayout.WEST);
    pane.setPreferredSize(new Dimension(150, 380));
    JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, pane, contentPane);
    JMenuBar organize = new JMenuBar();
    organize.setOpaque(true);
    organize.setBackground(getBackground());
    JMenu menu = new JMenu("Organize");
    menu.add(remove);
    remove.setEnabled(false);
    menu.setOpaque(true);
    menu.setBackground(getBackground());
    menu.setIcon(new ImageIcon(Hello.class.getResource("/rsz_todo-512.png")));
    organize.add(menu);
    JPanel finale = new JPanel(new BorderLayout());
    finale.setOpaque(true);
    finale.add(organize,BorderLayout.NORTH);
    finale.add(splitPane,BorderLayout.CENTER);
    setOpaque(true);
    add(finale);
}
public类HistList扩展了JPanel{
JTable table=新的JTable();
JMenuItem remove=新的JMenuItem(“remove”);
JScrollPane scrollPane=新的JScrollPane();
MyTableModel模型=新的MyTableModel();
历史清单(){
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(ClassNotFoundException |实例化Exception | IllegalacessException |不支持ookandfeelException ex){
例如printStackTrace();
}
字符串[]标题={“”,“时间”,“名称”,“位置”};
对象[][]数据={
{假,“01:22:16”,“谷歌”http://www.google.com"},
{false,“01:22:16”,“雅虎登录”https://login.yahoo.com/?.src=ym&.intl=us&.lang=en-US&.done=https%3a//mail.yahoo.com“},
{false,“01:22:29”,“Oracle |集成云应用程序和平台服务”,”https://www.oracle.com/index.html"}
};
对于(int i=0;i
我不知道是什么造成了这个白色区域。我在这一部分是新手。有人能帮我修复它吗?请原谅我的愚蠢问题。提前谢谢

但当我运行代码时,菜单栏旁边会出现白色区域

不要将菜单栏添加到框架的内容窗格中

JFrame为菜单栏保留了一个特殊的位置

  • 阅读JFrame API以找到合适的方法

  • 阅读Swing教程中有关如何使用菜单的部分,了解更多信息和工作示例

  • 注意Swing教程中的工作示例将帮助您更好地构造代码,以便在
    事件调度线程(EDT)
    上创建GUI组件

    虽然示例中的代码太多,但代码的形式也更多地是
    MCVE
    。因为您的问题是关于菜单栏的,所以您只需要JFrame、JMenuBar和JMenu来演示您所述的问题


    为所有Swing基础知识的示例保留一个指向教程的链接。

    1)为了更快地获得更好的帮助,发布一个or。2)
    结尾。添加(organize,BorderLayout.NORTH);
    为什么不使用
    setJMenuBar(…)
    ?3)所有的
    挫折背景(…)
    调用?这应该由PLAF来处理。感谢您的建议,我现在发布了我的完整代码“完整代码”,我从来没有这样建议过,而且在任何情况下都不是完整代码(缺少导入&放置窗格的主要方法)
    finale.add(organize,BorderLayout.NORTH);