JLabel中交换它通过JLabel的的setText保存文本(...)方法 [英] JLabel and swap the text it holds via the JLabel's setText(...) method

查看:443
本文介绍了JLabel中交换它通过JLabel的的setText保存文本(...)方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题领域应包含36个问题,回答这些问题是在网格中的36个按钮。

我有一个问题让问题字段中显示36的问题开始与什么是0 + 1,当用户点击相应的按钮就说明问题2示出了是什么是1 + 1的领域,因此,直到问题36

让我怎么得到一个JLabel的问题,并通过JLabel的的setText(...)交换其持有的文字法

这是我的code

 进口java.awt中的*。
java.awt.event中导入*。
进口java.awt.event.ActionListener;
进口的java.util。*;
进口的javax.swing *。
一流的NewClass {
    最终诠释行数= 6;
    最终诠释柱= 6;
    JButton的[]按钮=新的JButton [=包含]
    JLabel的statusLabel =新的JLabel(,JLabel.CENTER);
    java.util.List的<整数GT; buttonNumbers =新的ArrayList<整数GT;();
    INT buttonCounter = 1;
    公众的NewClass(){
        JPanel的buttonPanel =新JPanel(新网格布局(行,列));
        ButtonListener监听器=新ButtonListener(NewClass.this);
        为(中间体X = 0,Y = ROWS *柱; X&所述; Y; ​​X ++){
            按钮[X] =的新的JButton();
            按钮[X] .addActionListener(监听);
            buttonPanel.add(按钮[X]);
            buttonNumbers.add(新的整数(X + 1));
        }
        重置();
        JFrame的帧=新的JFrame();
        frame.getContentPane()加(statusLabel,BorderLayout.NORTH)。
        frame.getContentPane()加(buttonPanel,BorderLayout.CENTER)。
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(NULL);
        frame.setVisible(真);
    }
    公共无效复位(){
        Collections.shuffle(buttonNumbers);
        为(中间体X = 0,Y = ROWS *柱; X&所述; Y; ​​X ++){
            按钮[X] .setText(将String.valueOf(buttonNumbers.get(X)));
        }
        buttonCounter = 1;
        statusLabel.setText(什么是0+ 1+ buttonCounter);
    }
    公共静态无效的主要(字串[] args){
        新的NewClass();
    }
}
类ButtonListener实现的ActionListener {
    的NewClass贵;
    ButtonListener(的NewClass G){
        GUI =克;
    }
    公共无效的actionPerformed(ActionEvent的五){
        JButton的buttonClicked =(JButton的)e.getSource();
        INT clickedNumber =的Integer.parseInt(buttonClicked.getText());
        如果(clickedNumber == gui.buttonCounter){
            gui.buttonCounter ++;
            buttonClicked.setText(); //可选 - 清除正确选择
            如果(gui.buttonCounter> gui.ROWS * gui.COLUMNS)gui.reset();
            gui.statusLabel.setText(什么是0+ 1+ gui.buttonCounter);
        }
        其他{
            gui.reset();
            gui.statusLabel.setText(不正确的按钮点击后,再次启动:什么是0+ 1);
        }
    }
}


解决方案

更改code中的一行动作监听。更改此:

  gui.statusLabel.setText(什么是0+ 1+ gui.buttonCounter);

这样:

  gui.statusLabel.setText(是什么+ gui.buttonCounter ++ 1);

现在,它会问:


  • 什么是0+ 1

  • 什么是1 + 1

  • 什么是2 + 1

  • 什么是3 + 1

    ..........................................


  • 什么是1937655087345+ 1

The question field should contain 36 questions, the answer to these question are on the 36 buttons in the grid.

i am having a problem getting question field to display 36 question starting with what is 0 + 1 when the user clicks the correct button it then shows question 2 shows in the field which is what is 1+1 and so till question 36

so how do i get one question JLabel and swap the text it holds via the JLabel's setText(...) method

here is my code

import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.*;
class NewClass {
    final int ROWS = 6;
    final int COLUMNS = 6;
    JButton[] buttons = new JButton[ROWS*COLUMNS];
    JLabel statusLabel = new JLabel("",JLabel.CENTER);
    java.util.List<Integer> buttonNumbers = new ArrayList<Integer>();
    int buttonCounter = 1;
    public NewClass()  {
        JPanel buttonPanel = new JPanel(new GridLayout(ROWS,COLUMNS));
        ButtonListener listener = new ButtonListener(NewClass.this);
        for(int x = 0, y = ROWS*COLUMNS; x < y; x++){
            buttons[x] = new JButton();
            buttons[x].addActionListener(listener);
            buttonPanel.add(buttons[x]);
            buttonNumbers.add(new Integer(x+1));
        }
        reset();
        JFrame frame = new JFrame();
        frame.getContentPane().add(statusLabel,BorderLayout.NORTH);
        frame.getContentPane().add(buttonPanel,BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
    public void reset(){
        Collections.shuffle(buttonNumbers);
        for(int x = 0, y = ROWS*COLUMNS; x < y; x++){
            buttons[x].setText(String.valueOf(buttonNumbers.get(x)));
        }
        buttonCounter = 1;
        statusLabel.setText("what is 0+ 1  " + buttonCounter);
    }
    public static void main(String[] args) {
        new NewClass();
    }
}
class ButtonListener implements ActionListener {
    NewClass gui;
    ButtonListener(NewClass g){
        gui = g;
    }
    public void actionPerformed(ActionEvent e) {
        JButton buttonClicked = (JButton)e.getSource();
        int clickedNumber = Integer.parseInt(buttonClicked.getText());
        if(clickedNumber == gui.buttonCounter){
            gui.buttonCounter++;
            buttonClicked.setText("");//optional - clears correct selection
            if(gui.buttonCounter > gui.ROWS*gui.COLUMNS) gui.reset();
            gui.statusLabel.setText("what is 0+ 1" + gui.buttonCounter);
        }
        else {
            gui.reset();
            gui.statusLabel.setText("Incorrect button clicked, start again: what is 0+ 1");
        }
    }
}

解决方案

Change one line of code in the action listener. Change this:

gui.statusLabel.setText("what is 0+ 1" + gui.buttonCounter);

to this:

gui.statusLabel.setText("what is " + gui.buttonCounter + "+ 1");

Now it will ask:

  • what is 0+ 1
  • what is 1+ 1
  • what is 2+ 1
  • what is 3+ 1

    ..........................................

  • what is 1937655087345+ 1

这篇关于JLabel中交换它通过JLabel的的setText保存文本(...)方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