[英]Text in JLabel doesn't get updated on pressing a JButton
I am working on a project but the program seems to have a bug that I can not find. 我正在一个项目上,但是该程序似乎有一个我找不到的错误。
Here is an MCVE which reproduces the problem: 这是重现问题的MCVE :
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class SO{
JLabel label;
JButton button;
JPanel panel;
JFrame frame;
public static void main(String[] args){
new SO().start();
}
public void start()
{
label = new JLabel("Button not pressed");
button = new JButton("Press me");
frame = new JFrame();
panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
panel.add(label);
panel.add(button);
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.out.println("Button was pressed");
label = new JLabel("Button is pressed"); //Doesn't work
frame.repaint();
}
});
}
}
The above program has a JLabel with some text and a JButton both of which are added into a JPanel which in turn is added to a JFrame. 上面的程序有一个带有一些文本的JLabel和一个JButton,它们都被添加到JPanel中,而JPanel又被添加到JFrame中。
When the button is pressed, I want the text in the JLabel to change. 当按下按钮时,我希望更改JLabel中的文本。 But the text doesn't get changed despite the
println
executing every time I press the button. 但是,即使每次按下按钮都执行
println
,文本也不会改变。
What is the problem here? 这里有什么问题?
You are creating a new object of the JLabel
on clicking on the button but not adding it to the JPanel
or JFrame
after that. 单击该按钮时,您正在创建
JLabel
的新对象,但此后未将其添加到JPanel
或JFrame
。
In spite of creating new object ie 尽管创建了新对象,即
label = new JLabel("Button is pressed")
do something like, 做类似的事情,
label.setText("Button is pressed");
change 更改
label = new JLabel("Button is pressed");
to 至
label.setText("Button is pressed");
you don't need to create and assign new lable each time.just change the text 您无需每次都创建和分配新标签。只需更改文本
You can change that line to label.setText("Button is pressed"); 您可以将该行更改为label.setText(“按下按钮”); to make this work.
使这项工作。
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class SO{
JLabel label;
JButton button;
JPanel panel;
JFrame frame;
public static void main(String[] args){
new SO().start();
}
public void start()
{
label = new JLabel("Button not pressed");
button = new JButton("Press me");
frame = new JFrame();
panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
panel.add(label);
panel.add(button);
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.out.println("Button was pressed");
label.setText("Button is pressed"); //Doesn't work
frame.repaint();
}
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.