Advertisement
bha2597

Untitled

Sep 11th, 2014
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.30 KB | None | 0 0
  1. import java.awt.event.ActionEvent;
  2. import java.awt.event.ActionListener;
  3.  
  4. import javax.swing.*;
  5.  
  6. public class RobotGui implements ActionListener {
  7.    
  8.     private JFrame frame = new JFrame();
  9.     private JPanel panel = new JPanel();
  10.     private JTextField textField = new JTextField(20);
  11.     private JLabel label = new JLabel();
  12.     private JButton button = new JButton("Start");
  13.     private String myUserInput;
  14.    
  15.     public RobotGui(String userInput){
  16.        
  17.         myUserInput = userInput;
  18.        
  19.         frame.setTitle("Frame");
  20.         frame.setVisible(true);
  21.         frame.setSize(250,150);
  22.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  23.         frame.add(panel);
  24.         frame.setLocationRelativeTo(null);
  25.  
  26.         label.setText("Input: ");
  27.         panel.add(label);
  28.         panel.add(textField);
  29.         panel.add(button);
  30.        
  31.         frame.getContentPane().add(panel);
  32.         button.addActionListener(this);
  33.        
  34.         //Throws pointer exception if null
  35.         while(myUserInput == null){
  36.            
  37.             try {
  38.                 Thread.sleep(500);
  39.             } catch (InterruptedException e) {
  40.                 JOptionPane.showMessageDialog(null, "UserInput == null error.");
  41.             }
  42.         }
  43.    
  44.     }
  45.    
  46.     public String getUserInput(){
  47.         return myUserInput;
  48.     }
  49.  
  50.     public void actionPerformed(ActionEvent e) {
  51.         if (e.getSource() == button){
  52.             myUserInput = textField.getText();
  53.             frame.setVisible(false);
  54.            
  55.         }
  56.        
  57.     }
  58.  
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement