- Feb 8, 2004
- 12,604
- 15
- 81
Im learning swing!
Ive made a simple program to add or decrement a number on screen, there are two JButtons, one JLabel and its all in a JFrame. First question is how come when i run this program and then close with the red X the process continues to run? Or at least netbeans shows it still running.
Heres the code:
package buttontest;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
*
* @author Max
*/
public class ButtonTest extends JFrame
{
int buttonClicks;
JButton plusButton;
JButton minusButton;
JButton cancelButton;
JLabel outputLabel;
public ButtonTest(String aTitle)
{
super(aTitle);
setSize(300,300);
plusButton = new JButton("Plus");
minusButton = new JButton("Minus");
outputLabel = new JLabel("Button clicks appear here");
add(outputLabel,"Center");
add(plusButton,"East");
add(minusButton,"West");
plusButton.addActionListener(new ButtonListener());
minusButton.addActionListener(new ButtonListener());
}
private class ButtonListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent anEvent)
{
if(anEvent.getSource().equals(plusButton))
{
buttonClicks++;
}
if(anEvent.getSource().equals(minusButton))
{
buttonClicks--;
}
outputLabel.setText("Button clicks = " + buttonClicks);
}
}
}
Second question is whats happening when a method is called with no receiver argument? Up until now ive seen anObject.methodA(); or static like SomeClass.methodB(); what happens when an instance method is called on its own like methodA(); Does it just automatically apply to the JFrame that this method is a part of? Is that why methods like setSize(x,y); work?
Heres the code:
package buttontest;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
*
* @author Max
*/
public class ButtonTest extends JFrame
{
int buttonClicks;
JButton plusButton;
JButton minusButton;
JButton cancelButton;
JLabel outputLabel;
public ButtonTest(String aTitle)
{
super(aTitle);
setSize(300,300);
plusButton = new JButton("Plus");
minusButton = new JButton("Minus");
outputLabel = new JLabel("Button clicks appear here");
add(outputLabel,"Center");
add(plusButton,"East");
add(minusButton,"West");
plusButton.addActionListener(new ButtonListener());
minusButton.addActionListener(new ButtonListener());
}
private class ButtonListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent anEvent)
{
if(anEvent.getSource().equals(plusButton))
{
buttonClicks++;
}
if(anEvent.getSource().equals(minusButton))
{
buttonClicks--;
}
outputLabel.setText("Button clicks = " + buttonClicks);
}
}
}
Second question is whats happening when a method is called with no receiver argument? Up until now ive seen anObject.methodA(); or static like SomeClass.methodB(); what happens when an instance method is called on its own like methodA(); Does it just automatically apply to the JFrame that this method is a part of? Is that why methods like setSize(x,y); work?
