- JFrame is the class that handles windows in Java
- import 'javax.Swing.JFrame;
- creating a window is equivalent to creating an object of type JFrame
- JFrame has an extensive public interface
public void setSize(int width, int height)
public void setTitle(String title)
public JFrame(String title)
public void setVisible(boolean v)
public void add(Component x)
- notice that the program didn't end when the window closes by default
- default window close behaviour is to hide the window (but not exit the program)
- we have to handle the event where the user clicks close
public void setDefaultCloseOperation(int x)
- we can use
JFrame.HIDE_ON_CLOSE
JFrame.DO_NOTHING_ON_CLOSE
- These are given integer values (in order) 3,0,1
- do not use these values as Java might change those numbers in an update
Action Listeners and Action Events
- each component must register the listener that handles its events
- Java interface ActionListener
- event type is a class called ActionEvent
- a listener you define must implement the interface ActionListener
{
}
- Create the component
- JButton myButton = new JButton(" ");
- Write a listener class
- public void myListener implements ActionListener
{
public void actionPerformed(ActionEvent c)
{
}
} - Create an instance of the listener
- MyListener m = new myListener();
- register the listener with the component
- myButton.addActionListener(m)
- add the component to the JFrame
- myWindow.add(MyButton);
1 comment:
you guys already did GUI @_@, thats fast ...
Post a Comment