Results 1 to 5 of 5
Thread: problem for games
- 05-11-2007, 07:18 PM #1
Member
- Join Date
- May 2007
- Posts
- 2
- Rep Power
- 0
problem for games
i am trying to make a game .Now i am starting form game menu,but it isn't work.
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class GameMenu extends JFrame implements MouseListener
{
ImageIcon icon;
Image image;
ImageIcon icon2;
JList menulist;
GameMenu frame;
public GameMenu(){
//Create background
icon = new ImageIcon("image/bgimg.jpg");
icon2 = new ImageIcon("image/002.jpg");
JPanel panel = new JPanel(new GridBagLayout())
{
protected void paintComponent(Graphics g)
{
// Dispaly image at at full size
g.drawImage(icon.getImage(), 0, 0, null);
g.drawImage(icon2.getImage(), 0, 10, null);
super.paintComponent(g);
}
};
GridBagConstraints a = new GridBagConstraints();
//create menulist
//Feild
String[] gameMenulist = {"New Game","Abouts","how to play","Options","Exit"};
menulist= new JList(gameMenulist);
ListSelectionModel menulistModel = menulist.getSelectionModel();
//menulist setting
menulist.setSelectionMode(ListSelectionModel.SINGL E_INTERVAL_SELECTION);
menulist.setLayoutOrientation(JList.HORIZONTAL_WRA P);
menulist.setVisibleRowCount(-1);
menulist.setBackground(Color.BLACK);
menulist.setForeground(Color.WHITE);
menulist.addMouseListener(this);
panel.add(menulist, a);
panel.setOpaque( false );
panel.setPreferredSize( new Dimension(400, 400) );
getContentPane().add(panel);
}
public void mouseClicked(MouseEvent e) {
switch(menulist.getSelectedIndex()){
case 0:
frame.setSize(300,300);
case 1:
frame.setSize(600,600);
}
}
public void createGUI(){
frame = new GameMenu();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setSize(600, 600);
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}
public static void main(String [] args){
createGUI();
}
}
- 05-11-2007, 08:53 PM #2
Member
- Join Date
- Apr 2007
- Location
- Indiana
- Posts
- 83
- Rep Power
- 0
It does not even compile. Are you not getting a compilation error when you start it?
In main you call createGui() and I am seeing this error:
Cannot make a static reference to non-static method createGui().
Instantiate the class in main and in the constructor call createGui();
- 05-11-2007, 08:57 PM #3
Member
- Join Date
- Apr 2007
- Location
- Indiana
- Posts
- 83
- Rep Power
- 0
Also there are unimplemented methods from the MouseListener.
- 05-11-2007, 09:16 PM #4levent Guest
Try making createUI method static.
Instead of:
Java Code:public void createGUI(){ ...
write
Java Code:public static void createGUI(){ ...
And as Pegitha mentioned, you should implement all methods of MouseListener.
Add following lines somewhere inside class to do that:
Java Code:void mouseClicked(MouseEvent e) {}; void mouseEntered(MouseEvent e) {}; void mouseExited(MouseEvent e) {}; void mousePressed(MouseEvent e) {}; void mouseReleased(MouseEvent e) {};
It should work in that way.Last edited by levent; 05-11-2007 at 09:20 PM.
- 12-25-2007, 08:42 AM #5
Granted this thread is mighty old, but I figured I'd post in case anyone was looking
for it for whatever reason.
The following above methods need the keyword public in front of each method's void return type. Thus:
Also, make sure the frame variable is now static as a result:Java Code:public void mouseClicked(MouseEvent e) {}; // <-- Already implemented public void mouseEntered(MouseEvent e) {}; public void mouseExited(MouseEvent e) {}; public void mousePressed(MouseEvent e) {}; public void mouseReleased(MouseEvent e) {};
And watch out for the general spelling errors too that caused some compilation errors when I first ran it.Java Code:static GameMenu frame;
Similar Threads
-
Browser-based games. Where do I start?
By Brute in forum EclipseReplies: 1Last Post: 08-09-2007, 07:35 PM -
OS 10.4 - Help with Java in Yahoo! Games - Fonts Unreadable?
By acleme1 in forum Java AppletsReplies: 3Last Post: 07-16-2007, 11:22 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks