java lab@msc
java lab@msc
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
CheckboxGroup cbg;
public TrafficLightSimulator() {
setSize(600,400);
lbl1.setFont(font);
nPanel.add(lbl1);
add(nPanel);
cPanel.add(lbl2);
rbn1.setBackground(Color.RED);
rbn1.setFont(fontR);
cPanel.add(rbn1);
rbn1.addItemListener(this);
rbn2.setBackground(Color.ORANGE);
rbn2.setFont(fontR);
cPanel.add(rbn2);
rbn2.addItemListener(this);
rbn3.setBackground(Color.GREEN);
rbn3.setFont(fontR);
cPanel.add(rbn3);
rbn3.addItemListener(this);
add(cPanel);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String str=chk.getLabel();
char choice=str.charAt(0);
switch (choice) {
case 'R':lbl1.setText("STOP");
lbl1.setForeground(Color.RED);
break;
case 'O':lbl1.setText("READY");
lbl1.setForeground(Color.ORANGE);
break;
case 'G':lbl1.setText("GO");
lbl1.setForeground(Color.GREEN);
break;
// main method
new TrafficLightSimulator();
}
9.Write a Java program that to create simple simple calculator. for the , -,*, % operations. Add a text
field to display the result.
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
cal.setTitle("GUI Calculator");
cal.setSize(600, 400);
cal.setVisible(true);
cal.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
public GUICalculator() {
button1.addActionListener(listenForButton);
button2.addActionListener(listenForButton);
button3.addActionListener(listenForButton);
button4.addActionListener(listenForButton);
panel1.add(textField1);
panel1.add(textField2);
panel1.add(textField3);
panel1.add(button1);
panel1.add(button2);
panel1.add(button3);
panel1.add(button4);
this.add(panel1);
@Override
//1
double number1 = 0;
double number2 = 0;
//2
try {
number1 = Double.parseDouble(textField1.getText());
number2 = Double.parseDouble(textField2.getText());
error.printStackTrace();
//3
if (e.getSource() == button1) {