valign jpanel不工作

mm9b1k5b  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(363)

我正在尝试垂直对齐jpanel。setalignmenty未按预期工作。这是我到目前为止所做的。

import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;

public class ValignJPanel {

    public static void main(String args[]) {
        JFrame frame = new JFrame("Vertical Alignment");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel1 = wrapPanel(createPanel(), Component.TOP_ALIGNMENT);
        JPanel panel2 = wrapPanel(createPanel(), Component.CENTER_ALIGNMENT);
        JPanel panel3 = wrapPanel(createPanel(), Component.BOTTOM_ALIGNMENT);

        frame.setLayout(new GridLayout(1, 3));
        frame.add(panel1);
        frame.add(panel2);
        frame.add(panel3);

        frame.setSize(400, 150);
        frame.setVisible(true);
    }

    private static JPanel wrapPanel(JPanel oldPanel, float alignment) {
        JPanel panel = new JPanel();
        oldPanel.setAlignmentY(alignment);
        panel.add(oldPanel);
        return panel;
    }

    private static JPanel createPanel() {
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.add(new JLabel("Hello world"));
        panel.add(new JTextField(10));
        panel.setBorder(new LineBorder(Color.red));
        return panel;
    }
}

我正在尝试创建一个 Package 器方法,它接受jpanel并将其 Package 为给定的对齐方式。
编辑(从评论,与图像)我试图得到输出,这看起来像这样,但在代替按钮,我想有jpanel

svgewumm

svgewumm1#

您需要做的是将布局设置为 BoxLayout 对于 new JPanelwrapPanel 方法。

JPanel panel = new JPanel();
BoxLayout layout = new BoxLayout(panel, BoxLayout.LINE_AXIS);
panel.setLayout(layout);

bug:对于您当前的代码,它似乎不起作用,因为它会拉伸文本字段。如果你使用 JButton 代替 JTextFeild 你会看到效果的。我还在想为什么文本字段会这样。
与上面的代码一起更新,以解决文本字段拉伸的问题,正如camickr在这个答案中所回答的那样 BoxLayout 是少数几个尊重组件最小和最大尺寸的布局管理器之一。因此,如果要防止面板拉伸,可以使用:

panel.setMaximumSize( panel.getPreferredSize() );

上面的代码就是您要放入 createPanel 方法。

import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;

public class ValignJPanel {

    public static void main(String args[]) {
        JFrame frame = new JFrame("Vertical Alignment");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel1 = wrapPanel(createPanel(), Component.TOP_ALIGNMENT);
        JPanel panel2 = wrapPanel(createPanel(), Component.CENTER_ALIGNMENT);
        JPanel panel3 = wrapPanel(createPanel(), Component.BOTTOM_ALIGNMENT);

        frame.setLayout(new GridLayout(1, 3));
        frame.add(panel1);
        frame.add(panel2);
        frame.add(panel3);
        frame.setSize(400, 150);
        frame.setVisible(true);
    }

    private static JPanel wrapPanel(JPanel oldPanel, float alignment) {
        JPanel panel = new JPanel();
        BoxLayout layout = new BoxLayout(panel, BoxLayout.LINE_AXIS);
        panel.setLayout(layout);
        oldPanel.setAlignmentY(alignment);
        panel.add(oldPanel);
        return panel;
    }

    private static JPanel createPanel() {
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.add(new JLabel("Hello world"));
        panel.add(new JTextField(10));
        panel.setBorder(new LineBorder(Color.red));
        panel.setMaximumSize( panel.getPreferredSize());
        return panel;
    }
}

相关问题