1

をクリックしComputer Scienceた後に を無効にするクラス用のプログラムを作成しようとしています。ただし、一度クリックするとすぐに無効になります。私はまだ何が間違っているのか分かりません。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状態に戻す方法ですが、そのためには、別のJButton、または何らかの別のイベントを使用して、有効な状態に戻す必要があります。変数を使用するだけ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 に答える