I'm trying to get the JTabbedPane
automatically expanding over the parent JPanel
.
When I put everything in the Main class, it works:
Main:
public class Main extends JFrame {
public Main() {
JTabbedPane tpane = new JTabbedPane();
JPanel panel = new JPanel();
panel.add(new JButton("Button 1"));
tpane.addTab("Tab1", panel);
JPanel panel2 = new JPanel();
panel2.add(new JButton("Button 2"));
tpane.addTab("Tab2", panel2);
this.setSize(500, 500);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(tpane);
this.setVisible(true);
}
public static void main(String[] args) {
Main m = new Main();
}
}
But when I put it into another class, it won't work anymore:
Main:
public class Main extends JFrame {
View view = new View();
public Main() {
this.setSize(500, 500);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(view, BorderLayout.CENTER); // BorderLayout
this.setVisible(true);
}
public static void main(String[] args) {
Main m = new Main();
}
}
View:
public class View extends JPanel {
public View() {
JTabbedPane tpane = new JTabbedPane();
JPanel panel = new JPanel();
panel.add(new JButton("Button 1"));
tpane.addTab("Tab1", panel);
JPanel panel2 = new JPanel();
panel2.add(new JButton("Button 2"));
tpane.addTab("Tab2", panel2);
this.add(tpane, BorderLayout.CENTER); // BorderLayout
}
}