gpt4 book ai didi

java - 在 JButton OnclickListener 中编辑 JLabel 文本

转载 作者:行者123 更新时间:2023-12-02 03:51:42 24 4
gpt4 key购买 nike

我对编程相当陌生,最近我开始搞乱java中的JFrame。我正在创建一个程序,用户可以使用两个按钮滚动浏览图鉴。我有两个 JLabel,显示图鉴编号和图鉴名称。我遇到了障碍,无法根据在 onclick 监听器中编辑的字符串和整数来更改 jlabel 文本。请告诉我我做错了什么。

package window;

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;

public class RunWindow extends Canvas implements Runnable{

private static final long serialVersionUID = 1L;

public static final int WIDTH = 800;
public static final int HEIGHT = 600;
public static final int SCALE = 1;
public static final String NAME = "Pokedex";
public int pokedexNum = 1;
public String pokedexName = "Bulbasaur";
private JFrame frame;
private JPanel panel = new JPanel();
private JButton b1;
private JButton b2;
private boolean update = true;

JLabel label;
JLabel label2;
public boolean running = false;

public RunWindow(){
BufferedImage imgUP = null;
do {
switch(pokedexNum){
case 1:
pokedexName = "Bulbasaur";
break;
case 2:
pokedexName = "Ivysaur";
break;
case 3:
pokedexName = "Venisaur";
break;

}
label = new JLabel("Pokemon #"+pokedexNum+"-"+pokedexName);
label2 = new JLabel("#"+pokedexNum);
update = false;
}while (update);

label = new JLabel("Pokemon #"+pokedexNum+"-"+pokedexName);
label2 = new JLabel("#"+pokedexNum);

label.setLocation(50, 50);
frame = new JFrame(NAME);
b1 = new JButton("Up");
b2 = new JButton("Down");
frame.setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
frame.setPreferredSize(new Dimension(WIDTH*SCALE,HEIGHT*SCALE));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(this,BorderLayout.CENTER);
frame.setResizable(false);
frame.add(label);
frame.add(label2);
frame.add(panel);
frame.add(b1);
frame.add(b2);
frame.pack();
frame.setVisible(true);
frame.setLocationRelativeTo(null);

b1.setBounds(300, 400, 40, 20);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

System.out.println(pokedexNum);
if(pokedexNum <= 3){
pokedexNum++;
switch(pokedexNum){
case 1:
pokedexName = "Bulbasaur";
break;
case 2:
pokedexName = "Ivysaur";
break;
case 3:
pokedexName = "Venisaur";
break;

}
label = new JLabel("Pokemon #"+pokedexNum+"-"+pokedexName);
label2 = new JLabel("#"+pokedexNum);
update = false;
System.out.println(pokedexName);
label.setText("Pokemon #"+pokedexNum+"-"+pokedexName);
label2.setText("#"+pokedexNum);
label.setText(label.getText());

}
}

} );
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
pokedexNum--;
System.out.println("Down");
}
} );
b2.setBounds(300, 420, 40, 20);
b1.setHorizontalTextPosition(JButton.CENTER);
b1.setVerticalTextPosition(JButton.CENTER);
label.setBounds(300, 50, 200, 24);
label2.setBounds(340,400,80,20);
frame.add( label, BorderLayout.NORTH );
frame.add( panel , BorderLayout.CENTER);

}
public void run() {

}
public synchronized void start(){
running = true;
new Thread(this).start();

}
public void updatePokedex(){
do {
switch(pokedexNum){
case 1:
pokedexName = "Bulbasaur";
break;
case 2:
pokedexName = "Ivysaur";
break;
case 3:
pokedexName = "Venisaur";
break;

}
label = new JLabel("Pokemon #"+pokedexNum+"-"+pokedexName);
label2 = new JLabel("#"+pokedexNum);
update = false;
}while (update);
}
public synchronized void stop(){
running = false;
}
public static void main(String[] args){
new RunWindow().start();
}

}

最佳答案

我没有详细查看您的代码,但从我所看到的来看,您似乎创建了很多 JLabels,这可能是问题的原因,因为您最初创建的标签是您添加到 Swing 组件中并显示在 JFrame 中的那些。

您应该重用这些标签并简单地使用它们的 setText 方法来更改它们显示的文本。

关于java - 在 JButton OnclickListener 中编辑 JLabel 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35827389/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com