Java GridBagLayout的问题:TextArea重叠菜单栏

Java GridBagLayout的问题:TextArea重叠菜单栏,java,swing,layout-manager,gridbaglayout,Java,Swing,Layout Manager,Gridbaglayout,你好,我在申请方面遇到了麻烦。包含JTextArea的面板与包含菜单栏的面板重叠。我想自定义我的GUI应用程序,但无法修复重叠。下面是我的代码:我导入了所有需要的API import java.awt.BorderLayout; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.

你好,我在申请方面遇到了麻烦。包含JTextArea的面板与包含菜单栏的面板重叠。我想自定义我的GUI应用程序,但无法修复重叠。下面是我的代码:我导入了所有需要的API

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class ClientPanels extends JPanel{

private JMenu fileM, editM, styleM;
private JTextArea textArea;
private JMenuBar menuBar;
private JScrollPane scpane;
private JPanel panel1;
private JPanel panel2;
private JMenuItem aboutI, openI, saveI, cutI, copyI, pasteI,  exitI;
private GridBagConstraints g;

public ClientPanels() {
    initialize();
}

 private void initialize(){
//initializing and setting up my data
setLayout(new GridBagLayout());

    textArea2 = new JTextArea(40, 50);
    JScrollPane scpane = new JScrollPane(textArea2);
    panel1 = new JPanel(new BorderLayout());
    panel2 = new JPanel(new BorderLayout());

    //menuitems
    openI = new JMenuItem("Open");
    saveI = new JMenuItem("Save");
    exitI = new JMenuItem("Exit");

    copyI = new JMenuItem("Copy");
    pasteI = new JMenuItem("Paste");
    aboutI = new JMenuItem("About");

    fileM = new JMenu("File");
    editM = new JMenu("Edit");
    styleM = new JMenu("Style");

    fileM.add(openI);
    fileM.add(saveI);
    fileM.add(aboutI);
    fileM.add(exitI);

    editM.add(copyI);
    editM.add(pasteI);

    //adding the menu to the menuBars
    menuBar = new JMenuBar();
    menuBar.add(fileM);
    menuBar.add(editM);
    menuBar.add(styleM);

    panel1.add(menuBar,BorderLayout.NORTH);
    panel2.add(scpane, BorderLayout.CENTER);


    //adding the sub-panels to the main panel
    addComp(panel1,0,0,0.5,0.0,0,0,GridBagConstraints.NORTHEAST,GridBagConstraints.HORIZONTAL);
    addComp(panel2,4,1,3,10,0,10, GridBagConstraints.WEST, GridBagConstraints.NONE);

}
//adding components to panel using GridBagLayout as the layout manager
 private void addComp(JPanel panel, int Xpos, int Ypos, double wx,double wy,
                    int compWidth, int compHeight, int place, int stretch)
 {
    GridBagConstraints g = new GridBagConstraints();
    g.gridx = Xpos;
    g.gridy = Ypos;
    g.weightx = wx;
    g.weighty= wy;
    g.gridwidth = compWidth;
    g.gridheight = compHeight;
    g.insets = new Insets(5,5,5,5);
    g.anchor = place;
    g.fill = stretch;

    add(panel, g);
 }

}
主要方法:

import javax.swing.SwingUtilities;


 public class TestJavaClient {
    private ClientPanels panels;
     public TestJavaClient()
  { 
      JFrame frame = new JFrame();
      panels = new ClientPanels();

     frame.setSize(800,590);
     frame.setLocationRelativeTo(null);
     frame.add(panels);
     frame.pack();
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.setVisible(true);

  }

public static void main(String[] args) {


    SwingUtilities.invokeLater(new Runnable(){
        public void run()
        {
            new TestJavaClient();
        }
    });

  }
}
更改:

panel1.add(menuBar,BorderLayout.NORTH);
例如(您需要重构代码):


但正如在评论中提到的,MCVE会得到更好的答案。

通读一遍,理解它的意思,即最小、完整(且可兼容)…@MadProgrammer我为阅读时间过长而道歉。我重新编辑了我的代码。@java程序员显示菜单栏ok。但当我添加JTextArea时,它与菜单栏重叠。基本上写在菜单栏上并隐藏菜单项仍然无法编译…不要弄乱
gridwidth
/
gridheight
值,除非你知道它们实际上是做什么的。因为我是从我的类扩展的,是从JPanel扩展的。我不得不添加到面板1,最后添加到主面板。在我的测试课上,它被添加到了框架中。为什么仍然没有MCVE?MCVE需要是一个源文件-将
ClientPanels
从public降级为默认访问,并使用
main(String[])
将其粘贴到源文件的末尾。然后合并进口。顺便说一句-我真的看不出有什么理由在这里扩展
JPanel
。我在学习java swing并练习只是扩展JPanel“练习只是扩展JPanel”,我建议练习不要扩展面板,因为这是我们在不更改或添加功能时应该做的事情(即99.9%的时间)。
frame.setJMenuBar(menuBar);