For some reason I am not getting the same result from running the TwoButtons program K&B have on p. 379 of their "HeadFirst Java" book. When I run the app, I see no difference in the background of the "I'm a label" JLabel and the drawPanel. It is almost as if the line isn't working that reads: frame.getContentPane().add(BorderLayout.WEST, label);
Thanks for any hints as to what I might be doing wrong. I am a serious newbie at AWT/Swing, so I really appreciate it.
--Bob
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TwoButtons {
JFrame frame;
JLabel label;
public static void main (
String [] args) {
TwoButtons gui = new TwoButtons();
gui.go();
}
public void go() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton labelButton = new JButton("Change Label");
labelButton.addActionListener(new LabelListener());
JButton colorButton = new JButton("Change Circle");
colorButton.addActionListener(new ColorListener());
label = new JLabel("I'm a label");
MyDrawPanel drawPanel = new MyDrawPanel();
frame.getContentPane().add(BorderLayout.SOUTH, colorButton);
frame.getContentPane().add(BorderLayout.CENTER, drawPanel);
frame.getContentPane().add(BorderLayout.EAST, labelButton);
frame.getContentPane().add(BorderLayout.WEST, label);
frame.setSize(300,300);
frame.setVisible(true);
}
class LabelListener implements ActionListener {
public void actionPerformed (ActionEvent event) {
label.setText("Ouch");
//frame.repaint();
}
}
class ColorListener implements ActionListener {
public void actionPerformed (ActionEvent event) {
frame.repaint();
}
}
}
class MyDrawPanel extends JPanel {
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
int red = (int) (Math.random() * 255);
int green = (int) (Math.random() * 255);
int blue = (int) (Math.random() * 255);
Color startColor = new Color(red, green, blue);
red = (int) (Math.random() * 255);
green = (int) (Math.random() * 255);
blue = (int) (Math.random() * 255);
Color endColor = new Color(red, green, blue);
GradientPaint gradient = new GradientPaint(70,70,startColor, 150,150, endColor);
g2d.setPaint(gradient);
g2d.fillOval(70,70,100,100);
}
}
[ October 26, 2005: Message edited by: Bob Nedwor ]
[ October 26, 2005: Message edited by: Bob Nedwor ]