我正在努力让我的 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();
}
}
给你的 tabsPanel 一个合适的布局,即 BorderLayout:
JPanel tabsPanel = new JPanel(new BorderLayout()); //!!
请注意,您不应将组件两次添加到 GUI:
frame.add(buttonPanel);
frame.add(buttonPanel, BorderLayout.NORTH);
并且无需多次调用 pack()。
另外,您不应该调用 setSize(...)
,而最好重写关键组件的 getPreferredSize()
。
我是一名优秀的程序员,十分优秀!