简体   繁体   中英

How to align JPanel in java

I have a JPanel which is in a box layout but I am unsure how to align the JPanel to center of the window (and stay centered even if window is resized) I've tried looking for a solution but all questions seem over complicated compared to what it is that I'm looking for.

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

public class Stacker extends JFrame {
public Stacker() {
    super("Stacker");
    setSize(430, 200);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    // create top panel
    JPanel commandPane = new JPanel();
    BoxLayout vertical = new BoxLayout(commandPane,
        BoxLayout.Y_AXIS);
    commandPane.setLayout(vertical);

    JButton subscribe = new JButton("Subscribe");
    JButton unsubscribe = new JButton("Unsubscribe");
    JButton refresh = new JButton("Refresh");
    JButton save = new JButton("Save");

    commandPane.add(subscribe);
    commandPane.add(unsubscribe);
    commandPane.add(refresh);
    commandPane.add(save);

    JMenuItem j1 = new JMenuItem("File");
    JMenuItem j2 = new JMenuItem("Open");
    JMenuItem j3 = new JMenuItem("Close");
    JMenuBar menubar = new JMenuBar();
    JMenu menu = new JMenu("Feeds");
    menu.add(j1);
    menu.add(j2);
    menu.add(j3);
    menubar.add(menu);

    setJMenuBar(menubar);



    // create bottom panel
    /*JPanel textPane = new JPanel();
    JTextArea text = new JTextArea(4, 70);
    JScrollPane scrollPane = new JScrollPane(text);
    // put them together
    FlowLayout flow = new FlowLayout();
    setLayout(flow);
    add(commandPane);
    add(scrollPane); */
    setJMenuBar(menubar);
    add(commandPane);
    setVisible(true);
}

public static void main(String[] arguments) {
    Stacker st = new Stacker();
}
}

You say you're using a BoxLayout, but is the JPanel with the BoxLayout the JPanel you want to center, or does it contain the JPanel you want to center?

If it contains the JPanel you want to center, then you can add a glue on either side of the JPanel to be centered. If it is the JPanel you want to center, then you can use GridBagLayout or BoxLayout to achieve the effect you're talking about.

Googling something like "Java center component" will give you a ton of results.

for this idea (still not clear from your description) use GridBagLayout without set for GridBagConstraints

.

在此处输入图片说明

.

在此处输入图片说明

.

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

public class CenteredJPanel {

    private JFrame frame = new JFrame("Test");
    private JPanel panel = new JPanel();
    private JButton subscribe = new JButton("Subscribe");
    private JButton unsubscribe = new JButton("Unsubscribe");
    private JButton refresh = new JButton("Refresh");
    private JButton save = new JButton("Save");

    public CenteredJPanel() {
        panel.setLayout(new GridBagLayout());
        panel.add(subscribe);
        panel.add(unsubscribe);
        panel.add(refresh);
        panel.add(save);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(panel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                CenteredJPanel centeredJLabel = new CenteredJPanel();
            }
        });
    }
}

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