在JToolbar中设置JLabel的大小

时间:2011-03-10 12:47:30

标签: java swing

我有一些麻烦设置插入JToolbar的JLabel组件的大小。我使用JLabel来显示实际的鼠标坐标,但是根据数字的长度,JLabel会改变它的大小,从而产生我的JToolbar组件的一些讨厌的动作。

我正在玩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大小减小。

3 个答案:

答案 0 :(得分: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();
    }
}

答案 1 :(得分: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));
}

答案 2 :(得分: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);
    }
}

我建议在标签中添加一些文字以查看它们。