myListener m = new myListener;
myButton.addActionListener(m);
-------------------------------
/* can be replaced with below */
-------------------------------
myButton.addActionListener(new myListener());
public myJFrame()
{
super("title of Window");
.
.
.
JButton myButton = new JButton("title of button");
myButton.addActionListener( new myListener());
}
What happens when you have multiple buttons?
- Bad way
- define different listener classes for each button
- register the appropriate class to the appropriate button
- Good way
- examine e
- object of type ActionEvent
- ActionEvent has a method returning the label of the button pressed
public String getActionCommand()
/* Andrew drew a picture of two buttons labeled different things then pointed out that when you click the button "one" e.getActionCommand returns "One"*/
- These implement the interface LayoutManager
- BorderLayout
- FlowLayout
- GridLayout
- A window (JFrame) is a container
- allowed to contain objects and have layouts
- a window can be divided into regions using LayoutManager
- each region can be made into a container by declaring it a JPanel
- a panel is a Container
- Declare a LayoutManager
- setLayout(new BorderLayout());
- Add a apanel to as many regions as you want
- JPanel myPanel = new JPanel();
- add(MyPanel, BorderLayout, North);
- Declare a layout to the panel
- myPanel.SetLayout(new.FlowLayout());
No comments:
Post a Comment