JToggelButton class in java
//JToggleButton class
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class JToggleButton_demo extends JFrame implements ActionListener
{
Container con;
JLabel lbl1, lbl2;
JToggleButton btn1;
JToggleButton_demo(String title)
{
super(title);
con = new Container();
con.setLayout(new FlowLayout());
lbl2 = new JLabel("click Button to ON/OFF" );
con.add(lbl2);
lbl1 = new JLabel(" ");
con.add(lbl1);
btn1 = new JToggleButton("ON/OFF");
btn1.addActionListener(this);
con.add(btn1);
add(con);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent ae)
{
if (btn1.isSelected()) {
btn1.setText("ON");
}
else
{
btn1.setText("OFF");
}
lbl1.setText(btn1.getText());
}
public static void main(String[] args) {
JToggleButton_demo obj = new JToggleButton_demo("JToggleButton_demo");
obj.setSize(200, 200);
obj.setVisible(true);
}
}
OUTPUT:
Comments
Post a Comment