Assignment 6 Graphical User Interface Design a class called MyJFrame to reproduc
ID: 3860101 • Letter: A
Question
Assignment 6 Graphical User Interface Design a class called MyJFrame to reproduce Figures 1 and 2
Figure 1
Figure 2
Design a class called MyJButton that reproduces Figure 3.
Notice that the image icon is changed.
Figure 3
Part II
Add Functionality Add functionality to the image button, such that when the mouse clicks the button, a frame similar to the one below appears. Use the program code below to create this frame. However, you are responsible for defining the event that will cause this to happen.
// Import the basic graphics classes. import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import javax.swing.JFrame; public class MyBasicFrame extends JFrame { // Create a constructor method public MyBasicFrame() { // All we do is call JFrame's constructor. // We don't need anything special for this program. super("One Line"); setBackground(Color.blue); } /* The following methods are instance methods. Create a paint() method to override the one in JFrame. This is where the drawing happens. We don't have to call it in our program, it gets called automatically whenever the frame needs to be redrawn, like when it it made visible or moved or whatever. */ public void paint(Graphics g) { g.setColor(Color.red); g.drawLine(50,150,150,50); // Draw a line g.setColor(Color.YELLOW); g.fillOval(100, 100, 50, 50); g.setColor(Color.CYAN); g.setFont(new Font("Times Roman", Font.BOLD, 20)); g.drawString("More than a line", 50, 180); } }
Assignment 6 Graphical User Interface Design a class called MyJFrame to reproduce Figures 1 and 2 Figure 1 My First Java Graphical User Interface Program File Tool Help New Open Save Close Figure 2 ® My First Java Graphical User Interface Pro File Tool Help Sort Search Copy Paste PasteExplanation / Answer
import java.awt.EventQueue;
import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class MyJframe extends JFrame {
public MyJframe() {
initUI();
}
private void initUI() {
createMenuBar();
setTitle("Right menu");
setSize(300, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private void createMenuBar() {
JMenuBar menubar = new JMenuBar();
/*File Tools and Help*/
JMenu fileMenu = new JMenu("File");
JMenu toolsMenu = new JMenu("Tools");
JMenu helpMenu = new JMenu("Help");
/*Menu Items for File*/
JMenuItem newMi = new JMenuItem("New");
JMenuItem openMi = new JMenuItem("Open");
JMenuItem saveMi = new JMenuItem("Save");
JMenuItem closeMi = new JMenuItem("Close");
/* Menu Items for tools */
JMenuItem sortMi = new JMenuItem("Sort");
JMenuItem searchMi = new JMenuItem("Search");
JMenu pasteMi = new JMenu("Paste");
JMenuItem pastesubMi = new JMenuItem("Paste");
JMenuItem copyMi = new JMenuItem("Copy");
pasteMi.add(pastesubMi);
pasteMi.add(copyMi);
/*Adding file Menu Items */
fileMenu.add(newMi);
fileMenu.add(openMi);
fileMenu.add(saveMi);
fileMenu.add(closeMi);
/* Adding Tools Menu Items */
toolsMenu.add(sortMi);
toolsMenu.add(searchMi);
toolsMenu.add(pasteMi);
/*adding to menu bar*/
menubar.add(fileMenu);
menubar.add(toolsMenu);
menubar.add(Box.createHorizontalGlue());
menubar.add(helpMenu);
setJMenuBar(menubar);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
MyJframe ex = new MyJframe();
ex.setVisible(true);
});
}
}