I'm trying to make a Java Swing calculator application, and I want to make my own set of GUI(Based on original Swing), but I've got some problems with the menu & menu item. I've tried searching on the internet, but all I get is how to add menu to your app. I want to make one on my own. What can I do?
See the code below
import javax.swing.*;
import java.awt.*;
/**
* The base menu item that can click on, achieve the functions.
* Display on a MyMenu.
*
* @see MyMenuBar
* @see MyMenu
* @author abc, 2022/2/19
*/
public class MyMenuItem extends JMenuItem {
DefaultButtonModel model = (DefaultButtonModel) this.getModel();
/**
* The constructor. Creates a menu item with a text.
*
* @param text The String text to be displayed on the Item.
*/
public MyMenuItem(String text) {
setFocusPainted(false);
setBorderPainted(false);
setForeground(Color.WHITE);
setFont(new Font("Microsoft YaHei", Font.PLAIN, 14));
setText(text);
}
/**
* The constructor that creates an Item with text & icon.
*
* @param icon The icon to be displayed.
* @param text The text to be displayed.
*/
public MyMenuItem(ImageIcon icon, String text) {
setFocusPainted(false);
setBorderPainted(false);
setForeground(Color.WHITE);
setFont(new Font("Microsoft YaHei", Font.PLAIN, 14));
setText(text);
setIcon(icon);
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(MyFrame.baseColor);
g2d.fillRect(0, 0, getWidth(), getHeight());
if (model.isPressed()) {
g2d.setColor(new Color(55, 40, 150));
g2d.fillRect(0, 0, getWidth(), getHeight());
}
super.paintComponent(g);
}
}
MyMenu code (unfinished):
import javax.swing.*;
import java.awt.*;
/**
* The base class that forms a menu. Will be used to call out Settings &
* modes. Displays on top, in a menu bar.
*
* @see MyMenuBar
* @author abc, 2022/2/18 - 2022
*/
public class MyMenu extends JMenu {
public MyMenu(String s) {
setContentAreaFilled(false);
setFont(new Font("Consolas", Font.PLAIN, 18));
setForeground(Color.WHITE);
setText(s);
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(new Color(34, 36, 54));
g2d.fillRect(2, 2, this.getWidth() - 5, this.getHeight() - 5);
if (isSelected()) {
g2d.setColor(new Color(34, 36, 54));
g2d.fillRect(0, 0, getWidth(), getHeight());
}
}
}
MyMenuBar code:
import javax.swing.*;
import java.awt.*;
/**
* The menu bar of the app. Will be used in the mode switching &
* the call of the settings. Also, the basic Container for a MyMenu.
*
* @author abc, 2022/2/17 - 2022/2/18
*/
public class MyMenuBar extends JMenuBar {
public MyMenuBar() {
setBorderPainted(false);
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(MyFrame.baseColor);
g2d.fillRect(0, 0, this.getWidth(), this.getHeight());
}
}
MyFrame code:
import javax.swing.*;
import java.awt.*;
/**
* The basic Frame class of the calculator.
* The frame created will be colored & border-decorated.
* The class extends JFrame in "javax.swing", and uses its methods.
*
* @author abc, 2022/2/11
*/
public class MyFrame extends JFrame {
/**
* The color will be used for the background of the Frame.
*/
public static final Color baseColor = new Color(34, 36, 54);
public String status;
/**
* The contentpane of the frame.
*/
public JPanel panel = new JPanel();
/**
* The custom icon of the frame.
*/
static ImageIcon icon = new ImageIcon("sources/icon.png"); // Icon image
public MyFrame() {
status = "Regular"; // The status will be filled into the title.
panel.setBackground(baseColor);
setIconImage(icon.getImage());
setTitle(String.format("%s - Calc++", status));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Set the title and the close operation
setContentPane(panel);
setSize(800, 480);
setLocationRelativeTo(null);
}
}