imleriea CN1 code, describe on the next page the structure of a program which wh
ID: 3730324 • Letter: I
Question
imleriea CN1 code, describe on the next page the structure of a program which which has the following components: mple Graphical User Interface (GUI) consisting of a form with a border layout (1) a single button on the north area, whose label is initially "Hello" and which prints the message "World" on the console when pressed; assume that the listener attached to this button is not a command object (2) a side menu with a single item that has the effect of changing the button label to "Goodbye" when selected Provide code for the form and other user-defined classes that are used in the construction of this GUI. Note that CN1 Button class has a method setText(String) to change its label.Explanation / Answer
Q1. Running code
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Border implements ActionListener { //ActionListener is used to do some action it contains actionperformed method
JFrame f; //Every widget will be paste on frame
JButton b1;
Border()
{
f=new JFrame();
b1=new JButton("Hello");;
f.add(b1,BorderLayout.NORTH);
f.setSize(300,300);
f.setVisible(true);
b1.addActionListener(this);
}
public static void main(String[] args) {
new Border();
}
@Override
public void actionPerformed(ActionEvent e) {
b1.setText("World"); //Text change after clicking on button
}
}
Q2.Design Simple menu Using Swing Just click on File Menu and after click on new option then
the text ob button become Change to Goodbye
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package corejavaconcept;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class UseMenu extends javax.swing.JFrame implements ActionListener {
JFrame frame;
JMenuBar mb;
JMenu mn1,mn2,mn3;
JMenuItem item1,item2,item3;
JButton b1;
public void go()
{
frame=new JFrame("menubar");
mb=new JMenuBar();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b1=new JButton("Hello");
mn1=new JMenu("File");
mn2=new JMenu("Edit");
mn3=new JMenu("Open");
item1=new JMenuItem("new");
item2=new JMenuItem("save");
item3=new JMenuItem("load");
item1.addActionListener(this);
mb.add(mn1);
mb.add(mn2);
mb.add(mn3);
mn1.add(item1);
mn1.add(item2);
mn1.add(item3);
frame.setJMenuBar(mb);
frame.add(b1,BorderLayout.CENTER);
//UseAnimation1 obj=new UseAnimation1();
frame.setSize(300,300);
frame.setVisible(true);
}
public static void main(String aa[])
{
UseMenu obj=new UseMenu();
obj.go();
}
@Override
public void actionPerformed(ActionEvent e) {
b1.setText("GoodBye");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.