Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在使用FlowLayout的JPanel中垂直居中组件_Java_Swing_Layout_Layout Manager - Fatal编程技术网

Java 如何在使用FlowLayout的JPanel中垂直居中组件

Java 如何在使用FlowLayout的JPanel中垂直居中组件,java,swing,layout,layout-manager,Java,Swing,Layout,Layout Manager,我有一个特定的面板,其中包含随机数目的项目。此面板添加到使用BorderLayout的JPanel的东部 我想让它们垂直居中 我如何做到这一点 下面是一个可以运行的代码 public class MainFrame { public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new AlignDemo()); } } class AlignDemo imp

我有一个特定的面板,其中包含随机数目的项目。此面板添加到使用BorderLayout的JPanel的东部

我想让它们垂直居中

我如何做到这一点

下面是一个可以运行的代码

public class MainFrame {

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new AlignDemo());
    }
}

class AlignDemo implements Runnable {
    @Override
    public void run(){           
        try {
            JFrame mainWindow = new JFrame();
            mainWindow.getContentPane().add(initPanel());
            mainWindow.pack();
            mainWindow.setVisible(true);
        } catch (Throwable th) {                    
            JOptionPane.showMessageDialog(null,null,"General Error", JOptionPane.ERROR_MESSAGE);                        
        }
    }

    private JPanel initPanel() {
        FlowLayout layout = new FlowLayout(FlowLayout.LEFT);
        layout.setHgap(15);
        JPanel myContent = new JPanel();
        myContent.setPreferredSize(new Dimension(400,200));
        myContent.setBorder(BorderFactory.createLineBorder(Color.blue));
        JButton button1 = new JButton("I'm a button");
        JButton button2 = new JButton("I'm a button");
        JButton button3 = new JButton("I'm a button");
        myContent.add(button1,Component.CENTER_ALIGNMENT);
        myContent.add(button2,Component.CENTER_ALIGNMENT);
        myContent.add(button3,Component.CENTER_ALIGNMENT);
        return myContent;
    }
}   

它可以通过组合布局轻松实现。
JPanel
带有
FlowLayout
控件
)的
JPanel
将按钮相对放置,作为单个组件放置在带有
gridbagloayout
ui
)的
JPanel

import java.awt.*;
导入javax.swing.*;
导入javax.swing.border.EmptyBorder;
公共类中心按钮2{
私有JComponent ui=null;
中心按钮2(){
initUI();
}
public void initUI(){
如果(ui!=null)返回;
ui=new JPanel(new GridBagLayout());//将单个组件居中放置
ui.新订单(新的空订单(4,4,4,4));
JPanel controls=newjpanel(newflowlayout());

对于(int ii=1;ii而言,可以通过将布局组合起来来轻松实现。
JPanel
FlowLayout
控件
)相对定位按钮,将按钮作为单个组件放置在带有
GridBagLayout
ui
)的
JPanel

import java.awt.*;
导入javax.swing.*;
导入javax.swing.border.EmptyBorder;
公共类中心按钮2{
私有JComponent ui=null;
中心按钮2(){
initUI();
}
public void initUI(){
如果(ui!=null)返回;
ui=new JPanel(new GridBagLayout());//将单个组件居中放置
ui.新订单(新的空订单(4,4,4,4));
JPanel controls=newjpanel(newflowlayout());

对于(int ii=1;ii使用默认值
GridBagLayout()
。如果我不知道我的项目数量,你也建议这样做吗?另外,我的项目是图标,它们在使用该布局时不会跨越吗?我的意思是:用
网格布局创建一个额外的
JPanel
,设置它的首选大小,然后使用
流程布局在那里添加面板。但是我也意识到这不是与
GridBagLayout
组合时,换行符出现问题。如果将
FlowLayout
替换为,则使用默认的
GridBagLayout()
。如果我不知道我的项目数量,你也建议这样做吗?另外,我的项目是图标,它们在使用该布局时不会跨越吗?我的意思是:用
网格布局创建一个额外的
JPanel
,设置它的首选大小,然后使用
流程布局在那里添加面板。但是我也意识到这不是与
GridBagLayout
组合使用时,换行符出现问题。如果将
FlowLayout
替换为,则。
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class CenteredButtons2 {

    private JComponent ui = null;

    CenteredButtons2() {
        initUI();
    }

    public void initUI() {
        if (ui!=null) return;

        ui = new JPanel(new GridBagLayout()); // to center a single component
        ui.setBorder(new EmptyBorder(4,4,4,4));

        JPanel controls = new JPanel(new FlowLayout());
        for (int ii=1; ii<4; ii++) {
            controls.add(new JButton("Button " + ii));
        }
        controls.setBorder(new EmptyBorder(50, 90, 50, 90));
        ui.add(controls);
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                CenteredButtons2 o = new CenteredButtons2();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}