actionPerformed 之外的标签不刷新
label outside the actionPerformed does not refresh
我尝试实现这段代码,基本上它所做的是当我按下它唯一的按钮时,它在按钮上方的标签中递增一个单位。换句话说,它显示了用户按下按钮的次数。
但我的问题是,当我按下按钮时,我似乎无法刷新标签。保存数字 "clicks" 的变量递增,但在标签中它仍保留初始声明值 0。有人可以帮忙吗?
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ContadorCliques extends JFrame {
private TrataEvento trataEvento;
private JButton buttonClick;
private int clickCont = 0;
private JLabel l1;
public static void main(String[] args) {
ContadorCliques contador1 = new ContadorCliques("Hello!");
}
public ContadorCliques(String titulo) {
super(titulo);
Container c = getContentPane();
BorderLayout bl = new BorderLayout();
c.setLayout(bl);
l1=new JLabel(String.valueOf(clickCont));
JPanel pBotoes = criarPainelBotao();
c.add(pBotoes, BorderLayout.SOUTH);
//add(pBotoes, BorderLayout.SOUTH);
JPanel pCliques = criarPainelCliques();
c.add(pCliques, BorderLayout.CENTER);
//add(pCliques, BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 200);
setMinimumSize(new Dimension(getWidth(), getHeight()));
setLocationRelativeTo(null);
setVisible(true);
}
private JPanel criarPainelCliques() {
JLabel lbl = new JLabel("Número de Cliques: " + l1.getText());
JPanel p = new JPanel();
lbl.setFont(lbl.getFont().deriveFont(20.0f));
p.add(lbl, BorderLayout.CENTER);
return p;
}
private JPanel criarPainelBotao() {
trataEvento = new TrataEvento();
buttonClick = criarBotao();
JPanel p = new JPanel();
p.add(buttonClick);
return p;
}
private JButton criarBotao() {
JButton btn = new JButton("Click Here!");
btn.addActionListener(trataEvento);
return btn;
}
private class TrataEvento implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String obj = (String) e.getActionCommand();
if (obj.compareTo("Click Here!")==0) {
clickCont++;
System.out.println("Funciona");
System.out.println(clickCont);
l1.setText(String.valueOf(clickCont));
}
}
}
}
您永远不会将名为 l1
的 JLabel
添加到任何内容,因此永远不会显示其文本。我建议您通过将其添加到 GUI 来解决此问题。
尝试添加
l1.revalidate();
或
l1.repaint();
或在 setText()
之后按此顺序排列
将您的方法 criarPainelCliques() 更改为如下所示:
private JPanel criarPainelCliques() {
JLabel lbl = new JLabel("Número de Cliques: ");
JPanel p = new JPanel();
lbl.setFont(lbl.getFont().deriveFont(20.0f));
p.add(lbl, BorderLayout.CENTER);
p.add(l1);
return p;
}
目前您在这里只读取一次点击值:
JLabel lbl = new JLabel("Número de Cliques: " + l1.getText());
l1 再也不会被读取,因此您的点击计数器保持为 0。
我尝试实现这段代码,基本上它所做的是当我按下它唯一的按钮时,它在按钮上方的标签中递增一个单位。换句话说,它显示了用户按下按钮的次数。 但我的问题是,当我按下按钮时,我似乎无法刷新标签。保存数字 "clicks" 的变量递增,但在标签中它仍保留初始声明值 0。有人可以帮忙吗?
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ContadorCliques extends JFrame {
private TrataEvento trataEvento;
private JButton buttonClick;
private int clickCont = 0;
private JLabel l1;
public static void main(String[] args) {
ContadorCliques contador1 = new ContadorCliques("Hello!");
}
public ContadorCliques(String titulo) {
super(titulo);
Container c = getContentPane();
BorderLayout bl = new BorderLayout();
c.setLayout(bl);
l1=new JLabel(String.valueOf(clickCont));
JPanel pBotoes = criarPainelBotao();
c.add(pBotoes, BorderLayout.SOUTH);
//add(pBotoes, BorderLayout.SOUTH);
JPanel pCliques = criarPainelCliques();
c.add(pCliques, BorderLayout.CENTER);
//add(pCliques, BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 200);
setMinimumSize(new Dimension(getWidth(), getHeight()));
setLocationRelativeTo(null);
setVisible(true);
}
private JPanel criarPainelCliques() {
JLabel lbl = new JLabel("Número de Cliques: " + l1.getText());
JPanel p = new JPanel();
lbl.setFont(lbl.getFont().deriveFont(20.0f));
p.add(lbl, BorderLayout.CENTER);
return p;
}
private JPanel criarPainelBotao() {
trataEvento = new TrataEvento();
buttonClick = criarBotao();
JPanel p = new JPanel();
p.add(buttonClick);
return p;
}
private JButton criarBotao() {
JButton btn = new JButton("Click Here!");
btn.addActionListener(trataEvento);
return btn;
}
private class TrataEvento implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String obj = (String) e.getActionCommand();
if (obj.compareTo("Click Here!")==0) {
clickCont++;
System.out.println("Funciona");
System.out.println(clickCont);
l1.setText(String.valueOf(clickCont));
}
}
}
}
您永远不会将名为 l1
的 JLabel
添加到任何内容,因此永远不会显示其文本。我建议您通过将其添加到 GUI 来解决此问题。
尝试添加
l1.revalidate();
或
l1.repaint();
或在 setText()
之后按此顺序排列将您的方法 criarPainelCliques() 更改为如下所示:
private JPanel criarPainelCliques() {
JLabel lbl = new JLabel("Número de Cliques: ");
JPanel p = new JPanel();
lbl.setFont(lbl.getFont().deriveFont(20.0f));
p.add(lbl, BorderLayout.CENTER);
p.add(l1);
return p;
}
目前您在这里只读取一次点击值:
JLabel lbl = new JLabel("Número de Cliques: " + l1.getText());
l1 再也不会被读取,因此您的点击计数器保持为 0。