Created
December 3, 2014 02:32
-
-
Save jensbodal/c2f86070a8fdea27a47a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package finalproject; | |
import java.awt.BorderLayout; | |
import java.awt.event.*; | |
import javax.swing.*; | |
// Don't impelment ActionListener on the JFrame unless you need the JFrame to | |
// have an action listener | |
public class Game extends JFrame { | |
public String command; | |
JFrame frame = new JFrame("Game gui"); | |
JTextField textInput = new JTextField(); | |
JTextArea textOutputArea = new JTextArea(5, 30); | |
JButton jbtnenter = new JButton("Enter"); | |
JPanel displayPanel = new JPanel(); | |
JScrollPane scrollpane = new JScrollPane(textOutputArea); | |
public Game() { | |
jbtnenter.addActionListener(commandListener()); | |
textInput.addActionListener(commandListener()); | |
displayPanel.setLayout(new BorderLayout()); | |
textOutputArea.setEditable(false); | |
textOutputArea.setText("Question"); | |
// Add the components to the panel | |
displayPanel.add(textOutputArea, BorderLayout.NORTH); | |
displayPanel.add(textInput, BorderLayout.CENTER); | |
displayPanel.add(jbtnenter, BorderLayout.SOUTH); | |
// Add the panel to this JFrame | |
this.add(displayPanel); | |
// setVisible(true); -- Don't do this in this class, let the class that | |
// calls the Frame handle this | |
// Always call pack last | |
pack(); | |
} | |
private ActionListener commandListener() { | |
return new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
command = textInput.getText(); | |
if (command.equals("answer")) { | |
textOutputArea.setText("response"); | |
} | |
} | |
}; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package finalproject; | |
import javax.swing.JFrame; | |
public class GameRunner { | |
// Used to test the code | |
public static void main(String[] args) { | |
Game game = new Game(); | |
game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
game.setVisible(true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment