在 JButton OnclickListener 中编辑 JLabel 文本
Edit JLabel Text in JButton OnclickListener
我对编程还很陌生,最近我开始弄乱 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();
}
}
我没有详细查看您的代码,但据我所见,您似乎创建了很多 JLabel,这可能是问题的原因,因为您最初创建的标签是您添加到 Swing 组件中并显示在 JFrame 中的组件。
您应该重复使用这些标签并简单地使用它们的 setText 方法来更改它们显示的文本。
您已经有 2 个标签变量,您只想更改文本,但每次都创建新标签:
label = new JLabel("Pokemon #"+pokedexNum+"-"+pokedexName);
相反,你应该这样做:
label.setText("....");
无论如何你稍后会做一些台词。问题是您永远不会将新创建的标签添加到框架中(您在开始时添加它们,但是当您覆盖 "label" 和 "label2" 时,这些是新标签,您还需要添加它们到您的框架,每次单击按钮时都会添加 2 个标签)
只需删除:
label = new JLabel("Pokemon #"+pokedexNum+"-"+pokedexName);
label2 = new JLabel("#"+pokedexNum);
我还建议您不要使用 switch 语句,您应该使用字符串数组并调用
setText(pokemonNames[pokedexNum]);
这样您就不需要为不同的 pokedex 案例编写数百个代码行。
您可以像这样轻松地创建一个 String[]:
private static String[] pokeNames = new String[]{"","Bulbasaur","Ivysaur"};
我在开头包含了一个空字符串,以便数组的索引与图鉴中的数字相同(可以说索引从 1 开始)。
我对编程还很陌生,最近我开始弄乱 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();
}
}
我没有详细查看您的代码,但据我所见,您似乎创建了很多 JLabel,这可能是问题的原因,因为您最初创建的标签是您添加到 Swing 组件中并显示在 JFrame 中的组件。
您应该重复使用这些标签并简单地使用它们的 setText 方法来更改它们显示的文本。
您已经有 2 个标签变量,您只想更改文本,但每次都创建新标签:
label = new JLabel("Pokemon #"+pokedexNum+"-"+pokedexName);
相反,你应该这样做:
label.setText("....");
无论如何你稍后会做一些台词。问题是您永远不会将新创建的标签添加到框架中(您在开始时添加它们,但是当您覆盖 "label" 和 "label2" 时,这些是新标签,您还需要添加它们到您的框架,每次单击按钮时都会添加 2 个标签)
只需删除:
label = new JLabel("Pokemon #"+pokedexNum+"-"+pokedexName);
label2 = new JLabel("#"+pokedexNum);
我还建议您不要使用 switch 语句,您应该使用字符串数组并调用
setText(pokemonNames[pokedexNum]);
这样您就不需要为不同的 pokedex 案例编写数百个代码行。
您可以像这样轻松地创建一个 String[]:
private static String[] pokeNames = new String[]{"","Bulbasaur","Ivysaur"};
我在开头包含了一个空字符串,以便数组的索引与图鉴中的数字相同(可以说索引从 1 开始)。