sobota, 6 września 2014

Java - simple gui example program

package gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class myWindow extends JFrame implements ActionListener
{
    JButton bGetDate, bExit, bSysCheck;
    JLabel lshowDate;
   
   
    private static final long serialVersionUID = 1L;

    public myWindow()
    {
        setSize(400,300);
        setTitle("My Stratus");
        setLayout(null);
       
        bGetDate = new JButton("Date");
        bGetDate.setBounds(50, 50, 100, 20);
        add(bGetDate);
        bGetDate.addActionListener(this);
       
        bSysCheck = new JButton("SysChceck");
        bSysCheck.setBounds(150, 50, 100, 20);
        add(bSysCheck);
        bSysCheck.addActionListener(this);
       
       
        bExit = new JButton("Exit");
        bExit.setBounds(250, 50, 100, 20);
        add(bExit);
        bExit.addActionListener(this);
       
        lshowDate = new JLabel("Date: ");
        lshowDate.setBounds(50, 100, 200, 20);
        lshowDate.setForeground(Color.RED);
        lshowDate.setFont(new Font("SansSerif",Font.BOLD, 10));
        add(lshowDate);
           
    }
   
    public static void main(String[] args)
    {
        myWindow window = new myWindow();
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setVisible(true);
    }
   
    @Override
    public void actionPerformed(ActionEvent e)
    {
        Object src = e.getSource();
        if(src==bExit)
        {
            dispose();
        }
        else if(src==bGetDate)
        {
            //System.out.println(new Date());
            lshowDate.setText(new Date().toString());
           
           
        }
        else if(src==bSysCheck)
        {
            System.out.print("sys details: ");
            Runtime r = Runtime.getRuntime();
            Process p;
            try {
                p = r.exec("uname -a");
                p.waitFor();
                BufferedReader b = new BufferedReader(new InputStreamReader(p.getInputStream()));
               
                String line = "";

                while ((line = b.readLine()) != null)
                {
                  System.out.println(line);
                }
               
               
            } catch (IOException | InterruptedException e1) {
               
                e1.printStackTrace();
            }

           
        }
    }
}

           
       
   

Brak komentarzy:

Prześlij komentarz