Results 1 to 3 of 3
Thread: Open JFrame
- 12-01-2009, 06:28 PM #1
Member
- Join Date
- Mar 2009
- Posts
- 13
- Rep Power
- 0
Open JFrame
Hi,
I got a problem. I want my program too start with Text and a start button. When the start button is called it should open a new JFrame with the game. Here is the main class:
Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; public class GregerSnake extends JFrame{ private Board field = new Board(); private JButton[] button = new JButton[4]; private String[] buttonText ={"New Game","Menu","Com", "Cancel"}; private JPanel u; private JLabel o, p; //o är titeln public GregerSnake(){ u = new JPanel(); u.setBackground(Color.lightGray); o = new JLabel("GregerSnake"); o.setFont(new Font("Arial", Font.BOLD, 40)); u.add(o); MyListener myL = new MyListener(); for(int i=0;i<button.length;i++){ button[i] = new JButton(); button[i].setText(buttonText[i]); button[i].addActionListener(myL); u.add(button[i]); } Container c = getContentPane(); c.add(u, BorderLayout.NORTH); c.add(field, BorderLayout.CENTER); field.startGame(); } class MyListener implements ActionListener{ public void actionPerformed(ActionEvent ae){ field.requestFocus(); if(ae.getSource() == button[0]){ field.newGame(); } else if(ae.getSource() == button[1]){ // field.startMenu(); } else if(ae.getSource() == button[2]){ field.killCom(); } else if(ae.getSource() == button[3]){ System.exit(0); } } } public static void main(String[] args) { GregerSnake gt = new GregerSnake(); gt.setVisible(true); gt.setSize(586, 585); gt.setTitle("GregerSnake"); gt.setDefaultCloseOperation(EXIT_ON_CLOSE); gt.setResizable(false); gt.setLocationRelativeTo(null); } }
-
Rather than open a new Window, why not swap JPanels with a CardLayout and show your game in the center of the current JFrame?
- 12-01-2009, 09:03 PM #3
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Why don't you take a look at the games that come installed on you OS. I know Windows comes with many games, like Hearts, Solitaire etc. Look at the UI of those games and design you game based on that. I'm sure those game designers know more than you or I do, so follow there design.I want my program too start with Text and a start button. When the start button is called it should open a new JFrame with the game.
The basic design that I've noticed is to use Menus to start/stop/configure a game.
Similar Threads
-
Passing data from one JFrame to another JFrame
By tarami in forum New To JavaReplies: 3Last Post: 08-06-2009, 05:44 PM -
How to make a Jframe un-focusable when another Jframe is active?
By Robert_85 in forum Advanced JavaReplies: 4Last Post: 04-22-2009, 11:02 PM -
Different Files Open in File -> Open Workspace
By mgm2010 in forum JCreatorReplies: 0Last Post: 04-11-2009, 02:14 PM -
Can't open netbeans 6.1
By hisouka in forum NetBeansReplies: 3Last Post: 09-09-2008, 11:04 AM -
How to close an open JFrame window from a jsp page?
By kasisaiganesh in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 05-27-2008, 06:29 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks