• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can any Plz find the mistake in swing program

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
reply
    Bookmark Topic Watch Topic
  • New Topic