Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Dec 2004
    Posts
    202
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Java Help-Variables

    I hava 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();
                }
            });
        }
    
    }

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Location
    Kansas City, Kansas
    Posts
    1,518
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Like so?
    Code:
    public class Game implements KeyListener {
        JFrame gameFrame;
        JLabel gameOutput;
        JPanel gamePanel;
        JTextField typingArea;
        int wholeNum;
        int wholeNum2;
        int[] foo;
    The first two are variables for the whole numbers and the last one is a list of numbers, although you can change the variable type to whatever you need a list of.

  • #3
    Regular Coder
    Join Date
    Dec 2004
    Posts
    202
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yea just like that figured it out before that just need to get it so that the list has a string inside of it any help with that? Just help with lists is all

  • #4
    Senior Coder
    Join Date
    Aug 2002
    Location
    Kansas City, Kansas
    Posts
    1,518
    Thanks
    0
    Thanked 2 Times in 2 Posts
    The list is known as an array, check out this website, it might help a little.
    [http://www.scit.wlv.ac.uk/~jphb/java/arrays.html]


  •  

    Posting Permissions

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