在JButton OnclickListener中编辑JLabel文本

时间:2016-03-06 13:25:26

标签: java swing jframe jlabel

我对编程很新,最近我开始搞乱Java中的JFrame。我正在创建一个程序,用户可以使用两个按钮滚动浏览pokedex。我有两个JLabel,显示pokedex编号和pokedex名称。我遇到了一个障碍,无法根据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();
    }

}

2 个答案:

答案 0 :(得分:1)

我没有全面详细地查看您的代码,但是从我看到的情况来看,您似乎正在创建JLabel,这可能是问题的原因,因为您最初创建的标签是您添加到Swing组件中的那些组件,以及在JFrame中显示的组件。

您应该重复使用这些标签,只需使用他们的setText方法来更改他们正在显示的文本。

答案 1 :(得分:1)

您已经有2个Label变量而您只想更改文本,但每次都会创建新标签:

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"};

我在开头包含一个空字符串,以便数组的索引与pokedex中的数字相同(索引从1开始,所以说)。