java - 使所有按钮大小相同

标签 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");
        }
    }

我想让所有按钮的大小相同。在这里,第一个比第二个宽。我需要两者相同的宽度和高度。正如你所看到的,我已经使用了所有可用的方法,如 setPrefferedSize()、setMaximumSize()、setMinimumSize(),但它仍然无法正常工作。请帮忙!

最佳答案

这是使用 GridLayout 实现此目的的一种方法。我还引入了一个额外的 JPanel,以便在调整 JFrame 大小时按钮不会拉伸(stretch),并且我为其选择了 GridBagLayout,这样它就可以将按钮面板垂直居中。肯定还有其他方法可以解决您的问题。

您应该尽量避免的一件事是强制使用首选/最大/最小尺寸。将其委托(delegate)给 L&F 和 LayoutMananager。

如果您在 JFrame 上调用 pack(),则之前设置其大小是没有用的,因为 pack() 会改变它反正。尝试在 GUI 初始化的最后一行调用 setVisible(true);

如果您想正确理解布局、定位、大小调整等在 Swing 中的工作原理,我强烈建议您阅读 the tutorial on LayoutManager's .

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

关于java - 使所有按钮大小相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11536089/

相关文章:

java - 在 JPanel 之间绘图

java - JList/JTable 作为按钮面板

java - Flexiforce 到 Uno 到键盘。工作正常,但未达到预期效果

java - 删除 JPanel 内的 JPanel... 在 JPanel 内

java - 如何动态更新图像到jlabel

java - 通过 iText 重写现有 pdf

java - GridBagLayout 中的图像大小不正确

java - JBDC - 跨并发线程以原子方式执行 SELECT 和 INSERT

java - JavaFX 中的 Platform.runLater 和 Task

java - 如何通过java获取txt的列内容