Monday, July 30, 2012

Simple check Box Example

Here is a picture of the form running

Here is the code for the Form1.java and panel


import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
public class Form1 {
 JFrame frame= new JFrame("Checkbox test");
 JPanel panel = new JPanel(new FlowLayout());
 JCheckBox chk1 = new JCheckBox("0ne");
 JCheckBox chk2=new JCheckBox("Two");
 JButton button = new JButton("test");
 
 JLabel label = new JLabel();
 
 public Form1(){
 
 panel.add(chk1);
 panel.add(chk2);
 panel.add(button);
 button.addActionListener(new TestAction());
 panel.add(label);
 frame.add(panel);
 frame.setBounds(100,100,300,300);
 frame.setVisible(true);
 
 }
 
 private class TestAction implements ActionListener{

  @Override
  public void actionPerformed(ActionEvent arg0) {
   String s = "";
  
   if(chk1.isSelected()){
    s+=chk1.getText() + " ";
   }
   
   if (chk2.isSelected()){
    s+=chk2.getText() + " ";
   
  }
   label.setText(s);
  
 }
 }
}
 




Here is Program.java



public class Program {

 public static void main(String[] args){
  Form1 f = new Form1();
  
 }
}

No comments:

Post a Comment