I need help with some code. I'm new here and I program in Java since 2 months. I'm trying to create an editor with Java GUI, actually, I've made the "Save" button and the "Close" button. But HOW CAN I CREATE THE OPEN BUTTON?? Here is the code:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
@SuppressWarnings("serial")
public class SimplestEditor extends JFrame implements ActionListener {
public static void main(String[] args) {
new SimplestEditor().setVisible(true);
}
private JTextField textField;
public SimplestEditor() {
super("Simplest Editor");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(400, 300));
pack();
setLocationRelativeTo(null);
// creates the menu bar
JMenuBar menuBar = new JMenuBar();
// creates the file menu
JMenu fileMenu = new JMenu("File");
// creates a new menu item
JMenuItem saveMenuItem = new JMenuItem("Save");
saveMenuItem.addActionListener(this);
fileMenu.add(saveMenuItem);
// creates a new menu item
JMenuItem exitMenuItem = new JMenuItem("Exit");
exitMenuItem.addActionListener(this);
fileMenu.add(exitMenuItem);
// creates the edit menu
JMenu editMenu = new JMenu("Edit");
// creates a new submenu
JMenu changeFontColorMenuItem = new JMenu("Font Color");
// red menu item
JMenuItem redMenuItem = new JMenuItem("Red");
redMenuItem.addActionListener(this);
changeFontColorMenuItem.add(redMenuItem);
// blue menu item
JMenuItem blueMenuItem = new JMenuItem("Blue");
blueMenuItem.addActionListener(this);
changeFontColorMenuItem.add(blueMenuItem);
// green menu item
JMenuItem greenMenuItem = new JMenuItem("Green");
greenMenuItem.addActionListener(this);
changeFontColorMenuItem.add(greenMenuItem);
// adds the menu item to the edit menu
editMenu.add(changeFontColorMenuItem);
// creates a new menu item
JMenuItem clearMenuItem = new JMenuItem("Clear");
clearMenuItem.addActionListener(this);
editMenu.add(clearMenuItem);
// add the menus to the menu bar
menuBar.add(fileMenu);
menuBar.add(editMenu);
// sets the menu bar
setJMenuBar(menuBar);
// creates the text field and adds it to the panel
textField = new JTextField();
JPanel panel = new JPanel(new BorderLayout());
panel.add(textField, BorderLayout.CENTER);
// sets the content panel
setContentPane(panel);
}
/**
* Saves the contents of the text field into a file.
*/
protected void save() throws IOException {
// creates a file chooser to allow the user to select the file name and
// folder.
JFileChooser dialog = new JFileChooser();
// default file name is "file.txt"
File file = new File("file.txt");
dialog.setSelectedFile(file);
// displays the dialog
if (dialog.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
// gets the selected file
file = dialog.getSelectedFile();
// creates a file writer
FileWriter writer = new FileWriter(file);
// writes the contents of the text field into file
writer.append(textField.getText());
// closes the file writer
writer.close();
}
}
@Override
public void actionPerformed(ActionEvent e) {
String action = e.getActionCommand();
if ("Save".equals(action)) {
try {
save();
JOptionPane.showMessageDialog(null, "File has been saved.");
} catch (IOException e1) {
JOptionPane.showMessageDialog(null, "Could not save the file.");
}
} else if ("Exit".equals(action)) {
System.exit(0);
} else if ("Red".equals(action)) {
textField.setForeground(Color.red);
} else if ("Blue".equals(action)) {
textField.setForeground(Color.blue);
} else if ("Green".equals(action)) {
textField.setForeground(Color.green);
} else if ("Clear".equals(action)) {
textField.setText(null);
}
}
}
Please help me guys! Thanks in advance!

New Topic/Question
Reply




MultiQuote




|