Question: In Java I'm using the Eclipse IDE and am not able to get thisapplication to write to the file or change the color or exit

In Java

I'm using the Eclipse IDE and am not able to get thisapplication to write to the file or change the color or exit theprogram. When options isselected and date & time are selectednothing happens. This is the same for all menu options. How do Iget this to work? This is the assignment:

Create a user interface that has a top bar that includes a menu.The menu should have four items.

  • When the user selects the first menu option, then the date andtime should be printed in a text box.
  • When the user selects the second menu option, then the text boxcontents should be written to a text file named "log.txt."
  • When the user selects the third menu item then the framebackground color changes to random color hue of the color green.See https://www.w3schools.com/colors/colors_picker.asp (Linksto an external site.)
  • When the user selects the fourth menu option then the programexits.

Is anyone able to help me to get this working? This isthe code I can't get to work properly.

package application;

import javafx.event.ActionEvent;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.time.format.DateTimeFormatter;
import java.time.LocalDateTime;
import java.io.*;

/**
*
* @author xxxxx
*/
class MenuInterface implements ActionListener {
JMenuItem m1, m2, m3, m4;
JMenuBar mb;
JTextField txtbox;
JMenu menu;
JFrame frm;

public MenuInterface() {
frm = new JFrame();
frm.setLayout(null);

mb = new JMenuBar();
txtbox = new JTextField();
menu = new JMenu("options");

m1 = new JMenuItem("Date & Time");
m2 = new JMenuItem("Write_Into_File");
m3 = new JMenuItem("ChangeFrameColor");
m4 = new JMenuItem("Exit");

menu.add(m1);
menu.add(m2);
menu.add(m3);
menu.add(m4);

mb.add(menu);
frm.setJMenuBar(mb);

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

txtbox.setBounds(150, 50, 150, 30);
frm.add(txtbox);
frm.setVisible(true);
frm.setSize(500, 350);

}

public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == m1) {
DateTimeFormatter dt =DateTimeFormatter.ofPattern("yyyy/mm/dd hh:mm:ss");
LocalDateTime current =LocalDateTime.now();
txtbox.setText(dt.format(current) +"");
}

if (ae.getSource() == m2) {
String s1 = txtbox.getText();
try {
FileWriter fw = newFileWriter("Log.txt");
fw.write(s1);
fw.close();
} catch (Exception e1) {
txtbox.setText("Exception is " + e1);
}
}

if (ae.getSource() == m3) {
frm.getContentPane().setBackground(Color.green);
}

if (ae.getSource() == m4) {
frm.setVisible(false);
}

frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String args[]) {
MenuInterface md = new MenuInterface();
}

public void actionPerformed(java.awt.event.ActionEventarg0) {

}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!