当我想在我的 jpanel 中添加选项卡时,我遇到了一个简单的问题。选项卡的对齐方式是水平而不是垂直,看起来像废话 =/。
它看起来像这样:
如果我放弃面板并将 tabbedPane 直接添加到框架中,一切正常。如果您取消注释三行代码并删除,getContentPane().add(jtp);
您可以重现我的问题。
工作代码:
public class TabbedPane extends JFrame
{
public TabbedPane()
{
setTitle("Tabbed Pane");
setSize(300, 300); // set size so the user can "see" it
JTabbedPane jtp = new JTabbedPane();
// JPanel panel = new JPanel();//uncomment all three lines
// panel.add(jtp);
// getContentPane().add(panel);
getContentPane().add(jtp);//remove me
JPanel jp1 = new JPanel();// This will create the first tab
JPanel jp2 = new JPanel();// This will create the second tab
JLabel label1 = new JLabel();
label1.setText("This is Tab 1");
jp1.add(label1);
jtp.addTab("Tab1", jp1);
jtp.addTab("Tab2", jp2);
JButton test = new JButton("Press");
jp2.add(test);
setVisible(true); // otherwise you won't "see" it
}
public static void main(String[] args)
{
TabbedPane tab = new TabbedPane();
}
}
非常感谢!