2011-03-10 23 views
2

我有一些麻煩設置插入到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的大小減少。

+0

一個小型的自包含可執行示例將幫助很多 – 2011-03-10 12:53:25

+0

太棒了!你介意包含一個'main'方法和一些初始化代碼嗎? – 2011-03-10 13:14:51

+0

@BorisPavlović:這足夠嗎? – Heisenbug 2011-03-10 13:27:47

回答

0

您的代碼很好,但您忘記將組件添加到工具欄。

在工具欄構造

add(areaCoordX); 
add(areaCoordY); 

添加這些行看起來應該是現在這個樣子

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(""); 
    add(areaCoordX); 
    add(areaCoordY); 
    } 
public void setCoordOfComponent(Point c){ 
     this.areaCoordX.setText(""+c.x); 
     this.areaCoordY.setText(""+c.y); 
    } 
} 

我建議加上的標籤一些文本看他們。

+0

抱歉..我忘了..現在的代碼應該是相當完整的 – Heisenbug 2011-03-10 13:32:53

1

我無法讓它與JLabel一起工作,但如果您願意使用JTextField來替代,您可以創建具有固定寬度(列數)的那些。這裏是我的工作示例,使用兩側的按鈕來顯示工具欄不會調整大小。

import java.awt.BorderLayout; 
import java.awt.Component; 
import java.awt.Point; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseMotionAdapter; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JTextField; 
import javax.swing.JToolBar; 

public class EditorStatusBar extends JToolBar { 
    private final JTextField coords; 

    public EditorStatusBar(Component parent) { 
     super(); 

     setFloatable(false); 

     add(new JButton("Button 1")); 
     addSeparator(); 

     coords = new JTextField(5); 
     coords.setEditable(false); 
     coords.setHorizontalAlignment(JTextField.CENTER); 
     add(coords); 

     addSeparator(); 
     add(new JButton("Button 2")); 

     parent.addMouseMotionListener(new MouseMotionAdapter() { 
      @Override 
      public void mouseMoved(MouseEvent e) { 
       Point p = e.getPoint(); 
       coords.setText(p.x + ":" + p.y); 
      } 
     }); 
    } 
} 

class Gui extends JFrame { 
    public static void main(String[] args) { 
     Gui gui = new Gui(); 
     gui.setVisible(true); 
    } 

    public Gui() { 
     super(); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setLayout(new BorderLayout()); 
     add(new EditorStatusBar(this), BorderLayout.NORTH); 
     add(new JLabel("Content", JLabel.CENTER), BorderLayout.CENTER); 
     pack(); 
    } 
}
+0

謝謝..這不是我想要產生的效果,但有效地消除了令人討厭的調整大小效果 – Heisenbug 2011-03-14 09:17:27

1

我可以用下面的代碼來做到這一點。要修復組件的大小,我必須使用setPreferredSizesetMaximumSizesetMinimumSize中的每一個。

/** 
* Insert a component in the toolbar and specify its width. 
* 
* @param component 
*/ 
protected void addToolbarItem(JLabel component, int width) 
{ 
    addToolbarItem(component); 
    final Dimension dim = new Dimension(width, toolBar.getHeight()); 
    component.setPreferredSize(dim); 
    component.setMinimumSize(dim); 
    component.setMaximumSize(dim); 
    component.setBorder(new LineBorder(Color.BLACK)); 
}