Advertisement
Robin_Lery

GUI program

Mar 11th, 2015
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.89 KB | None | 0 0
  1. package demo.sphinx.helloworld;
  2. import java.awt.Color;
  3. import java.awt.EventQueue;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.io.PrintStream;
  7.  
  8. import javax.swing.JButton;
  9. import javax.swing.JFrame;
  10. import javax.swing.JLabel;
  11. import javax.swing.JOptionPane;
  12. import javax.swing.JScrollPane;
  13. import javax.swing.ScrollPaneConstants;
  14. import javax.swing.SwingConstants;
  15. import javax.swing.SwingWorker;
  16. import javax.swing.UIManager;
  17. import javax.swing.JTextArea;
  18.  
  19. public class Frame1 {
  20.    
  21.  
  22.     JFrame frame;
  23.     private JLabel state;
  24.    
  25.  
  26.     /**
  27.      * Launch the application.
  28.      */
  29.     public static void main(String[] args) {
  30.        
  31.         EventQueue.invokeLater(new Runnable() {
  32.             public void run() {
  33.                 try {
  34.                     Frame1 window = new Frame1(args);
  35.                     window.frame.setVisible(true);
  36.                    
  37.                 } catch (Exception e) {
  38.                     e.printStackTrace();
  39.                 }
  40.             }
  41.         });
  42.     }
  43.  
  44.     /**
  45.      * Create the application.
  46.      * @param args
  47.      */
  48.     public Frame1(String[] args) {
  49.         initialize(args);
  50.     }
  51.  
  52.     /**
  53.      * Initialize the contents of the frame.
  54.      * @param args
  55.      * @param args
  56.      */
  57.    
  58.     public void doRun(String[] args) {
  59.        
  60.         SwingWorker<Void, String> worker = new SwingWorker<Void, String>(){
  61.  
  62.             @Override
  63.             protected Void doInBackground() throws Exception {
  64.                 // TODO Auto-generated method stub
  65.                 HelloWorld obj = new HelloWorld();
  66.                 obj.main(args);
  67.                 return null;
  68.             }};
  69.             worker.execute();
  70.     }
  71.     private void initialize(String[] args) {
  72.         frame = new JFrame();
  73.         frame.getContentPane().setForeground(new Color(50, 205, 50));
  74.         frame.setBounds(100, 100, 525, 295);
  75.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  76.         frame.getContentPane().setLayout(null);
  77.        
  78.         JButton btnNewButton = new JButton("Start Recognizing");
  79.         btnNewButton.setBackground(UIManager.getColor("Button.background"));
  80.         btnNewButton.setForeground(new Color(34, 139, 34));
  81.         btnNewButton.addActionListener(new ActionListener() {
  82.             public void actionPerformed(ActionEvent arg0) {
  83.                 state.setText("Listening");
  84.                 System.out.println("Started Listening");
  85.                 state.setBackground(new Color(51, 204, 0));
  86.                 doRun(args);
  87.  
  88.             }
  89.         });
  90.         btnNewButton.setBounds(10, 11, 156, 49);
  91.         frame.getContentPane().add(btnNewButton);
  92.        
  93.         JButton btnNewButton_1 = new JButton("Stop Recognizing");
  94.         btnNewButton_1.addActionListener(new ActionListener() {
  95.             public void actionPerformed(ActionEvent e) {
  96.                 state.setText("Not listening");
  97.                 state.setBackground(new Color(204, 0, 51));
  98.                 System.out.println("Stopped Listening");
  99.             }
  100.         });
  101.         btnNewButton_1.setForeground(new Color(204, 0, 0));
  102.         btnNewButton_1.setBackground(UIManager.getColor("Button.background"));
  103.         btnNewButton_1.setBounds(176, 11, 156, 49);
  104.         frame.getContentPane().add(btnNewButton_1);
  105.        
  106.         JButton btnNewButton_2 = new JButton("Show Commands");
  107.         btnNewButton_2.addActionListener(new ActionListener() {
  108.             public void actionPerformed(ActionEvent e) {
  109.                 JOptionPane.showMessageDialog(null, "List\n"
  110.                         + "One\n"
  111.                         + "two");
  112.             }
  113.         });
  114.         btnNewButton_2.setBounds(342, 11, 156, 49);
  115.         frame.getContentPane().add(btnNewButton_2);
  116.        
  117.         state = new JLabel("Not listening");
  118.         state.setForeground(new Color(255, 255, 255));
  119.         state.setBackground(new Color(204, 0, 51));
  120.         state.setHorizontalAlignment(SwingConstants.CENTER);
  121.         state.setBounds(10, 222, 488, 24);
  122.         state.setOpaque(true);
  123.         frame.getContentPane().add(state);
  124.        
  125.        
  126.         JScrollPane scrollPane = new JScrollPane();
  127.         scrollPane.setBounds(10, 66, 488, 145);
  128.         scrollPane.setVerticalScrollBarPolicy ( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS );
  129.         frame.getContentPane().add(scrollPane);
  130.        
  131.         JTextArea textArea = new JTextArea();
  132.         textArea.setBounds(10, 72, 488, 139);
  133.         System.setOut(new PrintStream(new Echo(System.out, textArea)));
  134.         scrollPane.setViewportView(textArea);
  135.        
  136.         frame.getContentPane().add(scrollPane);
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement