Java 同时使用JTabbedPane和JScrollPane

Java 同时使用JTabbedPane和JScrollPane,java,swing,layout,jtextarea,Java,Swing,Layout,Jtextarea,正如您看到的代码一样,我想实现第二个选项卡,其中包含一个文本区域、滚动和一个按钮。这个(JScrollPane scr=newjscrollpane(secondTab(panel2));)代码以前在函数中,private static JTextArea secondTab(JPanel panel),但我将它从函数中取出,并将其放回大型机。因为滚动和文本区域没有显示。现在,我将代码移动到大型机上,tex tarea和scroll都是可见的,但是我正在努力使按钮显示在第二个选项卡上。你们知道吗

正如您看到的代码一样,我想实现第二个选项卡,其中包含一个文本区域、滚动和一个按钮。这个(
JScrollPane scr=newjscrollpane(secondTab(panel2));
)代码以前在函数中,
private static JTextArea secondTab(JPanel panel)
,但我将它从函数中取出,并将其放回大型机。因为滚动和文本区域没有显示。现在,我将代码移动到大型机上,tex tarea和scroll都是可见的,但是我正在努力使按钮显示在第二个选项卡上。你们知道吗

public MainFrame(String username)
{

    JTabbedPane tab = new JTabbedPane();
    mainFrame.add(tab, BorderLayout.CENTER);

    JPanel panel1 = new JPanel();
    firstTab(panel1);
    tab.add("babababa", panel1);


    JPanel panel2 = new JPanel();
    JScrollPane scr = new JScrollPane(secondTab(panel2));
    JButton saveButton = new JButton("Save");
    panel2.add(saveButton);
    saveButton.setBounds(190, 280, 80, 40);

    tab.add("hahaha", panel2.add(scr));

    mainFrame.setBounds(200,200,500,400);
    mainFrame.setVisible(true);
}

private static JTextArea secondTab(JPanel panel) {
    panel.setLayout(null);

    final JTextArea nameTextArea=new JTextArea();
    nameTextArea.setBounds(10,10,440,270);
    nameTextArea.setLineWrap(true);

    return nameTextArea;

}

}

您应该避免使用空布局,因为这会导致非常不灵活的GUI,虽然它们在一个平台上看起来不错,但在大多数其他平台或屏幕分辨率上看起来很糟糕,并且很难更新和维护。不仅如此,当视口视图(显示在JScrollPane内部的组件)使用空布局时,JScrollPanes无法正常工作。不仅如此,如果您设置了JTextArea的边界甚至是首选大小,它将不会在JScrollPane中展开,也不会看到滚动条

解决方案:

  • 避免像瘟疫一样使用空布局
  • 学习并使用布局管理器:
  • 切勿设置JTextArea的大小或preferredSize。而是考虑设置它的列和行。
  • 例如:

    import java.awt.BorderLayout;
    import javax.swing.*;
    
    public class MainFrame2 extends JPanel {
    
       private JTabbedPane tabbedPane = new JTabbedPane();
       private JTextArea textArea = new JTextArea(20, 40);
    
       public MainFrame2() {
          tabbedPane.add("Bahahahaha", new JPanel());
          tabbedPane.add("TextArea Info", createTextAreaPane());
    
          setLayout(new BorderLayout());
          add(tabbedPane, BorderLayout.CENTER);
       }
    
       private JComponent createTextAreaPane() {
          JPanel btnPanel = new JPanel();
          btnPanel.add(new JButton("Save"));
    
          JPanel textAreaPane = new JPanel(new BorderLayout());
          textAreaPane.add(new JScrollPane(textArea), BorderLayout.CENTER);
          textAreaPane.add(btnPanel, BorderLayout.SOUTH);
          return textAreaPane;
       }
    
       private static void createAndShowGui() {
          MainFrame2 mainPanel = new MainFrame2();
    
          JFrame frame = new JFrame("Main Frame");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(mainPanel);
          frame.pack();
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    

    最好在每个方法中构建每个选项卡面板

    private JPanel buildFirstTab() {
    // create new panel
    // add components to the panel
    // return the panel
    }
    
     private JPanel buildSecondTab() {
    // create new panel
    // create JScrollPane
    // create JTextArea
    // add text area to scrollpane by calling scrollpane.setViewPort method
    // add scrollpane to the panel
    // create button
    // add button to the panel
    // return the panel
    }
    
    
    public MainFrame(String username) {
        JTabbedPane tab = new JTabbedPane();
        mainFrame.add(tab, BorderLayout.CENTER);
        tab.add("1st tab", buildFirstTab());
        tab.add("2nd tab", buildSecondTab());
        mainFrame.setBounds(200,200,500,400);
        mainFrame.setVisible(true);
    }
    

    在前面的答案的基础上,尝试使用简单的布局,如BoxLayout(将组件添加到彼此的顶部)或FlowLayout(将组件添加到上一个组件的旁边),然后学习更复杂的布局,如GridBagLayout(到目前为止,大多数情况下,这对我来说是最好的布局)。

    那么,为什么要删除上一个问题()在我给你答案之后。在我花时间给你指出正确的方向之后,你不应该接受这个答案吗?