2011-05-28 95 views
1

我正在創建一個使用JProgressBar控件的java swing應用程序。該控件在Linux和Windows上看起來不錯,但對於我在Mac上的喜好來說太大了。我想改變它的高度。控制JProgressBar高度

我正在構建整個佈局,通過框,即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的正確的方式進行控制。

你能幫忙嗎?

UPDATE:這裏是一個小程序,它演示該問題

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

} 
+0

爲了更快得到更好的幫助,請發佈[SSCCE](http://pscode.org/sscce.html)。 – 2011-05-28 17:26:23

回答

2
  1. 如果空間可用,箱體佈局將試圖將部件拉伸到其最大尺寸。因此,請確保首選高度和最大高度是相同的值,以免拉伸。

  2. 進度條的大小可由LAF確定。 Metal LAF使用「ProgressBar.horizo​​ntalSize」。看看UIManager Defaults,你也許可以爲Mac定製它。

+0

@Pass,爲了其他人的利益,哪些建議有效? – camickr 2011-05-28 18:06:02

+0

this.statusProgressbar.setPreferredSize(statusProgressbarDimension); this.statusProgressbar.setMaximumSize(statusProgressbarDimension); //當我在測試程序中單獨測試時,有沒有技巧。但是,當我嘗試更新進度欄時,GUI會有點不合適。 – Pass 2011-05-28 18:12:47

+1

@Pass,正確的解決方案是重寫getMaximumSize()方法以返回適當的值,該值將是最大寬度以及首選高度。 – camickr 2011-05-28 18:34:09

1

佈局通常兌現的部件的優選尺寸。

+0

這是正確的,但它似乎並沒有工作 – Pass 2011-05-28 17:35:03

+1

@Pass:我們無法知道哪些工作或什麼不適合您,因爲我們無法編譯和運行您的發佈的代碼。如上所述,考慮創建併發佈一個小型可編譯和可運行的示例,[SSCCE](http://SSCCE.org) – 2011-05-28 17:40:01