0

So for the sake of keeping it simple, lets say I want to have 3 separate programs all display in a JTabbedPane. Suppose they are all Hello World simple file in a JFrame.

Lets use this simple file and create HelloWorld1.java, HelloWorld2.java and HelloWorld3.java. They exist separately this way. BUT say I want to combine them into ONE JTabbedPane - still keeping them separate. I am guessing I would need to create a project (lets say in Eclipse) and set up a JTabbedPane in a forth file and add these? Can someone give me a demonstration of this so I can understand clearer how to go about this. Appreciate any help. Just really interested in what the main (forth) file might look like.

4

2 回答 2

4
public static void main(String[] args) {
    JFrame child = new HelloWorld1("child1");
    child.add(new JLabel("Child1"));

    JTabbedPane jTabbedPane = new JTabbedPane();
    jabbedPane.addTab("Child", child.getContentPane());

    child = new HelloWorld2("child2");
    jabbedPane.addTab("Child2", child.getContentPane());
    //and so on with other applications/frames

    JFrame main = new JFrame("Main");
    main.setSize(600,600);
    main.add(jTabbedPane);      
    main.setVisible(true);
}
于 2012-11-22T18:09:29.280 回答
2

解决方法很简单:

1)而不是说public class HelloWorld1 extends JFrame{....}使用public class HelloWorld1 extends JPanel{...}

2) 在主类 ( 和 ) 中实例化您的类HellowWorld1并将HellowWorld2它们HellowWorld3添加到JTabbedPane如下所示:

HelloWorld1 hw = new HelloWorld1();
....
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.add("Title of first panel",hw);
于 2012-11-22T18:19:24.817 回答