Skip to content

Fix Visibility Issue and Improve Code Structure in Electricity Billing System GUI #4

@PritishDoc

Description

@PritishDoc

The Project class in the Electricity Billing System GUI implementation has the following issues and improvement opportunities:

Visibility Issue: The main window is set to invisible upon initialisation, which prevents the application from displaying. This is due to setVisible(false) in the constructor.
Hardcoded Paths: The paths to external applications such as Notepad, Calculator, and Chrome are hardcoded. This may lead to issues on different systems or configurations.
Unused Import Statements: Import statements are not optimized. Some of them are redundant.
Commented Code: There are sections of commented-out code, such as //master.add(m3);, which should be cleaned up or justified.
Exception Handling: Exception handling for external application execution is minimal and should provide feedback to the user in case of failure.
Steps to Reproduce:
Compile and run the provided Project class.
Observe that the application window does not appear.
Expected Behavior:
The application window should be visible upon launch, and menu items should function correctly, launching external applications or dialogs as specified.

Proposed Fix:
Set Visibility to True: Change setVisible(false) to setVisible(true) in the constructor to make the main window visible upon launch.
Refactor Hardcoded Paths: Use relative paths or system properties to locate external applications, ensuring compatibility across different systems.
Optimize Imports: Remove unused import statements.
Clean Up Commented Code: Remove or justify commented-out code sections.
Improve Exception Handling: Provide user feedback for exceptions when launching external applications.
``import java.awt.;
import java.awt.event.
;
import javax.swing.*;

