making buttons
Hello. Trying to learn some graphics by making 2 frame classes I will call from my main. But my first class where I whant to make an quit button dosent compile. Any tips why?
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
// View class who creates an quit button for the JFrame
publicclass Viewextends JPanel{
private JButton quit;
// Create the Quit Button
public QuitButton(){
JButton quit =new JButton("Quit");
add(quit);
ActionListener listener =new clickListener();
quit.addActionListener(listener);
}
// Tell the JButton to quit the JFrame when pushed
publicclass clickListenerimplements ActionListener{
publicvoid actionPerformed(ActionEvent event){
System.exit(0);
}
}
}
// error: invalid method declaration; return type required (Line 12 - public QuitButton(){ )
And when I try to set "QuitButton" to "void" I磎 getting:
"cannot find symbol - class ActionListener" @ line 21 where I try to implement ActionListener.
[2031 byte] By [
Hunter78a] at [2007-11-27 2:56:26]

Ok. All my classes are done now and compiling just becouse I have put the all to be "void". And this leads to no results so apparently they need to have a return type as you said. Can we maybe look at my most simpel class who are supposed to draw the rectangle?(It compiles but I guess it dosent do anything with void as a type. If I change to "static" it says once again it need a return type. Any idea?)
import javax.swing.*;
import java.awt.*;
// View class who creates an rectangle for the JFrame
public class View extends JPanel{
private Rectangle rect;
// Creates the Rectangle and draw it
public void Rectangle(Graphics g){
Graphics2D g2 = (Graphics2D)g;
Rectangle rect = new Rectangle(5,5,200,200);
g2.draw(rect);
}
}
> Ok. All my classes are done now and compiling just
> becouse I have put the all to be "void". And this
> leads to no results so apparently they need to have a
> return type as you said. Can we maybe look at my most
> simpel class who are supposed to draw the
> rectangle?(It compiles but I guess it dosent do
> anything with void as a type. If I change to "static"
> it says once again it need a return type. Any idea?)
Do you call the Rectangle method on that class? If you don't, then it will never run, and nothing will be drawn. Perhaps you want to put that drawing code in the paintComponent() method, so that it will be drawn as part of the JPanel? If you want the Rectangle method to be treated as a constructor, then you have to give it the same name as the class:
public View() {// a constructor
}
public void Rectangle() {// a method
}
Painting: http://java.sun.com/docs/books/tutorial/uiswing/painting/index.html
Constructors: http://java.sun.com/docs/books/tutorial/java/javaOO/constructors.html