Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.*;
- public class RobotGui implements ActionListener {
- private JFrame frame = new JFrame();
- private JPanel panel = new JPanel();
- private JTextField textField = new JTextField(20);
- private JLabel label = new JLabel();
- private JButton button = new JButton("Start");
- private String myUserInput;
- public RobotGui(String userInput){
- myUserInput = userInput;
- frame.setTitle("Frame");
- frame.setVisible(true);
- frame.setSize(250,150);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.add(panel);
- frame.setLocationRelativeTo(null);
- label.setText("Input: ");
- panel.add(label);
- panel.add(textField);
- panel.add(button);
- frame.getContentPane().add(panel);
- button.addActionListener(this);
- //Throws pointer exception if null
- while(myUserInput == null){
- try {
- Thread.sleep(500);
- } catch (InterruptedException e) {
- JOptionPane.showMessageDialog(null, "UserInput == null error.");
- }
- }
- }
- public String getUserInput(){
- return myUserInput;
- }
- public void actionPerformed(ActionEvent e) {
- if (e.getSource() == button){
- myUserInput = textField.getText();
- frame.setVisible(false);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement