Reputation: 11
I am trying to write a program for my Computer Science
class that disables a JButton
after it's clicked on 8 times
. However, it disables right after I click on it once. I'm still not sure what I am doing wrong.
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);
}
}
}
}
Upvotes: 1
Views: 278
Reputation: 24626
Here I had modified your code a bit, please have a look, it disables the JButton
. Though how to bring it back to Enabled
state, for that, you need to either use another JButton
, or some sort of another event to bring it back to enabled state. Simply use a private int counter = 0
variable, that will count the button counts till 8 and then disable the 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.");
}
}
}
}
Upvotes: 1