Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-31-2007, 07:12 PM
Member
 
Join Date: Jul 2007
Posts: 35
carl is on a distinguished road
Problem with variables in java
Hi, I have code that works but i just need to get it so there are 3 variables that are set throughout the whole file.

The first variable needs to be a list the others just whole numbers. Here is the code if you want to help me out

Code:
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Game implements KeyListener { JFrame gameFrame; JLabel gameOutput; JPanel gamePanel; JTextField typingArea; public Game() { //Create and set up the window. gameFrame = new JFrame("Text Game"); gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); gameFrame.setSize(new Dimension(120, 40)); gamePanel = new JPanel(new GridLayout(2, 2)); addWidgets(); gameFrame.getContentPane().add(gamePanel, BorderLayout.CENTER); //Display the window. gameFrame.pack(); gameFrame.setVisible(true); } private void addWidgets() { //Create widgets. gameOutput = new JLabel("Press an Arrow Key"); typingArea = new JTextField(20); typingArea.addKeyListener(this); //Listen to events from the Convert button. gameOutput.addKeyListener(this); //Add the widgets to the container. gamePanel.add(gameOutput); gamePanel.add(typingArea); gameOutput.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); } public void keyTyped(KeyEvent e) { } public void keyReleased(KeyEvent e) { } public void keyPressed(KeyEvent e) { int keyCode = e.getKeyCode(); switch (keyCode) { case KeyEvent.VK_UP: gameOutput.setText("Up"); break; case KeyEvent.VK_DOWN: gameOutput.setText("Down"); break; case KeyEvent.VK_LEFT: gameOutput.setText("Left"); break; case KeyEvent.VK_RIGHT: gameOutput.setText("Right"); break; } } private static void createAndShowGUI() { //Make sure we have nice window decorations. JFrame.setDefaultLookAndFeelDecorated(true); Game converter = new Game(); } public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }
Thanks.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-31-2007, 09:50 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,266
hardwired is on a distinguished road
need to get it so there are 3 variables that are set throughout the whole file.
For these to be accessible to everything, ie, all methods, in the class it needs to be in class scope. Variables that have class scope are called member variables (among other things).
Code:
public class Game implements KeyListener { JFrame gameFrame; JLabel gameOutput; JPanel gamePanel; JTextField typingArea; // Declare them as member variables in class scope. JList list; int aConstant; int anotherConstant; public Game() {
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Initialize variables before use Java Tip Java Tips 0 12-22-2007 01:22 PM
Variables mew New To Java 3 12-11-2007 02:44 PM
Help with variables in java fernando New To Java 2 08-06-2007 07:03 PM
Help with static variables bbq Advanced Java 1 06-28-2007 07:38 PM
Saving Variables Fish New To Java 6 06-25-2007 10:20 PM


All times are GMT +3. The time now is 03:08 PM.


VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org