对于Java家庭作业额外的学分,我试图在一个JTabbedPane中添加几个面板。实际上,它们是JFrames,但我只是更改为扩展JPanel而不是JFrame,并删除了main()。无论如何,当我运行main()时,JTabbedPane和两个面板都会出现,但是是分开的。我遗漏了什么?
import javax.swing.*;
public class TabbedPane extends JFrame
{
JPanel DayGui = new JPanel();
JPanel OfficeAreaCalculator = new JPanel();
JLabel firstLabel = new JLabel("First tabbed pane");
JLabel secondLabel = new JLabel("Second tabbed pane");
JTabbedPane tabbedPane = new JTabbedPane();
// constructor
public TabbedPane()
{
DayGui.add(firstLabel);
OfficeAreaCalculator.add(secondLabel);
tabbedPane.add("First Panel", DayGui);
tabbedPane.add("Second Panel", OfficeAreaCalculator);
add(tabbedPane);
}
public static void main(String[] args)
{
TabbedPane tab = new TabbedPane();
tab.pack();
tab.setVisible(true);
JTabbedPane DayGui = new JTabbedPane();
JTabbedPane OfficeAreaCalculator = new JTabbedPane();
DayGui dg = new DayGui();
OfficeAreaCalculator oac = new OfficeAreaCalculator();
}
}发布于 2013-09-29 22:32:32
您正在创建两对变量,用于引用要添加到JFrame中的组件,但添加指向空白JPanels的变量。研究基于官方教程的代码
public class TabbedPaneApp {
private static void createAndShowGUI() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTabbedPane firstPanel = new JTabbedPane();
JTabbedPane secondPanel = new JTabbedPane();
JLabel firstLabel = new JLabel("First tabbed pane");
JLabel secondLabel = new JLabel("Second tabbed pane");
JTabbedPane tabbedPane = new JTabbedPane() {
public Dimension getPreferredSize() {
return new Dimension(500, 400);
};
};
firstPanel.add(firstLabel);
secondPanel.add(secondLabel);
tabbedPane.add("First Panel", firstPanel);
tabbedPane.add("Second Panel", secondPanel);
frame.add(tabbedPane);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}https://stackoverflow.com/questions/19083958
复制相似问题