0

我想增加和减少JButton其焦点增益和丢失事件的大小。同时我想JLabel用 focussed 的文本来改变JButton

如果我不更改JLabel文本,我可以更改按钮的大小,但是当我同时更改标签时,大小JButton不会改变。

这是代码:

public class Main extends JFrame implements FocusListener {

    JButton b1, b2;
    JLabel lbl;
    private static final long serialVersionUID = 1L;

    public Main() {

        setSize(600, 600);//Size of JFrame
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);//Sets if its visible.

        JPanel panel = new JPanel();

        b1 = new JButton("Start");//The JButton name.
        b1.setRequestFocusEnabled(false);
        b1.addFocusListener(this);
        panel.add(b1);

        b2 = new JButton("End");//The JButton name.
        b2.setRequestFocusEnabled(false);
        b2.addFocusListener(this);

        panel.add(b2);

        add(panel, BorderLayout.CENTER);
        lbl = new JLabel("       ");
        add(lbl, BorderLayout.SOUTH);
    }

    public static void main(String[] args) {
        new Main();//Reads method main()
    }

    /*
    * What the button does.
    */
    @Override
    public void focusLost(FocusEvent ae) {

        if (ae.getSource() == b2) {
            b2.setSize(55, 26);
        } else if (ae.getSource() == b1) {
            b1.setSize(55, 26);
        }
    }

    @Override
    public void focusGained(FocusEvent ae) {

        if (ae.getSource() == b2) {
            lbl.setText("End");
            b2.setSize(55, 40);
        } else if (ae.getSource() == b1) {
            lbl.setText("Start");
            b1.setSize(55, 40);
        }
    }
}
4

2 回答 2

3

问题是您将绝对布局(或空布局)与隐式 LayoutManager 混合(JPanel 默认带有 FlowLayout)。

当您在 JLabel 上调用 setText(并且该文本发生更改)时,它会自动调用 revalidate,这最终会触发 LayoutManager 来布置组件。

要么使用 LayoutManager,可选地设置一些约束和 pref/min/max 大小(但不推荐后者)(并且你永远不会调用 setLocation/setSize/setBounds),或者使用 null-layout 并设置自己的位置和大小组件(在这种情况下,您必须称自己为 setSize/setLocation/setBounds)。

认真考虑阅读LayoutManager 教程。有一个专门的章节“没有布局管理器(绝对定位) ”。

于 2012-09-17T08:02:58.363 回答
0

我认为这就是您要实现的目标。

public class Main extends JFrame implements FocusListener {

JButton b1, b2;
JLabel lbl;
private static final long serialVersionUID = 1L;

public Main() {

    setSize(600, 600);// Size of JFrame
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);// Sets if its visible.

    JPanel panel = new JPanel();
    panel.setLayout(null);
    panel.setSize(this.getSize());
    b1 = new JButton("Start");// The JButton name.
    b1.setRequestFocusEnabled(false);
    b1.addFocusListener(this);
    b1.setLocation(10, 12);
    panel.add(b1);

    b2 = new JButton("End");// The JButton name.
    b2.setRequestFocusEnabled(false);
    b2.addFocusListener(this);
    b2.setLocation(70, 12);
    panel.add(b2);

    add(panel, BorderLayout.CENTER);
    lbl = new JLabel("       ");
    add(lbl, BorderLayout.SOUTH);
}

public static void main(String[] args) {
    new Main();// Reads method main()
}

/*
 * What the button does.
 */
@Override
public void focusLost(FocusEvent ae) {

    if (ae.getSource() == b2) {
        b2.setSize(55, 26);
    } else if (ae.getSource() == b1) {
        b1.setSize(55, 26);
    }

}

@Override
public void focusGained(FocusEvent ae) {

    if (ae.getSource() == b2) {
        lbl.setText("End");
        b2.setSize(55, 40);
    } else if (ae.getSource() == b1) {
        lbl.setText("Start");
        b1.setSize(55, 40);
    }

}
}
于 2012-09-17T08:36:27.490 回答