简体   繁体   中英

Can I have JPanels with different sizes in JTabbedPane tabs?

I want JPanels to add to a JTabbedPane which would have different sizes. I want the JTabbedPane to have a fixed size (of a JFrame it's inserted into) and inside of the tabs to show JPanels which would have the individual size for each JPanel. So if the JPanel is smaller than the size of a tab, the rest of the tab will be the colour of the JFrame (and if it's bigger, JScrollPane will show). How can I achieve it?

Here is a simple code:

package sample;

import javax.swing.*;
import java.awt.*;

public class Main {

    private static JFrame frame;

    Main() {
        frame = new JFrame();

        JTabbedPane jTabbedPane = new JTabbedPane();

        JPanel panel1 = new JPanel();
        JPanel panel2 = new JPanel();
        JPanel panel3 = new JPanel();

        panel1.setSize(new Dimension(400, 400));
        panel2.setSize(new Dimension(500, 600));
        panel3.setSize(new Dimension(100, 100));

        panel1.setBackground(Color.WHITE);
        panel2.setBackground(Color.RED);
        panel3.setBackground(Color.GREEN);

        panel1.setBorder(BorderFactory.createLineBorder(Color.BLACK, 3));
        panel2.setBorder(BorderFactory.createLineBorder(Color.BLACK, 3));
        panel3.setBorder(BorderFactory.createLineBorder(Color.BLACK, 3));

        jTabbedPane.addTab("Tab1", panel1);
        jTabbedPane.addTab("Tab2", panel2);
        jTabbedPane.addTab("Tab3", panel3);

        frame.getContentPane().add(jTabbedPane, BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 600);
        frame.setLocationRelativeTo(null);
    }


    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                Main main = new Main();
                frame.setVisible(true);
            }
        });
    }
}

Does anyone know how to get this effect? I found several posts on resizing the whole window when resizing the content, but I want the window to stay one size and resize just the content.

Can I set Layout for tabs? Even if I set up layout for JFrame in which JTabbedPane is inserted, the tabs seem to have their own layout inside. I want the elements to be at top left corner. Can I change it somehow?

. So if the JPanel is smaller than the size of a tab, the rest of the tab will be the colour of the JFrame

Not sure that is a great LAF. The tabbed pane has a border. even the you only display the smaller panel, you will still see the border of the tabbed pane be the size of the largest tab.

Can I set Layout for tabs?

The tabbed pane has its own layout manager which is why all child tabs are the same size.

However, you can simply add a "child" panel to the panel you add to the tab. Then you use whatever layout manager you want to position the child panel.

Using this approach you would set the background of the child panel, and the tab will appear as the same background of the frame, but of course you would still see the tabbed pane border.

panel1.setSize(new Dimension(400, 400));

Don't use setSize(...) in Swing components. That does nothing. It is the job of the layout manager to set the size/location of the component, so anything you specify will be changed.

For a simple "MCVE" example you can use setPreferredSize(...) , but the general rule is to let each component determine its own preferred size.

Implementing the above suggestions you get this:

import javax.swing.*;
import java.awt.*;

public class Main2 {

    private static JFrame frame;

    Main2() {
        frame = new JFrame();

        JTabbedPane jTabbedPane = new JTabbedPane();
        jTabbedPane.setBorder(null);

        JPanel panel1 = new JPanel();
        JPanel panel2 = new JPanel();
        JPanel panel3 = new JPanel();

        panel1.setLayout( new FlowLayout() );
        JPanel child = new JPanel();
        child.setPreferredSize( new Dimension(100, 100) );
        child.setBackground( Color.WHITE );
        panel1.add( child );

        panel1.setPreferredSize(new Dimension(400, 400));
        panel2.setPreferredSize(new Dimension(500, 600));
        panel3.setPreferredSize(new Dimension(100, 100));

        //panel1.setBackground(Color.WHITE);
        panel2.setBackground(Color.RED);
        panel3.setBackground(Color.GREEN);

        panel1.setBorder(BorderFactory.createLineBorder(Color.BLACK, 3));
        panel2.setBorder(BorderFactory.createLineBorder(Color.BLACK, 3));
        panel3.setBorder(BorderFactory.createLineBorder(Color.BLACK, 3));

        jTabbedPane.addTab("Tab1", panel1);
        jTabbedPane.addTab("Tab2", panel2);
        jTabbedPane.addTab("Tab3", panel3);

        frame.setLayout( new FlowLayout() );
        frame.add(jTabbedPane);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 700);
        frame.setLocationRelativeTo(null);
    }


    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                Main2 main = new Main2();
                frame.setVisible(true);
            }
        });
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM