Ask Question

Name:
Title:
Your Question:

Answer Question

Name:
Your Answer:
User Submitted Source Code!


Description:
  demo
Language: JAVA
Code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class demo {
     
     private JFrame mainframe;
     private JLabel headerlabel;
     private JLabel statuslabel;
     private JPanel controlpanel;
     
     
     public demo()
     {
          prepareGUI();
     }

     public static void main (String args[])
     {
          demo d = new demo();
          d.showEventdemo();
     }
     private void prepareGUI() {
          // TODO Auto-generated method stub
          mainframe = new JFrame("Java SWING Example");
          mainframe.setSize(400,400);
          mainframe.setLayout(new GridLayout(5,5));
          
          headerlabel = new JLabel("", JLabel.CENTER);
          statuslabel = new JLabel("", JLabel.CENTER);
          statuslabel.setSize(350,150);
          
          mainframe.addWindowListener(new WindowAdapter()
                    {
               public void WindowClosing(WindowEvent windowEvent)
               {
               System.exit(0);
               }
                    });
          
          controlpanel = new JPanel();
          controlpanel.setLayout(new FlowLayout());
          
          mainframe.add(headerlabel);
          mainframe.add(statuslabel);
          mainframe.add(controlpanel);
          mainframe.setVisible(true);
               
     }
     
     private void showEventdemo()
     {
          headerlabel.setText("Control in Action: Button");
          
          JButton ok = new JButton();
          JButton submit = new JButton();
          JButton cancel = new JButton();
          
          ok.setActionCommand("OK");
          submit.setActionCommand("Submit");
          cancel.setActionCommand("Cancel");
          
          
          ok.addActionListener(new ButtonClickListener());
          submit.addActionListener(new ButtonClickListener());
          cancel.addActionListener(new ButtonClickListener());
          
          controlpanel.add(ok);
          controlpanel.add(submit);
          controlpanel.add(cancel);
          
          mainframe.setVisible(true);
          
     }

     private class ButtonClickListener implements ActionListener
     {
          public void actionPerformed(ActionEvent e)
          {
               String command = e.getActionCommand();
               if (command.equals("OK")) {
                      statuslabel.setText("Ok button clicked.");       
               }
               else if (command.equals("Submit")) {
                    statuslabel.setText("Submit button clicked.");
               }
               else {
                    statuslabel.setText("Cancel button clicked.");
               }
          }     
     }
}
     
Comments: