Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Computer Performance Engineering 10th European Workshop Epew 2013 Venice Italy September 17 2013 Proceedings

Authors: Maria Simonetta Balsamo ,William Knottenbelt ,Andrea Marin

2013 Edition

3642407242, 978-3642407246

More Books

Students also viewed these Programming questions

Question

1. Try oral, open-book, or group tests.

Answered: 1 week ago