1

我正在努力使我的 JPanel 更大。我有三个选项卡,但是当我运行程序时,并非所有选项卡都显示(除非我单击箭头)。我只需要知道如何使面板更大,这样我就不必单击箭头

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;


public class TestApplication {
    public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setSize(1000, 1000);
    frame.setTitle("RBA Test Application");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

    JButton initialize = new JButton("Initialize");
    JButton connect = new JButton("Connect");
    JButton disconnect = new JButton("Disconnect");
    JButton shutdown = new JButton("Shut Down");
    JButton portsettings = new JButton("Port Settings");


    JPanel buttonPanel = new JPanel();
    buttonPanel.add(initialize);
    buttonPanel.add(connect);
    buttonPanel.add(disconnect);
    buttonPanel.add(shutdown);
    buttonPanel.add(portsettings);
    frame.add(buttonPanel);
    frame.add(buttonPanel, BorderLayout.NORTH);

    frame.pack();

    JTabbedPane tabbedPane = new JTabbedPane();
    tabbedPane.addTab("LLP", null, null, "Low Level Protocol");
    tabbedPane.addTab("POS",null, null, "Point Of Sale");
    tabbedPane.addTab("Test", null, null, "Test");

    JPanel tabsPanel = new JPanel();
    tabsPanel.add(tabbedPane);
    frame.add(tabsPanel);
    tabsPanel.setPreferredSize(new Dimension(900, 900));
    frame.add(tabsPanel, BorderLayout.CENTER);

    frame.pack();



    }






}
4

1 回答 1

1

给你的 tabsPanel 一个像样的布局,即 BorderLayout:

  JPanel tabsPanel = new JPanel(new BorderLayout()); //!!

请注意,您不应该向GUI中添加两次组件:

  frame.add(buttonPanel);
  frame.add(buttonPanel, BorderLayout.NORTH);

而且不需要多次调用 pack() 。
此外,您不应该调用setSize(...)而是更好地覆盖getPreferredSize()关键组件。

于 2013-05-21T18:39:51.457 回答