我希望 fill() 方法在按下“重试”按钮时再次运行。但是当我按下按钮时没有任何反应?我能做什么?

问题描述 投票:0回答:0

这部分主要是我声明面板、标签和其他东西,比如进度条、滑块

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

public class Mari extends JFrame implements ChangeListener, ActionListener {
    JProgressBar bar;
    JSlider slider;
    JPanel panel;
    JLabel label1;
    JLabel label2;
    JLabel label3;
    JButton button;
    Random random;
    JTextField textfield;
    int number;
    Mari() throws InterruptedException {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setPreferredSize(new Dimension(600, 600));
        // COMPONENTS
        label1 = new JLabel("Choose the right number: ");
        label1.setPreferredSize(new Dimension(100,30 ));
        label1.setFont(new Font("Times New Roman", Font.PLAIN, 10));
        label1.setLayout(null);




        label2 = new JLabel("*Random Number*");
        label2.setPreferredSize(new Dimension(300, 40));
        label2.setFont(new Font("Times New Roman", Font.BOLD, 20));
        label2.setLayout(null);


        label3 = new JLabel("*current temp*");
        label3.setPreferredSize(new Dimension(100, 25));
        label3.setLayout(null);

        // slider
        slider = new JSlider(0, 100);
        slider.setSize(500, 100);
        slider.setLayout(null);
        slider.setOrientation(SwingConstants.HORIZONTAL);
        slider.setPaintTicks(true);
        slider.setMinorTickSpacing(10);
        slider.setPaintTrack(true);
        slider.setMajorTickSpacing(20);
        slider.setPaintLabels(true);
        slider.addChangeListener((ChangeListener) this);
        randomNumber();
        //textfield
        textfield = new JTextField();
        textfield.setPreferredSize(new Dimension(100, 30));
        textfield.setLayout(null);

        //Progressbar
        bar = new JProgressBar(0, 100);
        bar.setBounds(0, 0, 600, 100);
        bar.setValue(0);
        bar.setStringPainted(true);
        bar.setForeground(Color.orange);
        bar.setBackground(Color.LIGHT_GRAY);

        label3.setText("current temperature: "+slider.getValue());
        //button
        button = new JButton("Retry");
        button.setSize(new Dimension(50,30));
        button.setLayout(null);
        button.setFocusable(false);
        button.addActionListener(this);

        // panel
        panel = new JPanel();
        panel.setSize(new Dimension(600, 600));
        panel.setLayout(new GridLayout(10, 1));
        panel.add(label1);
        panel.add(label2);
        panel.add(slider);
        panel.add(label3);
        panel.add(textfield);
        panel.add(bar);
        panel.add(button);



        this.add(panel);
        this.setLayout(new FlowLayout());
        this.pack();
        this.setVisible(true);
        fill();


    }
    public void randomNumber(){
        random = new Random();
        number = random.nextInt(slider.getMaximum()- slider.getMinimum())+slider.getMinimum();
        label2.setText("Needed temperature: "+String.valueOf(number));

    }


这里我创建了 fill() 方法

    public void fill() throws InterruptedException {
        int counter = 0;
        boolean b;
        while(counter<100){
            if (slider.getValue()!=number) {
                bar.setValue(counter);
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }counter += 1;
            } else if (!textfield.getText().equals(String.valueOf(number))) {
                bar.setValue(counter);
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }counter += 1;
            }
            else{
                bar.setForeground(Color.green);
                bar.setString("Well done!");
                bar.setValue(100);

            }
        }

        bar.setForeground(Color.red);
        bar.setString("You failed");
    }



覆盖滑块功能



    @Override
    public void stateChanged(ChangeEvent e) {
        label3.setText("temperature: "+slider.getValue());
        if(slider.getValue()==number){
            slider.setEnabled(false);
        }

下面的代码是我为按钮创建函数的部分(想要)Fill() 方法再次运行

 }
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == button) {
            slider.setEnabled(true);  // re-enable the slider
            textfield.setText("");  // clear the text field
            randomNumber();  // generate a new random number
            bar.setValue(0);  // reset the progress bar
            bar.setForeground(Color.GREEN);  // reset the color of the progress bar
            bar.setString("0%");  // reset the text of the progress bar
            try {
                fill();  // start the game again
            } catch (InterruptedException ex) {
                throw new RuntimeException(ex);
            }
        }
    }



}

我是初学者,所以我遇到了困难。如果你能帮助我,我将不胜感激

button methods program-entry-point jprogressbar
© www.soinside.com 2019 - 2024. All rights reserved.