Java:JPanel中的垂直对齐

ycggw6v2  于 2023-05-27  发布在  Java
关注(0)|答案(3)|浏览(139)

我正在尝试在一个JPanel中垂直对齐(居中)两个JLabel。

JPanel panel = new JPanel();
panel.setPreferredSize(size);
JLabel label1 = new JLabel(icon);
JLabel label2 = new JLabel("text");
panel.add(label1);
panel.add(label2);

我试过使用setAlignmentY(),但没有成功。这两个标签始终显示在JPanel的顶部。
UPD:标签应该像使用FlowLayout一样彼此相邻,但在JPanel的中间。

5n0oy7gb

5n0oy7gb1#

使用带有默认约束的GridBagLayout。下面是一个小的demo代码:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TestVerticalAlignement {

    protected void initUI() {
        final JFrame frame = new JFrame();
        frame.setTitle("Test vertical alignement");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        panel.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        JLabel label1 = new JLabel("label1");
        JLabel label2 = new JLabel("label2");
        panel.add(label1, gbc);
        panel.add(label2, gbc);
        frame.add(panel);
        frame.setSize(300, 300);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new TestVerticalAlignement().initUI();
            }
        });
    }

}
z9smfwbn

z9smfwbn2#

你可以使用Nakul Sudhakar的2013年答案:
我使用了一个BoxLayout,并将它的第二个参数设置为BoxLayout.Y_AXIS,它对我很有效:

panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
oyt4ldly

oyt4ldly3#

使用gridlayout,简单。应该可以
请看下面的例子:

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

/*
    <applet code=AJ07 width=450 height=450>
    </applet>
*/

    public class AJ07 extends JApplet{
        Container c=null;

        public void init(){

                JPanel pTop=new JPanel();
                JPanel pLeft=new JPanel();
                JPanel pCenter=new JPanel();
                JPanel pProperties=new JPanel();

                pLeft.setLayout(new GridLayout(20,1));

                c=this.getContentPane();
                JButton bNew=new JButton("New");
                pTop.add(bNew);
                JButton bOpen=new JButton("Open");
                pTop.add(bOpen);
                JButton bSave=new JButton("Save");
                pTop.add(bSave);
                JButton bSaveAll=new JButton("Save All");
                pTop.add(bSaveAll);
                JButton bRun=new JButton("Run");
                pTop.add(bRun);
                JButton bStop=new JButton("Stop");
                pTop.add(bStop);
                JButton bPause=new JButton("Pause");
                pTop.add(bPause);

                JButton bText=new JButton("TextBox");
                pLeft.add(bText);
                JButton bButton=new JButton("Button");
                pLeft.add(bButton);

                pProperties.setLayout(new GridLayout(20,1));
                pProperties.add(new Label("BackColor"));
                pProperties.add(new Label("ForeColor"));
                c.add(new TextArea(),BorderLayout.CENTER);

                c.add(pTop,BorderLayout.NORTH);
                c.add(pLeft,BorderLayout.WEST);
                c.add(new Label("Project Loaded Successfully!"),BorderLayout.SOUTH);
                c.add(pProperties,BorderLayout.EAST);
                //c.add(pCenter,BorderLayout.CENTER);
        }
    }

其输出如下:

相关问题