简体   繁体   中英

Is there a simpler way to display this Java UI?

I want to display a line of text with a button underneath it, both of which are centered horizontally and vertically. I currently have a BoxLayout containing a JLabel , a rigid area for spacing and then a JButton within a JPanel . This seems quite complicated for what I want to do and also when the window size is changed, both the text and the button are no longer centered. Is there a better way to do this?

----------------------
|                    |
|                    |
|                    |
|      ________      |
|      | Text |      |
|      --------      |
|      ________      |
|      |Button|      |
|      --------      |
|                    |
|                    |
|                    |
----------------------

使用2x1 GridLayout

I'd go with something like:

JButton btn = new JButton("I'm a Button");
JLabel lbl = new JLabel("I'm a Label");

JPanel pan = new JPanel();
pan.setLayout(new BoxLayout(pan, BoxLayout.Y_AXIS));

pan.add(lbl);
pan.add(btn);

JPanel pan2 = new JPanel();
pan2.setLayout(new BoxLayout(pan2, BoxLayout.X_AXIS));

pan2.add(Box.createHorizontalGlue());
pan2.add(pan);
pan2.add(Box.createHorizontalGlue());

setLayout(new BorderLayout());
add(pan2, BorderLayout.CENTER);

It's still a bit complex, and you'd probably want to add another panel for the label so that it's centered, UIs in my experience tend to be a lot of work to layout.

居中组件

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

class CenteredComponents {

    public static void main(String[] args) {
        SwingUtilities.invokeLater( new Runnable() {
            public void run() {
                JPanel gui = new JPanel(new GridLayout(0,1));
                JLabel l = new JLabel("Centered Text", SwingConstants.CENTER);
                gui.add(l);
                // the GBL allows us to center a component
                JPanel p = new JPanel(new GridBagLayout());
                p.add(new JButton("Centered Button"));
                gui.add(p);

                JFrame f = new JFrame("Centered Components");
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setContentPane(gui);
                f.pack();
                f.setSize(350,150);
                f.setLocationByPlatform(true);
                f.setVisible(true);
            }
        });
    }
}

Update

I need both components to stay vertically centered when the window is maximised

居中组件2

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

class CenteredComponents {

    public static void main(String[] args) {
        SwingUtilities.invokeLater( new Runnable() {
            public void run() {
                JPanel gui = new JPanel(new GridLayout(0,1));
                // the GBL allows us to center a component..
                JPanel center = new JPanel(new GridBagLayout());
                JLabel l = new JLabel("Centered Text", SwingConstants.CENTER);
                gui.add(l);
                gui.add(new JButton("Centered Button"));
                // ..in this case the component is a panel
                center.add(gui);

                JFrame f = new JFrame("Centered Components");
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setContentPane(center);
                f.pack();
                f.setSize(350,150);
                f.setLocationByPlatform(true);
                f.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