1

I am creating a java swing app which makes use of the JProgressBar control. The control looks ok on Linux and Windows but it is too big for my likings on Mac. I would like to change its height.

I am constructing the entire layout via boxes, i.e. createHorizontalBox, etc. I understand that whatever I put in the box it should take its entire space.

Here is my code (it is ugly):

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

I am struggling to find the right way to layout all the components such as the JProgressBar height can be controlled.

Can you help?

UPDATE: Here is a small program which demonstrates the problem

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 Defaultsを確認すると、Mac 用にカスタマイズできる場合があります。

于 2011-05-28T17:56:41.833 に答える
2

レイアウトは通常、コンポーネントの推奨サイズを尊重します。

于 2011-05-28T17:25:50.197 に答える