JLabel and swap the text it holds via the JLabel's setText(…) method

点点圈 提交于 2020-01-16 20:34:11

问题


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

回答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


来源:https://stackoverflow.com/questions/22977030/jlabel-and-swap-the-text-it-holds-via-the-jlabels-settext-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!