使所有按钮大小相同

Pea*_*Gen 2 java swing

请看下面的代码

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

public class GUI extends JFrame
{
    private JButton open, process;
    private JLabel center;
    private JScrollPane scroll;
    private Box box;
    private IplImage image;

    public FirstGUI()
    {
        open = new JButton("Open Image");
        open.setPreferredSize(new Dimension(70,20));
        open.setMaximumSize(new Dimension(100,20));

        open.addActionListener(new OpenImageAction());
        process = new JButton("Process");
        process.setPreferredSize(new Dimension(100,20));
        process.setMinimumSize(new Dimension(100,20));
        process.addActionListener(new ProcessAction());
        System.out.println("Open Size: "+open.getSize()+"      Process size: "+process.getSize());


        box = new Box(BoxLayout.Y_AXIS);
        box.add(open);
        box.add(process);

        center = new JLabel();
        scroll = new JScrollPane(center);

        getContentPane().add(box,"West");
        getContentPane().add(scroll,"Center");

        this.setSize(300,300);
        this.pack();
        this.validate();
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    public static void main(String[]args)
    {
        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

            new GUI();
        }
        catch(java.lang.Exception e)
        {
            JOptionPane.showMessageDialog(null,"GUI Error");
        }
    }
Run Code Online (Sandbox Code Playgroud)

我想让所有按钮尺寸相同.在这里,第一个比第二个宽.我需要两个相同的宽度和高度.如您所见,我已经使用了所有可用的setPrefferedSize(),setMaximumSize(),setMinimumSize(),但它仍然无法正常工作.请帮忙!

Gui*_*let 9

以下是使用a实现此目的的一种方法GridLayout.我还介绍了一个附加功能,JPanel以便在JFrame调整大小时按钮不会拉伸,我选择了GridBagLayout它,使其垂直居中按钮面板.肯定有其他方法可以解决您的问题.

您应该尽量避免使用强制首选/最大/最小尺寸.将此委托给L&F和LayoutMananager's.

如果你打电话pack()给a JFrame,之前设置它的大小是没用的pack(),无论如何都会改变它.尝试调用setVisible(true);GUI初始化的最后一行.

如果你想正确理解布局,定位,大小调整等在Swing中是如何工作的,我强烈建议你阅读关于LayoutManager的教程.

import java.awt.BorderLayout;
import java.awt.GridBagLayout;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class GUI extends JFrame {
    private JButton open, process;
    private JLabel center;
    private JScrollPane scroll;
    private JPanel box;

    public GUI() {
        open = new JButton("Open Image");
        // open.addActionListener(new OpenImageAction());
        process = new JButton("Process");
        // process.addActionListener(new ProcessAction());

        box = new JPanel(new GridLayout(2, 1));
        box.add(open);
        box.add(process);
        JPanel west = new JPanel(new GridBagLayout());
        west.add(box);

        center = new JLabel("Some center label");
        scroll = new JScrollPane(center);

        getContentPane().add(west, BorderLayout.WEST);
        getContentPane().add(scroll);

        this.pack();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (java.lang.Exception e) {
            JOptionPane.showMessageDialog(null, "GUI Error");
        }
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new GUI();
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)