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