Java:JPanel中的垂直对齐

时间:2012-07-06 07:28:50

标签: java swing alignment jpanel jlabel

我试图在一个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);

我尝试过使用setAligmentY()但没有成功。两个标签始终显示在JPanel的顶部。

UPD:标签应该像使用FlowLayout一样位于彼此旁边,但是在JPanel的中间。

3 个答案:

答案 0 :(得分:20)

使用具有默认约束的GridBagLayout。这是一个小的演示代码:

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();
            }
        });
    }

}

答案 1 :(得分:14)

你可以看到这个答案: https://stackoverflow.com/a/18073909/189411

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

答案 2 :(得分:2)

使用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);
        }
    }

输出如下:

enter image description here