【问题标题】:Auto adjust button width自动调整按钮宽度
【发布时间】:2014-01-23 08:10:25
【问题描述】:

我有一个带有 3 个按钮的 JPanel(它是一个 JPanel,它将包含一个带有按钮 和 JLabels 的菜单)。

按钮需要堆叠在一起,它们之间要留出很小的空间。

我将按钮堆叠在一起:

 BoxLayout myLayout = new BoxLayout(this, BoxLayout.Y_AXIS);
 this.setLayout(myLayout);
 this.setPreferredSize(new Dimension(300,150));

现在按钮在添加时会自动堆叠,但它们的大小都不同,具体取决于其中的文本量。

我想要的是按钮位于JPanel 的中心,并且具有面板的宽度 - 每边 10 像素。

我有什么:

给所有的按钮:

    .setPreferredSize(new Dimension(280,20));

但这并没有什么不同。

为了更清楚,我将发布完整的代码,以便您了解我的意思:

private JButton buttonStartStop;
private JButton buttonReset;


public ViewControlsPanel()
{
    BoxLayout myLayout = new BoxLayout(this, BoxLayout.Y_AXIS);
    this.setLayout(myLayout);

    this.setPreferredSize(new Dimension(300,150));

    buttonStartStop = new JButton("Start");
    buttonStartStop.setPreferredSize(new Dimension(280,30));

    buttonReset = new JButton("Reset");
    buttonReset.setPreferredSize(new Dimension(280,20));


    this.add(buttonStartStop);
    this.add(buttonReset);

}

我试图给出不同的维度,但没有帮助。

我的猜测是可以给 BoxLayout 一个属性来给包含的组件一个首选的大小,但我找不到它。

【问题讨论】:

标签: java swing jbutton layout-manager boxlayout


【解决方案1】:

我想要的是位于JPanel 中心的按钮,并且具有面板的宽度 - 每侧 10 像素。

这似乎最适合 GridLayout 的单个列 EmptyBorder 为 10 像素(除非我误解了要求)。

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class SingleColumnOfButtonsLayout {

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                // the GUI as seen by the user (without frame)
                JPanel gui = new JPanel(new GridLayout(0,1,10,10));
                gui.setBorder(new EmptyBorder(10,10,10,10));

                gui.add(new JButton("Start"));
                gui.add(new JButton("Reset"));
                gui.add(new JButton("A Very Long String"));

                JFrame f = new JFrame("Single Column of Buttons Layout");
                f.add(gui);
                // Ensures JVM closes after frame(s) closed and
                // all non-daemon threads are finished
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                // See http://stackoverflow.com/a/7143398/418556 for demo.
                f.setLocationByPlatform(true);

                // ensures the frame is the minimum size it needs to be
                // in order display the components within it
                f.pack();
                // should be done last, to avoid flickering, moving,
                // resizing artifacts.
                f.setVisible(true);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency
        SwingUtilities.invokeLater(r);
    }
}

【讨论】:

  • 这适用于任何组件,只需更改此部分 gui.add(new Thing()),其中 Thing 是您想要的组件
【解决方案2】:

这只是 Andrews 答案的扩展,以证明您可以使用任何组件。唯一的变化是我把它变成了 2 列宽

JPanel gui = new JPanel(new GridLayout(0,2,10,10));

并添加了不同的组件

gui.add(new JLabel("Start Label", SwingConstants.TRAILING));
gui.add(new JButton("Start"));
gui.add(new JButton("Reset"));
gui.add(new JLabel("Reset Label", SwingConstants.CENTER));
gui.add(new JComboBox(new String[]{"A","B","C"}));
gui.add(new JCheckBox("A Check Box"));

看起来像这样:

【讨论】:

  • thx 我对 JLabels 有疑问,这是一个很好的例子,可以显示所有组件都调整到布局。每天都变得更聪明:)
猜你喜欢
  • 1970-01-01
  • 2017-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-24
  • 2017-07-22
  • 2020-05-19
  • 1970-01-01
相关资源
最近更新 更多