Hi, I m learning
java my self.. right now I am doing swings.... I tried to write a simple program to handle one event.
The program is to change the panel color when i click bottom button and the label value should change when i click east side button. I feel some thing wrong is there in actionPerformed method or in MyDrawPanel class..
I am not getting any exception also..
I m trying to resolve this problem from past 3 days. Can any one correct the program.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class SimpleGUI1 implements ActionListener {
JButton colorbutton, labelbutton;
JLabel label;
JFrame frame;
public static void main(
String[] args) {
// TODO Auto-generated method stub
javax.swing.SwingUtilities.invokeLater(new Runnable(){public void run() {SimpleGUI1 gui = new SimpleGUI1();gui.go();}});
}
public void go(){
JFrame.setDefaultLookAndFeelDecorated(true);
frame = new JFrame("SimpleGUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
colorbutton = new JButton("Change Colors");
labelbutton = new JButton("Change Label");
label = new JLabel("Hello World");
colorbutton.addActionListener(this);
MyDrawPanel drawPanel = new MyDrawPanel();
frame.getContentPane().add(BorderLayout.SOUTH,colorbutton);
frame.getContentPane().add(BorderLayout.EAST,labelbutton);
frame.getContentPane().add(BorderLayout.WEST,label);
frame.getContentPane().add(BorderLayout.CENTER,drawPanel);
frame.setSize(300,300);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent event){
if (event.getSource()== colorbutton){
colorbutton.setText("I've been clicked");
frame.repaint();
}
else{
labelbutton.setText("I've been clicked");
label.setText("That Hurt");
}
}
}
class MyDrawPanel extends JPanel{
public void PaintComponent(Graphics g){
g.fillRect(0,0,50,50);
int red = (int)(Math.random() * 255);
int green =(int)(Math.random() * 255);
int blue =(int)(Math.random() * 255);
Color randomcolor = new Color(red,green,blue);
g.setColor(randomcolor);
g.fillOval(70,70,100,100);
}
}
[ September 02, 2007: Message edited by: Shyam Kumar Kotha ]
[ September 02, 2007: Message edited by: Shyam Kumar Kotha ]