我有一些麻煩設置插入到JToolbar中的JLabel組件的大小。我使用JLabel來顯示實際的鼠標座標,但取決於數字的長度,JLabel會改變它的大小,從而產生JToolbar組件的一些令人討厭的移動。在JToolbar中設置JLabel的大小
我玩setSize,setMinimumSize等來解決這個問題,但仍然沒有結果。我知道這種方法的行爲會隨着佈局管理器而改變。
哪種方法可以將JLabel的固定最小維數定義到JToolbar中?
這裏是我的代碼:
public class EditorStatusBar extends JToolBar{
private JLabel areaCoordX;
private JLabel areaCoordY;
public EditorStatusBar(){
super();
addSeparator(new Dimension(100,this.getSize().height));
this.areaCoordX = new JLabel(" ");
this.areaCoordX.setMinimumSize(new Dimension(80,10));
this.areaCoordX.setPreferredSize(new Dimension(80,10));
this.areaCoordX.setHorizontalAlignment(JLabel.RIGHT);
this.areaCoordX.setOpaque(true);
this.areaCoordY = new JLabel("");
this.add(areaCoordX);
this.add(new JLabel(":"));
this.add(areaCoordY);
}
}
public void setCoordOfComponent(Point c){
this.areaCoordX.setText(""+c.x);
this.areaCoordY.setText(""+c.y);
}
}
public class Gui extends JFrame implements ActionListener, ItemListener,ChangeListener{
private EditorStatusBar statusBar;
public static void main(String[] args) {
new Gui();
}
private void buildStatusBar(){
statusBar = new EditorStatusBar();
statusBar.setFloatable(false);
statusBar.setMaximumSize(new Dimension(2000, 20));
}
public Gui() {
super();
getContentPane().setLayout(
new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
buildStatusBar();
this.getContentPane().add(statusBar);
}}
當我更新與setCoordOfComponent(值),如果設置了最小和首選大小它的工作原理,但是當鼠標移動例如在JMenu的,JPanel的大小減少。
一個小型的自包含可執行示例將幫助很多 – 2011-03-10 12:53:25
太棒了!你介意包含一個'main'方法和一些初始化代碼嗎? – 2011-03-10 13:14:51
@BorisPavlović:這足夠嗎? – Heisenbug 2011-03-10 13:27:47