Results 1 to 2 of 2
  1. #1
    carl is offline Member
    Join Date
    Jul 2007
    Posts
    35
    Rep Power
    0

    Default 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

    Java 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.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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).
    Java 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() {

Similar Threads

  1. Initialize variables before use
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-22-2007, 11:22 AM
  2. Variables
    By mew in forum New To Java
    Replies: 3
    Last Post: 12-11-2007, 12:44 PM
  3. Help with variables in java
    By fernando in forum New To Java
    Replies: 2
    Last Post: 08-06-2007, 05:03 PM
  4. Help with static variables
    By bbq in forum Advanced Java
    Replies: 1
    Last Post: 06-28-2007, 05:38 PM
  5. Saving Variables
    By Fish in forum New To Java
    Replies: 6
    Last Post: 06-25-2007, 08:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •