Java 更改JButton的大小以及JButton的焦点增益和丢失事件上标签的文本
我想在焦点增益和丢失事件上增加和减少Java 更改JButton的大小以及JButton的焦点增益和丢失事件上标签的文本,java,swing,focus,jbutton,jlabel,Java,Swing,Focus,Jbutton,Jlabel,我想在焦点增益和丢失事件上增加和减少JButton的大小。同时,我想用聚焦的JButton文本更改JLabel 如果我没有更改JLabel文本,我可以更改按钮的大小,但是当我同时更改标签时,JButton的大小不会更改 代码如下: public class Main extends JFrame implements FocusListener { JButton b1, b2; JLabel lbl; private static final long serialV
JButton
的大小。同时,我想用聚焦的JButton
文本更改JLabel
如果我没有更改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);
}
}
}
问题是您将绝对布局(或空布局)与隐式布局管理器(默认情况下JPanel附带FlowLayout)混合在一起 当您在JLabel上调用setText(并且该文本发生更改)时,它会自动调用revalidate,这将最终触发LayoutManager来布局组件 使用LayoutManager,可以选择设置一些约束和pref/min/max size(但不建议使用后者)(并且您从不调用setLocation/setSize/setBounds),或者使用null layout并自行设置组件的位置和大小(在这种情况下,您必须调用setSize/setLocation/setBounds)
认真考虑阅读。有一章专门介绍“没有布局管理器(绝对定位)”。问题在于,您将绝对布局(或空布局)与隐式布局管理器(JPanel默认带有FlowLayout)混合在一起 当您在JLabel上调用setText(并且该文本发生更改)时,它会自动调用revalidate,这将最终触发LayoutManager来布局组件 使用LayoutManager,可以选择设置一些约束和pref/min/max size(但不建议使用后者)(并且您从不调用setLocation/setSize/setBounds),或者使用null layout并自行设置组件的位置和大小(在这种情况下,您必须调用setSize/setLocation/setBounds)
认真考虑阅读。有一个专门的章节是关于“没有布局管理器(绝对定位)”的。我认为这就是您试图实现的目标
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);
}
}
}
我想这就是你想要达到的目标
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);
}
}
}