1

我正在创建一个使用 JProgressBar 控件的 java swing 应用程序。该控件在 Linux 和 Windows 上看起来不错,但对于我在 Mac 上的喜好来说太大了。我想改变它的高度。

我正在通过盒子构建整个布局,即 createHorizo​​ntalBox 等。我知道无论我放在盒子里什么,它都应该占据整个空间。

这是我的代码(很难看):

    this.iconLabel = new JLabel();
    this.nameLabel = new JLabel();
    this.statusProgressbar = new JProgressBar();
    this.pausePush = new PushButton();
    this.resumePush = new PushButton();
    this.actionPush = new PushButton();
    this.statusLabel = new JLabel("...");
    this.clientTask = null;

    this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
    this.setOpaque(false);
    this.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

    this.add(this.iconLabel);

    Box container = Box.createVerticalBox();
    Box box = Box.createHorizontalBox();

    box.setOpaque(false);
    box.add(this.nameLabel);
    box.add(Box.createHorizontalGlue());

    container.add(box);

    this.pausePush.setVisible(false);
    this.resumePush.setVisible(false);
    this.actionPush.setVisible(false);

    box = Box.createHorizontalBox();

    box.setOpaque(false);
    box.add(this.statusProgressbar);
    box.add(this.pausePush);
    box.add(this.resumePush);
    box.add(this.actionPush);

    container.add(box);

    this.nameLabel.setFont(new Font(UIManager.getFont("TabbedPane.font").getFamily(), 0, 12));
    this.statusLabel.setFont(new Font(UIManager.getFont("TabbedPane.font").getFamily(), 0, 9));

    box = Box.createHorizontalBox();

    box.setOpaque(false);
    box.add(this.statusLabel);
    box.add(Box.createHorizontalGlue());

    container.add(box);

    this.add(container);

我正在努力寻找正确的方法来布局所有组件,例如可以控制 JProgressBar 的高度。

你能帮我吗?

更新:这是一个演示问题的小程序

package test;

import java.awt.Dimension;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.UIManager;

public class Test {

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();

        frame.setSize(500, 500);

        panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
        panel.setOpaque(false);
        panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        JLabel nameLabel = new JLabel("...");
        JProgressBar statusProgressbar = new JProgressBar();
        JLabel statusLabel = new JLabel("...");

        Box container = Box.createVerticalBox();
        Box box = Box.createHorizontalBox();

        box.setOpaque(false);
        box.add(nameLabel);
        box.add(Box.createHorizontalGlue());

        container.add(box);

        statusProgressbar.setPreferredSize(new Dimension(0, 5)); // NOT WORKING

        box = Box.createHorizontalBox();

        box.setOpaque(false);
        box.add(statusProgressbar);

        container.add(box);

        nameLabel.setFont(new Font(UIManager.getFont("TabbedPane.font").getFamily(), 0, 9));
        statusLabel.setFont(new Font(UIManager.getFont("TabbedPane.font").getFamily(), 0, 9));

        box = Box.createHorizontalBox();

        box.setOpaque(false);
        box.add(statusLabel);
        box.add(Box.createHorizontalGlue());

        container.add(box);

        panel.add(container);

        frame.add(panel);
        frame.setVisible(true);
    }

}
4

2 回答 2

4
  1. 如果空间可用,Box 布局将尝试将组件拉伸到其最大尺寸。所以请确保首选高度和最大高度是相同的值,这样它就不会被拉伸。

  2. 进度条的大小可以由 LAF 确定。Metal LAF 使用“ProgressBar.horizo​​ntalSize”。查看UIManager 默认值,您也许可以为 Mac 自定义它。

于 2011-05-28T17:56:41.833 回答
2

布局通常尊重组件的首选大小。

于 2011-05-28T17:25:50.197 回答