/* this file illustrates the creation of a graphical user interface using the Java "swing" API. */ import javax.swing.*; import java.awt.*; import java.awt.Graphics; import java.awt.event.*; class mygui extends JFrame implements ActionListener { // constants (finalized variables) that define window dimensions: public final int WW = 630; // window width public final int WH = 600; // window height; public final int GAP = 40; // standard gap between elements public int X0, Y0; // position of upper left corner of window // variables that define gui elements: private JButton button1, button2; // clickable buttons private JLabel btlabel, talabel; // labels are just text on the gui private JTextArea outputarea; // a multi-line editible area private JTextField inputfield; // single line editible area private JCheckBox goodbox, badbox; // check boxes private JMenuBar menubar; // the menu bar at the top private JMenu filemenu, optmenu; // two menus on the menu bar private JMenuItem save, quit, help; // three items on the menus private Graphics brush; // to paint additional elements private JScrollPane scroller; // to make an element scrollable private Image goodimage, badimage; public mygui(int x, int y) // constructor, with positioning parameters { X0 = x; Y0 = y; // creation of gui element objects button1 = new JButton("Click Me"); button2 = new JButton("Don't Click Me"); talabel = new JLabel("This is a JTextArea:"); btlabel = new JLabel("These are not just buttons, they're JButtons!:"); outputarea = new JTextArea("text is displayed here\n"); scroller = new JScrollPane(outputarea); // make outputarea scrollable inputfield = new JTextField(40); // 40 column input area badbox = new JCheckBox("Always trust content from Microsoft"); goodbox = new JCheckBox("Always trust content form professor Liang"); menubar = new JMenuBar(); filemenu = new JMenu("file"); optmenu = new JMenu("options"); save = new JMenuItem("save"); quit = new JMenuItem("exit"); help = new JMenuItem("help"); // set position of gui elements (except menu bar items) // Java will also try to add place them automatically if you don't. talabel.setBounds(20,40,300,20); scroller.setBounds(20,70,520,230); // contains outputarea inputfield.setBounds(100,320,350,30); badbox.setBounds(80,400,300,20); goodbox.setBounds(80,430,350,20); btlabel.setBounds(50,470,340,20); button1.setBounds(50,500,200,40); button2.setBounds(300,500,200,40); // set additional attributes of elements - see java doc for details: button1.setHorizontalAlignment(JTextField.CENTER); button2.setHorizontalAlignment(JTextField.CENTER); outputarea.setFont(new Font("Serif",Font.BOLD,18)); outputarea.setEditable(false); // default is true goodbox.setSelected(true); button1.setBackground(Color.green); button2.setBackground(Color.red); inputfield.setBackground(Color.blue); // add elements to gui: Container pane = this.getContentPane(); // the "content pane" of window pane.setLayout(null); // else java will place items automatically // pane.setLayout(new FlowLayout()); // one possible layout pane.add(button1); pane.add(button2); pane.add(talabel); pane.add(btlabel); pane.add(scroller); pane.add(inputfield); pane.add(goodbox); pane.add(badbox); pane.setBackground(Color.lightGray); // set background color // setting up the menu bar is a bit different: filemenu.add(save); filemenu.add(quit); optmenu.add(help); menubar.add(filemenu); menubar.add(optmenu); setJMenuBar(menubar); // associate gui elements with event handler: button1.addActionListener(this); button2.addActionListener(this); inputfield.addActionListener(this); goodbox.addActionListener(this); badbox.addActionListener(this); help.addActionListener(this); quit.addActionListener(this); save.addActionListener(this); // additional attributes: load images: goodimage = Toolkit.getDefaultToolkit().getImage( "/shared/csc/local/stuff/computer.gif"); prepareImage(goodimage,this); badimage = Toolkit.getDefaultToolkit().getImage( "/shared/csc/local/stuff/server_on_fire_md_wht.gif"); prepareImage(badimage,this); // finally, set properties of entire window this.setBounds(X0,Y0,WW,WH); // "this" is optional } // mygui constructor // sometimes need function to perform addtional setup that // can't be done in constructor public void init() { } // init // action dispatch method - intercepts events and dispatch to other methods public void actionPerformed(ActionEvent e) // event handler { if (e.getSource() == button1) b1handler(); if (e.getSource() == button2) b2handler(); if (e.getSource() == inputfield) infhandler(); if (e.getSource() == help) helphandler(); if (e.getSource() == quit) quithandler(); if (e.getSource() == save) savehandler(); if (e.getSource() == goodbox) { gboxchecked = !gboxchecked; checkhandler(); } if (e.getSource() == badbox) { bboxchecked = !bboxchecked; checkhandler(); } repaint(); // refreshes display } // actionPerformed action handler public void b1handler() { outputarea.append("thank you!\n"); } private int timesclicked = 0; // note this variable is inside the class public void b2handler() { timesclicked++; if (timesclicked<2) button2.setText("Ouch!"); else outputarea.append("I'm warning you!\n"); } public void infhandler() { String s = inputfield.getText(); outputarea.append("what do you mean by \""+ s + "\"?\n"); } public void helphandler() { outputarea.append("what do you need help with?\n"); } public void savehandler() { // empty handler - does nothing } public void quithandler() { outputarea.append("why would you want to quit such a nice program!?\n"); } private boolean gboxchecked = true; private boolean bboxchecked = false; public void checkhandler() { int i; brush = this.getGraphics(); // better done here if (gboxchecked && bboxchecked) { JOptionPane.showMessageDialog(this,"Make Up Your Mind!"); goodbox.setSelected(false); badbox.setSelected(false); gboxchecked = false; bboxchecked = false; } // can't trust both else if (gboxchecked) { for (i=0;i<100;i++) { brush.drawImage(goodimage,200,200,this); try {Thread.sleep(20);} catch (Exception ee) {} } // for } // goodchecked else if (bboxchecked) { for (i=0;i<100;i++) { brush.drawImage(badimage,200,200,this); try {Thread.sleep(20);} catch (Exception ee) {} } // for } // badchecked } // checkhandler } // mygui class public class gui { public static void main(String[] args) { mygui window0 = new mygui(50,50); mygui window1 = new mygui(500,500); // can create multiple windows window0.init(); window1.init(); window0.setVisible(true); //window1.setVisible(true); window0.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} }); window1.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} }); } // main } // gui