Hi, I have a JAVA PROGRAMMING assignment i need solutions.Firstly this is my GUI
ID: 3736015 • Letter: H
Question
Hi,
I have a JAVA PROGRAMMING assignment i need solutions.Firstly this is my GUI assignment.We should make a TextEditor
rules below,and i attached some pictures.
----------------------------------------------------------------------------------------
You are asked to make a simple TextEditor in this assignment. Your view of the editor should look like Figure 1
New: Creates a new file tab by taking the file name.
Open: Open a saved file
Save: Save the file to the specified location.
Save as: It saves the file with a different name
Close File: Closes the active file tab.
Exit: Exit Program.
Copy : Copies the selected text in the file.( highlighted)
Paste : Pastes the copied text in the field.
Delete : Deletes the selected text in the file.( highlighted)
Select All: Select all text.( highlighted)
Undo : Undo the last operation in the file. (Enough for the last 10 operations)
Redo : Repeats the operation that was retrieved in the file.
Preferences : When this option is clicked, a frame will be opened as in Figure 3 (b).
Do not split words over two lines: When this option is selected, each insertion that you make at the end of the text being worked on goes to a single line. Your changes appear on a single line.
Display line numbers: It starts and numbers each line in the Text editor.
Finally on the Search menu::
Find : Search in the entered text. A window should be opened as shown in Figure 4 (a) to enter the term when clicked.
Find and Replace : The entered term replaces the entered term for the exchange. A window like Figure 4 (b) should be opened in order to enter the term when clicked.
File Edit Search Dosyal.txt Dosya2 txt In computer science, a graphical user interface or GUL is a type of interface that allows users to interact with electronic devices through graphical icons and visual indicators such as secondary notation, as opposed to text-based interfaces, typed command labels or text navigation. GUls were introduced in reaction to the perceived steep learning curve of command-line interfaces (CLIs), which require commands to be typed on the keyboard The actions in a Gut are usualy performed through direct manipulation of the graphical elements In addition to computers, Guis can be found in hand-held devices suuch as MP3 players, portable media players, gaming devices, smartphones and smaller household, office and industrial equipment. The term "GuI tends not to be applied to other low-resolution types of interfaces with display resolutions, such as video games where HUD[61 is preferred), or not restricted to flat screens, where HUDl61 is preferred), restricted like volumetric displays because the term is restricted t generic information, in the tradition of the computer science research at the PARC (Palo Alto Research Center), Designing the visual composition and temporal behavior of a GUI is an important part of software application programming in the area of human-computer interaction. its goal is to enhance the efficiency and ease of use for the underlying logical design of a stored program, a design discipline known as usability,. Methods of user-centered design are used to ensure that the visual language introduced in the design is well tailored to the tasks Eigure : Text EditorExplanation / Answer
package editors;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.text.*;
public class TextEditor extends JFrame {
private JTextArea area = new JTextArea(20,120);
private JFileChooser dialog = new JFileChooser(System.getProperty("user.dir"));
private String currentFile = "Untitled";
private boolean changed = false;
public TextEditor() {
area.setFont(new Font("Monospaced",Font.PLAIN,12));
JScrollPane scroll = new JScrollPane(area,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
add(scroll,BorderLayout.CENTER);
JMenuBar JMB = new JMenuBar();
setJMenuBar(JMB);
JMenu file = new JMenu("File");
JMenu edit = new JMenu("Edit");
JMenu search = new JMenu("Search");
JMB.add(file); JMB.add(edit);JMB.add(search);
file.add(New);
file.add(Open);
file.add(Save);
file.add(SaveAs);
file.add(Exit);
file.addSeparator();
for(int i=0; i<=4; i++)
file.getItem(i).setIcon(null);
edit.add(Cut);edit.add(Copy);edit.add(Paste);
edit.add(AllSelect);
edit.getItem(0).setText("Cut");
edit.getItem(1).setText("Copy");
edit.getItem(2).setText("Paste");
edit.getItem(2).setText("Select All");
search.add(SEARCH);
search.getItem(0).setText("Search");
Save.setEnabled(false);
SaveAs.setEnabled(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
area.addKeyListener(k1);
setTitle(currentFile);
setVisible(true);
}
private KeyListener k1 = new KeyAdapter() {
public void keyPressed(KeyEvent e) {
changed = true;
Save.setEnabled(true);
SaveAs.setEnabled(true);
}
};
Action New = new AbstractAction("New") {
public void actionPerformed(ActionEvent e) {
area.setText("****Please start writing from here*****");
SaveAs.setEnabled(true);
}
};
Action SEARCH = new AbstractAction("SEARCH") {
public void actionPerformed(ActionEvent e) {
TextField tf=new TextField();
Label l=new Label();
area.add(l);
area.add(tf);
final JButton button2 = new JButton("SEARCH");
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String text = area.getSelectedText();
if(text.contains(tf.getText()))
l.setText("Word is found");
else
l.setText("Word is not found");
}
});
}
};
Action Exit = new AbstractAction("Exit") {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
};
Action AllSelect = new AbstractAction("AllSelect") {
public void actionPerformed(ActionEvent e) {
area.requestFocusInWindow();
area.selectAll();
}
};
Action Open = new AbstractAction("Open") {
public void actionPerformed(ActionEvent e) {
saveOld();
if(dialog.showOpenDialog(null)==JFileChooser.APPROVE_OPTION) {
readInFile(dialog.getSelectedFile().getAbsolutePath());
}
SaveAs.setEnabled(true);
}
};
Action Save = new AbstractAction("Save") {
public void actionPerformed(ActionEvent e) {
if(!currentFile.equals("Untitled"))
saveFile(currentFile);
else
saveFileAs();
}
};
Action SaveAs = new AbstractAction("Save as...") {
public void actionPerformed(ActionEvent e) {
saveFileAs();
}
};
ActionMap m = area.getActionMap();
Action Cut = m.get(DefaultEditorKit.cutAction);
Action Copy = m.get(DefaultEditorKit.copyAction);
Action Paste = m.get(DefaultEditorKit.pasteAction);
private void saveFileAs() {
if(dialog.showSaveDialog(null)==JFileChooser.APPROVE_OPTION)
saveFile(dialog.getSelectedFile().getAbsolutePath());
}
private void saveOld() {
if(changed) {
if(JOptionPane.showConfirmDialog(this, "Would you like to save "+ currentFile +" ?","Save",JOptionPane.YES_NO_OPTION)== JOptionPane.YES_OPTION)
saveFile(currentFile);
}
}
private void readInFile(String fileName) {
try {
FileReader r = new FileReader(fileName);
area.read(r,null);
r.close();
currentFile = fileName;
setTitle(currentFile);
changed = false;
}
catch(IOException e) {
Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog(this,"Editor can't find the file called "+fileName);
}
}
private void saveFile(String fileName) {
try {
FileWriter w = new FileWriter(fileName);
area.write(w);
w.close();
currentFile = fileName;
setTitle(currentFile);
changed = false;
Save.setEnabled(false);
}
catch(IOException e) {
}
}
public static void main(String[] arg) {
new TextEditor();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.