JLabel 中的文本在按下 JButton 时不会更新
Text in JLabel doesn't get updated on pressing a JButton
我正在做一个项目,但程序似乎有一个我找不到的错误。
这是重现问题的 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();
}
});
}
}
上面的程序有一个带有一些文本的 JLabel 和一个 JButton,它们都被添加到 JPanel 中,而 JPanel 又被添加到 JFrame 中。
按下按钮时,我希望更改 JLabel 中的文本。但是,尽管我每次按下按钮时都会执行 println
,但文本并没有改变。
这里有什么问题?
改变
label = new JLabel("Button is pressed");
至
label.setText("Button is pressed");
您不需要每次都创建和分配新标签time.just更改文本
您在单击按钮时创建了 JLabel
的新对象,但之后没有将其添加到 JPanel
或 JFrame
。
尽管创建了新对象,即
label = new JLabel("Button is pressed")
做类似的事情,
label.setText("Button is pressed");
您可以将该行更改为 label.setText("Button is pressed");使这项工作。
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();
}
});
}
}
我正在做一个项目,但程序似乎有一个我找不到的错误。
这是重现问题的 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();
}
});
}
}
上面的程序有一个带有一些文本的 JLabel 和一个 JButton,它们都被添加到 JPanel 中,而 JPanel 又被添加到 JFrame 中。
按下按钮时,我希望更改 JLabel 中的文本。但是,尽管我每次按下按钮时都会执行 println
,但文本并没有改变。
这里有什么问题?
改变
label = new JLabel("Button is pressed");
至
label.setText("Button is pressed");
您不需要每次都创建和分配新标签time.just更改文本
您在单击按钮时创建了 JLabel
的新对象,但之后没有将其添加到 JPanel
或 JFrame
。
尽管创建了新对象,即
label = new JLabel("Button is pressed")
做类似的事情,
label.setText("Button is pressed");
您可以将该行更改为 label.setText("Button is pressed");使这项工作。
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();
}
});
}
}