重用JLabel连续显示文本

时间:2018-01-20 17:10:41

标签: java jlabel

我想要显示文字。当您单击该文本时,它应该转到text2,当您单击它时,它应该转到text3,依此类推。我的代码的问题是它总是直接带我到最后一个文本。你们能帮忙吗?甚至我的老师都和我坐在一起,想不出办法。下面是代码:

package test1;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JLabel;

public class App {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        JFrame j1 = new JFrame("Game");
        JLabel l1 = new JLabel();
        int count = 1;
        boolean chapterOver = false;

        j1.setSize(500, 500);
        j1.setVisible(true);
        j1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        j1.add(l1);
        l1.setText("text 1");
        l1.setHorizontalAlignment(JLabel.CENTER);

        while (chapterOver == false) {
            if (count == 1) {
                l1.addMouseListener(new MouseListener() {

                    @Override
                    public void mouseReleased(MouseEvent e) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void mousePressed(MouseEvent e) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void mouseExited(MouseEvent e) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void mouseEntered(MouseEvent e) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void mouseClicked(MouseEvent e) {
                        // TODO Auto-generated method stub
                        l1.setText("text2");
                    }
                });
                count++;
            }

            if (count == 2) {
                l1.addMouseListener(new MouseListener() {

                    @Override
                    public void mouseReleased(MouseEvent e) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void mousePressed(MouseEvent e) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void mouseExited(MouseEvent e) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void mouseEntered(MouseEvent e) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void mouseClicked(MouseEvent e) {
                        // TODO Auto-generated method stub
                        l1.setText("text3");
                    }
                });
                count++;
            }

        }

    }

}

2 个答案:

答案 0 :(得分:1)

这是一个好的解决方案:

static int count = 1;
public static void main(String[] args) {
    /*
    When user clicks, it tiggers an event which tiggers  `setTextCount`
    */

    l1.addMouseListener(new MouseListener() {

        @Override
        public void mouseClicked(MouseEvent e) {


            setTextCount(l1);

        }
    });


}

public static void setTextCount(javax.swing.JLabel label){
    label.setText("text "+ (count + 1));
    count = (count + 1 )%3;
}

看起来你在UI上仍然有点混乱。 UI编程是控制的反转。因此,当用户点击某些内容时,UI会更改程序的状态。我的意思是,你为什么要添加while循环?

答案 1 :(得分:0)

  

我知道如何使用调试器。

要么这是谎言,要么在执行时不理解你的程序。

您的调试器会显示:

            count++;
        }
        // count = 2 in first iteration, therefore following if never fails
        if (count == 2) {
        // this means you always _immediately_ add the second MouseListener Listener to the l1

如果你在两个MouseListener方法中放置断点,你会发现实际上两个断点都是在点击标签后运行的。

初学者不应该随机选择Swing组件。使用JLable制作MouseListener 有效是一种不好的开始方式。

我建议您(和您的老师)按照官方Java教程重新启动,了解如何创建GUI:https://docs.oracle.com/javase/tutorial/uiswing/index.html