1

我正在尝试为我的班级编写一个程序,该程序在单击后Computer Science禁用 a 。但是,它在我单击一次后立即禁用。我仍然不确定我做错了什么。JButton8 times

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class JFrameDisableButton extends JFrame
{
    public static void main(String[] args)
    {
        JFrameDisableButton window = new JFrameDisableButton();
        window.setVisible(true);
    }

    final int WIDTH = 150;
    final int HEIGHT = 150;
    private Font bigFont = new Font("Arial", Font.BOLD, 16);
    private JButton disableButton = new JButton("Disable");
    private Container pane = getContentPane();
    private JLabel annoyed;

    public JFrameDisableButton()
    {
        super("Disable Frame");
        setSize(WIDTH,HEIGHT);
        setLayout(new FlowLayout());
        pane.add(disableButton);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        DisableButtonListener disableListener = new DisableButtonListener();
        disableButton.addActionListener(disableListener);
    }

    private class DisableButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent click)
        {
            if(actionPerformed(ActionEvent (click = 8)))
            {
                disableButton.setEnabled(false);
            }
            else
            {
                disableButton.setEnabled(true);
                annoyed = new JLabel("That's enough!");
                pane.add(annoyed);
                annoyed.setFont(bigFont);
            }
        }
    }
}
4

1 回答 1

1

在这里,我对您的代码进行了一些修改,请看一下,它禁用了JButton. 尽管如何将其恢复到Enabled状态,但为此,您需要使用 anotherJButton或其他某种事件将其恢复到启用状态。只需使用一个private int counter = 0变量,它将按钮计数计数到 8,然后禁用JButton.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class JFrameDisableButton extends JFrame
{
    public static void main(String[] args)
    {
        /*
         * Do learn about Concurrency in Swing too,
         * to display GUI related updates on the EDT.
         */
        EventQueue.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                JFrameDisableButton window = new JFrameDisableButton();
                window.setVisible(true);
            }
        });        
    }

    final int WIDTH = 150;
    final int HEIGHT = 150;
    private Font bigFont = new Font("Arial", Font.BOLD, 16);
    private JButton disableButton = new JButton("Disable");
    private Container pane = getContentPane();
    private JLabel annoyed;
    private int counter = 0;

    public JFrameDisableButton()
    {
        super("Disable Frame");

        setLayout(new FlowLayout());
        pane.add(disableButton);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        DisableButtonListener disableListener = new DisableButtonListener();
        disableButton.addActionListener(disableListener);
        annoyed = new JLabel("Clicked : " + counter + " times.");
        annoyed.setFont(bigFont);
        pane.add(annoyed);
        /*
         * Always call pack()/setSize() methods, only
         * when you are done adding components to 
         * the parent Container. Once it had realized it 
         * components, so that it can calculate, it''s 
         * size in a better way.
         */
        pack();
    }

    private class DisableButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent click)
        {
            counter++;
            if(counter == 8)
            {
                annoyed.setText("Clicked : " + counter + " times.");
                disableButton.setEnabled(false);
                counter = 0;
            }
            else
            {
                annoyed.setText("Clicked : " + counter + " times.");                                
            }
        }
    }
}
于 2013-02-03T05:38:42.197 回答