Now we can move on to some fun stuff. HelloJava3 brings us a new graphical interface component: JButton(Why isn’t it just called a Button? Button is the name that was used in Java’s original GUI toolkit, AWT. AWT had some significant shortcomings, so it was extended and essentially replaced by Swing in Java 1.2. Since AWT already took the reasonable names, such as Button and MenuBar, and mixing them in code could be confusing, Swing user interface component names start with J, such as JButton and JMenuBar.). In this example, we add a JButton component to our application that changes the color of our text each time the button is pressed. The draggable-message capability is still there, too. Our new code looks like this:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class HelloJava3
{
public static void main( String[] args ) {
JFrame frame = new JFrame( "HelloJava3" );
frame.add( new HelloComponent3("Hello, Java!") );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setSize( 300, 300 );
frame.setVisible( true );
}
}
class HelloComponent3 extends JComponent
implements MouseMotionListener, ActionListener
{
String theMessage;
int messageX = 125, messageY = 95; // Coordinates of the message
JButton theButton;
int colorIndex; // Current index into someColors
static Color[] someColors = {
Color.black, Color.red, Color.green, Color.blue, Color.magenta };
public HelloComponent3( String message ) {
theMessage = message;
theButton = new JButton("Change Color");
setLayout( new FlowLayout() );
add( theButton );
theButton.addActionListener( this );
addMouseMotionListener( this );
}
public void paintComponent( Graphics g ) {
g.drawString( theMessage, messageX, messageY );
}
public void mouseDragged( MouseEvent e ) {
messageX = e.getX();
messageY = e.getY();
repaint();
}
public void mouseMoved( MouseEvent e ) {}
public void actionPerformed( ActionEvent e ) {
// Did somebody push our button?
if (e.getSource() == theButton)
changeColor();
}
synchronized private void changeColor() {
// Change the index to the next color, awkwardly.
if (++colorIndex == someColors.length)
colorIndex = 0;
setForeground( currentColor() ); // Use the new color.
repaint();
}
synchronized private Color currentColor() {
return someColors[colorIndex];
}
}
Compile HelloJava3 in the same way as the other applications. Run the example, and
you should see the display shown in the figure below. Drag the text. Each time you press the
button, the color should change.
What have we added this time? Well, for starters, we have a new variable:
JButton theButton;
The theButton variable is of type JButton and is going to hold an instance of the
javax.swing.JButton class. The JButton class, as you might expect, represents a
graphical button, like other buttons in your windowing system.
Three additional lines in the constructor create the button and display it:
theButton = new JButton("Change Color");
setLayout( new FlowLayout() );
add( theButton );
In the first line, the new keyword creates an instance of the JButton class. The next line
affects the way our component will be used as a container to hold the button. It tells
HelloComponent3 how it should arrange components that are added to it for display—
in this case, to use a scheme called a FlowLayout (more on that coming up). Finally, it
adds the button to our component, just like we added HelloComponent3 to the content
pane of the JFrame in the main() method.
0 comments:
Post a Comment