• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can any Plz find the mistake in swing program  RSS feed

 
Shyam Kumar Kotha
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what's it not doing that it's supposed to do?

guesses
1)
public void PaintComponent(Graphics g){//is mis-spelled s/be paintComponent
2)
MyDrawPanel has no components added, therefore has no size
//in constructor add setPreferredSize(new Dimension(whatever,whatever));
3)
else{
labelbutton.setText("I've been clicked");
//actionListener only added to colorbutton, therefore will never get to 'else'

anything else not working?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!