Add JTabbedPane with buttons, labels ... in a frame with an absolute layout

4.8k views Asked by At

I have a code in java.

package interfaces;

import javax.swing.JPanel;

public class TabbedPaneDemo extends JPanel {
public TabbedPaneDemo() {
    JTabbedPane pane = new JTabbedPane();

    JPanel dashboardPanel = new JPanel();
    dashboardPanel.add(new JLabel("Dashboard"));
    // Add Dashboard Tab
    pane.addTab("Dashboard", dashboardPanel);

    JPanel transactionPanel = new JPanel();
    transactionPanel.add(new JLabel("Transactions"));
    // Add Transactions Tab
    pane.addTab("Transactions", transactionPanel);

    JPanel accountPanel = new JPanel();
    accountPanel.add(new JLabel("Account"));
    // Add Account Tab
    pane.addTab("Account", accountPanel);

    this.setLayout(new BorderLayout());
    this.setPreferredSize(new Dimension(400, 200));
    this.add(pane, BorderLayout.CENTER);
}

public static void main(String[] args) {
    
    JPanel panel = new TabbedPaneDemo();
     panel.setOpaque(true);
   // panel.setBounds(12, 12, 45, 98);

    JFrame frame = new JFrame("JTabbedPane Demo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(panel);
    frame.pack();
    frame.setVisible(true);
}}

and the output it's !

enter image description here

I want to change in this code to be like this.

enter image description here

I want to use an absolute layout (null). I want to add menu, buttons and labels with these tabs ...

How can I do??

1

There are 1 answers

2
camickr On BEST ANSWER

You should NOT use absolute layout. There is not reason to use absolute layout.

Instead you should use layout managers.

Maybe create a panel and add buttons to the panel. Then add the panel to the NORTH of the content pane. The tabbed pane would be added to the CENTER.

Read the Swing tutorial on Using Layout Managers.