public class Project extends JFrame implements ActionListener {
Project() {
super("Electricity Billing System");

    setSize(1500, 800);

    /* Adding background image */
    ImageIcon ic = new ImageIcon(ClassLoader.getSystemResource("images/main.jpg"));
    Image i3 = ic.getImage().getScaledInstance(1420, 720, Image.SCALE_DEFAULT);
    ImageIcon icc3 = new ImageIcon(i3);
    JLabel l1 = new JLabel(icc3);
    add(l1);

    /* First Column */
    JMenuBar mb = new JMenuBar();
    JMenu master = new JMenu("Master");
    JMenuItem m1 = new JMenuItem("New Customer");
    JMenuItem m2 = new JMenuItem("Customer Details");
    JMenuItem m3 = new JMenuItem("Deposit Details");
    master.setForeground(Color.BLUE);

    /* ---- Customer Details ---- */
    m1.setFont(new Font("monospaced", Font.PLAIN, 12));
    ImageIcon icon1 = new ImageIcon(ClassLoader.getSystemResource("images/icon1.jpg"));
    Image image1 = icon1.getImage().getScaledInstance(20, 20, Image.SCALE_DEFAULT);
    m1.setIcon(new ImageIcon(image1));
    m1.setMnemonic('D');
    m1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D, ActionEvent.CTRL_MASK));
    m1.setBackground(Color.WHITE);

    /* ---- Meter Details ---- */
    m2.setFont(new Font("monospaced", Font.PLAIN, 12));
    ImageIcon icon2 = new ImageIcon(ClassLoader.getSystemResource("images/icon2.png"));
    Image image2 = icon2.getImage().getScaledInstance(20, 20, Image.SCALE_DEFAULT);
    m2.setIcon(new ImageIcon(image2));
    m2.setMnemonic('M');
    m2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_M, ActionEvent.CTRL_MASK));
    m2.setBackground(Color.WHITE);

    /* ---- Deposit Details  ----- */
    m3.setFont(new Font("monospaced", Font.PLAIN, 12));
    ImageIcon icon3 = new ImageIcon(ClassLoader.getSystemResource("images/icon3.png"));
    Image image3 = icon3.getImage().getScaledInstance(20, 20, Image.SCALE_DEFAULT);
    m3.setIcon(new ImageIcon(image3));
    m3.setMnemonic('N');
    m3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
    m3.setBackground(Color.WHITE);

    m1.addActionListener(this);
    m2.addActionListener(this);
    m3.addActionListener(this);

    // --------------------------------------------------------------------------------------------

    /* Second Column */
    JMenu user = new JMenu("User");
    JMenuItem u1 = new JMenuItem("Pay Bill");
    JMenuItem u2 = new JMenuItem("Calculate Bill");
    JMenuItem u3 = new JMenuItem("Last Bill");
    user.setForeground(Color.RED);

    /* ---- Pay Bill ---- */
    u1.setFont(new Font("monospaced", Font.PLAIN, 12));
    ImageIcon icon4 = new ImageIcon(ClassLoader.getSystemResource("images/icon4.png"));
    Image image4 = icon4.getImage().getScaledInstance(20, 20, Image.SCALE_DEFAULT);
    u1.setIcon(new ImageIcon(image4));
    u1.setMnemonic('P');
    u1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, ActionEvent.CTRL_MASK));
    u1.setBackground(Color.WHITE);

    /* ---- Bill Details ---- */
    u2.setFont(new Font("monospaced", Font.PLAIN, 12));
    ImageIcon icon5 = new ImageIcon(ClassLoader.getSystemResource("images/icon5.png"));
    Image image5 = icon5.getImage().getScaledInstance(20, 20, Image.SCALE_DEFAULT);
    u2.setIcon(new ImageIcon(image5));
    u2.setMnemonic('B');
    u2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B, ActionEvent.CTRL_MASK));
    u2.setBackground(Color.WHITE);

    /* ---- Last Bill ----*/
    u3.setFont(new Font("monospaced", Font.PLAIN, 12));
    ImageIcon icon6 = new ImageIcon(ClassLoader.getSystemResource("images/icon6.png"));
    Image image6 = icon6.getImage().getScaledInstance(20, 20, Image.SCALE_DEFAULT);
    u3.setIcon(new ImageIcon(image6));
    u3.setMnemonic('L');
    u3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L, ActionEvent.CTRL_MASK));
    u3.setBackground(Color.WHITE);

    u1.addActionListener(this);
    u2.addActionListener(this);
    u3.addActionListener(this);

    // --------------------------------------------------------------------------------------------- 

    /* Third Column */
    JMenu report = new JMenu("Report");
    JMenuItem r1 = new JMenuItem("Generate Bill");
    report.setForeground(Color.BLUE);

    /* ---- Report ---- */
    r1.setFont(new Font("monospaced", Font.PLAIN, 12));
    ImageIcon icon7 = new ImageIcon(ClassLoader.getSystemResource("images/icon7.png"));
    Image image7 = icon7.getImage().getScaledInstance(20, 20, Image.SCALE_DEFAULT);
    r1.setIcon(new ImageIcon(image7));
    r1.setMnemonic('R');
    r1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, ActionEvent.CTRL_MASK));
    r1.setBackground(Color.WHITE);

    r1.addActionListener(this);

    // -----------------------------------------------------------------------------------------------

    /* Fourth Column */
    JMenu utility = new JMenu("Utility");
    JMenuItem ut1 = new JMenuItem("Notepad");
    JMenuItem ut2 = new JMenuItem("Calculator");
    JMenuItem ut3 = new JMenuItem("Web Browser");
    utility.setForeground(Color.RED);

    /* ---- Notepad ---- */
    ut1.setFont(new Font("monospaced", Font.PLAIN, 12));
    ImageIcon icon8 = new ImageIcon(ClassLoader.getSystemResource("images/icon12.png"));
    Image image8 = icon8.getImage().getScaledInstance(20, 20, Image.SCALE_DEFAULT);
    ut1.setIcon(new ImageIcon(image8));
    ut1.setMnemonic('N');
    ut1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
    ut1.setBackground(Color.WHITE);

    /* ---- Calculator ---- */
    ut2.setFont(new Font("monospaced", Font.PLAIN, 12));
    ImageIcon icon9 = new ImageIcon(ClassLoader.getSystemResource("images/icon9.png"));
    Image image9 = icon9.getImage().getScaledInstance(20, 20, Image.SCALE_DEFAULT);
    ut2.setIcon(new ImageIcon(image9));
    ut2.setMnemonic('C');
    ut2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK));
    ut2.setBackground(Color.WHITE);

    /* ---- Web Browser ---- */
    ut3.setFont(new Font("monospaced", Font.PLAIN, 12));
    ImageIcon icon10 = new ImageIcon(ClassLoader.getSystemResource("images/icon10.png"));
    Image image10 = icon10.getImage().getScaledInstance(20, 20, Image.SCALE_DEFAULT);
    ut3.setIcon(new ImageIcon(image10));
    ut3.setMnemonic('W');
    ut3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, ActionEvent.CTRL_MASK));
    ut3.setBackground(Color.WHITE);

    ut1.addActionListener(this);
    ut2.addActionListener(this);
    ut3.addActionListener(this);

    // ---------------------------------------------------------------------------------------

    /* Fifth Column */
    JMenu exit = new JMenu("Exit");
    JMenuItem ex = new JMenuItem("Exit");
    exit.setForeground(Color.BLUE);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions