import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class SwingCheckBoxDemo implements ActionListener {
JCheckBox FZYamaha;
JCheckBox XCD135Bajaj;
JCheckBox StunnerHero;
JCheckBox Avenger;
public SwingCheckBoxDemo() {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("SwingCheckBoxDemo");
frame.setLayout(new BorderLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FZYamaha = new JCheckBox("FZ Yamaha");
FZYamaha.setMnemonic(KeyEvent.VK_C);
FZYamaha.setSelected(true);
XCD135Bajaj = new JCheckBox("XCD 135 Bajaj");
XCD135Bajaj.setMnemonic(KeyEvent.VK_G);
XCD135Bajaj.setSelected(false);
StunnerHero = new JCheckBox("Stunner");
StunnerHero.setMnemonic(KeyEvent.VK_H);
StunnerHero.setSelected(false);
Avenger = new JCheckBox("Avenger");
Avenger.setMnemonic(KeyEvent.VK_T);
Avenger.setSelected(false);
/*Put the check boxes in a column in a panel*/
JPanel checkPanel = new JPanel(new GridLayout(0, 1));
checkPanel.add(FZYamaha);
checkPanel.add(XCD135Bajaj);
checkPanel.add(StunnerHero);
checkPanel.add(Avenger);
frame.add(checkPanel, BorderLayout.LINE_START);
frame.pack();
frame.setSize(300,100);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
FZYamaha.addActionListener(this);
XCD135Bajaj.addActionListener(this);
StunnerHero.addActionListener(this);
Avenger.addActionListener(this);
}
public void actionPerformed(ActionEvent ae) {
if(ae.getSource() == FZYamaha) {
System.out.println("Yamaha");
FZYamaha.setSelected(true);
XCD135Bajaj.setSelected(false);
StunnerHero.setSelected(false);
Avenger.setSelected(false);
}
if(ae.getSource() == XCD135Bajaj) {
System.out.println("XCD135Bajaj");
FZYamaha.setSelected(false);
XCD135Bajaj.setSelected(true);
StunnerHero.setSelected(false);
Avenger.setSelected(false);
}
if(ae.getSource() == StunnerHero) {
System.out.println("StunnerHero");
FZYamaha.setSelected(false);
XCD135Bajaj.setSelected(false);
StunnerHero.setSelected(true);
Avenger.setSelected(false);
}
if(ae.getSource() == Avenger) {
System.out.println("Avenger");
FZYamaha.setSelected(false);
XCD135Bajaj.setSelected(false);
StunnerHero.setSelected(false);
Avenger.setSelected(true);
}
}
public static void main(String[] args) {
SwingCheckBoxDemo swingCheckBoxDemo = new SwingCheckBoxDemo();
}
}
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class SwingCheckBoxDemo implements ActionListener {
JCheckBox FZYamaha;
JCheckBox XCD135Bajaj;
JCheckBox StunnerHero;
JCheckBox Avenger;
public SwingCheckBoxDemo() {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("SwingCheckBoxDemo");
frame.setLayout(new BorderLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FZYamaha = new JCheckBox("FZ Yamaha");
FZYamaha.setMnemonic(KeyEvent.VK_C);
FZYamaha.setSelected(true);
XCD135Bajaj = new JCheckBox("XCD 135 Bajaj");
XCD135Bajaj.setMnemonic(KeyEvent.VK_G);
XCD135Bajaj.setSelected(false);
StunnerHero = new JCheckBox("Stunner");
StunnerHero.setMnemonic(KeyEvent.VK_H);
StunnerHero.setSelected(false);
Avenger = new JCheckBox("Avenger");
Avenger.setMnemonic(KeyEvent.VK_T);
Avenger.setSelected(false);
/*Put the check boxes in a column in a panel*/
JPanel checkPanel = new JPanel(new GridLayout(0, 1));
checkPanel.add(FZYamaha);
checkPanel.add(XCD135Bajaj);
checkPanel.add(StunnerHero);
checkPanel.add(Avenger);
frame.add(checkPanel, BorderLayout.LINE_START);
frame.pack();
frame.setSize(300,100);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
FZYamaha.addActionListener(this);
XCD135Bajaj.addActionListener(this);
StunnerHero.addActionListener(this);
Avenger.addActionListener(this);
}
public void actionPerformed(ActionEvent ae) {
if(ae.getSource() == FZYamaha) {
System.out.println("Yamaha");
FZYamaha.setSelected(true);
XCD135Bajaj.setSelected(false);
StunnerHero.setSelected(false);
Avenger.setSelected(false);
}
if(ae.getSource() == XCD135Bajaj) {
System.out.println("XCD135Bajaj");
FZYamaha.setSelected(false);
XCD135Bajaj.setSelected(true);
StunnerHero.setSelected(false);
Avenger.setSelected(false);
}
if(ae.getSource() == StunnerHero) {
System.out.println("StunnerHero");
FZYamaha.setSelected(false);
XCD135Bajaj.setSelected(false);
StunnerHero.setSelected(true);
Avenger.setSelected(false);
}
if(ae.getSource() == Avenger) {
System.out.println("Avenger");
FZYamaha.setSelected(false);
XCD135Bajaj.setSelected(false);
StunnerHero.setSelected(false);
Avenger.setSelected(true);
}
}
public static void main(String[] args) {
SwingCheckBoxDemo swingCheckBoxDemo = new SwingCheckBoxDemo();
}
}
No comments:
Post a Comment