Complete the following program based on the given comments. The program uses two
ID: 3807977 • Letter: C
Question
Complete the following program based on the given comments. The program uses two buttons to display messages on the console. public class TestButton extends JFrame {//create an instance of JButton with label "OK"//create an instance of JButton with label "Cancel" Public TestButton () {JPanel panel = new JPanel ();//add the OK button to panel//add the Cancel button to panel this.add(panel);//add OKListener to OK button//add CancelListener to Cancel button public static void main (String [] args) {TestButton app = new TestButton (); App.setVisible(true);Explanation / Answer
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class TestButton extends JFrame{
JButton b=new JButton("Ok");
JButton c=new JButton("Cancel");
public TestButton(){
JPanel panel = new JPanel();
panel.add(b);
panel.add(c);
this.add(panel);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(null, "Ok Button is clicked");
}
});
c.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Cancel Button is clicked");
}
});
}
public static void main(String args[]){
TestButton app = new TestButton();
app.setVisible(true);
}
}
when you click on the ok button dialog box is shown similarly for canel button also show the similar dialog box
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.