我想让我的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();
}
}
答案 0 :(得分:1)
为您的tabsPanel提供合适的布局,即BorderLayout:
JPanel tabsPanel = new JPanel(new BorderLayout()); //!!
注意,您不应该向GUI添加两次组件:
frame.add(buttonPanel);
frame.add(buttonPanel, BorderLayout.NORTH);
并且不需要多次调用pack()
另外,您不应该调用setSize(...)
,而应该更好地覆盖关键组件的getPreferredSize()
。