Import java.awt. *;
Import java.awt.event. *;
Import javax.swing. *;
Common class JNotePadUI extension JFrame {
Private JMenuItem menuOpen
Private JMenuItem menu saving;
Private JMenuItem menuSaveAs
Private JMenuItem menuClose
Private JMenu editMenu
Private JMenuItem menuCut
Private JMenuItem menuCopy
Private JMenuItem menuPaste
Private JMenuItem menuAbout
Private JTextArea textArea
Private JLabel stateBar
Private JFileChooser fileChooser
Private JPopupMenu popUpMenu
Public JNotePadUI() {
Super ("new text file");
setUpUIComponent();
setUpEventListener();
set visible(true);
}
Private void setUpUIComponent() {
setSize(640,480);
//menu bar
JMenuBar menuBar = new JMenuBar();
//Set the file menu
JMenu file menu = new JMenu(" file ");
menu open = new JMenuItem(" open ");
//shortcut key settings
menuOpen.setAccelerator(
keystone . get keystone(
Key events. VK_O,
Enter an event. CTRL _ MASK));
menu save = new JMenuItem(" save ");
menuSave.setAccelerator(
keystone . get keystone(
Key events. VK,
Enter an event. CTRL _ MASK));
MenuSaveAs = new JMenuItem ("save as");
menu close = new JMenuItem(" close ");
menuClose.setAccelerator(
keystone . get keystone(
Key events. VK_Q,
Enter an event. CTRL _ MASK));
file menu . add(menu open);
file menu . add separator(); //parting line
file menu . add(menu save);
file menu . add(menus aveas);
file menu . add separator(); //parting line
file menu . add(menu close);
//Set the edit menu
JMenu edit menu = new JMenu(" edit ");
menu cut = new JMenuItem(" cut ");
menuCut.setAccelerator(
keystone . get keystone(key event。 VK X,
Enter an event. CTRL _ MASK));
menu copy = new JMenuItem(" copy ");
menuCopy.setAccelerator(
keystone . get keystone(key event。 VK special zone,
Enter an event. CTRL _ MASK));
menu paste = new JMenuItem(" paste ");
Menupast.set accelerator (
keystone . get keystone(key event。 VK v,
Enter an event. CTRL _ MASK));
edit menu . add(menu cut);
edit menu . add(menu copy);
editmenu . add(menu paste);
//Set the About menu
JMenu about menu = new JMenu(" about ");
MenuAbout = new JMenuItem ("about jnotepad");
about menu . add(menu about);
MenuBar.add (file menu);
menubar . add(edit menu);
MenuBar.add (about menu);
SetJMenuBar (menu bar);
//Text editing area
textArea = new JTextArea();
TextArea.setFont (new font ("√", font. Plain,16));
textarea . setline wrap(true);
JScrollPane panel = new JScrollPane(textArea,
ScrollPaneConstants。 VERTICAL _ SCROLLBAR _ AS _ NEEDED,
ScrollPaneConstants。 Horizontal _ scroll bar _ never);
container content pane = getContentPane();
contentPane.add(panel,BorderLayout。 Center);
//Status bar
StateBar = new JLabel ("unmodified");
state bar . sethorizontal alignment(swing constants。 Left);
stateBar.setBorder(
border factory . createetchedborder());
contentPane.add(stateBar,BorderLayout。 South);
popup menu = edit menu . getpopup menu();
file chooser = new jfile chooser();
}
Private void setUpEventListener() {
//Event handling of pressing the window close button
addWindowListener(
New WindowAdapter() {
public void window closing(window event e){
close file();
}
}
);
//Menu-Open
menuOpen.addActionListener(
New ActionListener() {
public void action performed(action event e){
openFile();
}
}
);
//Menu-Save
menuSave.addActionListener(
New ActionListener() {
public void action performed(action event e){
save file();
}
}
);
//Menu-Save As
menuSaveAs.addActionListener(
New ActionListener() {
public void action performed(action event e){
save file as();
}
}
);
//Menu-Close File
menuClose.addActionListener(
New ActionListener() {
public void action performed(action event e){
close file();
}
}
);
//menu cutting
menuCut.addActionListener(
New ActionListener() {
public void action performed(action event e){
cut();
}
}
);
//Menu-Copy
menuCopy.addActionListener(
New ActionListener() {
public void action performed(action event e){
copy();
}
}
);
//Menu-Paste
menuPaste.addActionListener(
New ActionListener() {
public void action performed(action event e){
paste();
}
}
);
//Menu-About
menuAbout.addActionListener(
New ActionListener() {
public void action performed(action event e){
//Show dialog box
JOptionPane.showOptionDialog (empty,
"Program name: \n JNotePad \n"+
"Programming: \n? \n" +
Introduction: \ nSimple text editor \ n,
About JNotePad,
JOptionPane。 Default option,
JOptionPane。 Information _ message,
null,null,null);
}
}
);
//Edit Region Keyboard Event
textArea.addKeyListener(
New key adapter () {
Keyevent e typed by public void (
process textarea();
}
}
);
//Edit regional mouse events
textArea.addMouseListener(
New mouse adapter () {
public void mouse released(mouse event e){
if(e.getButton() == MouseEvent。 Button 3)
popUpMenu.show(editMenu,e.getX(),e . gety());
}
public void mouse clicked(mouse event e){
if(e.getButton() == MouseEvent。 Button 1)
popup menu . set visible(false);
}
}
);
}
Private void openFile() {
If(isCurrentFileSaved()) {// Is the file in a saved state?
open(); //open
}
Otherwise {
//Show dialog box
int option = jooptionpane . showconfirmdialog(
Empty, "The file has been modified. Do you want to save it? ”,
"Save the file?" ,JOptionPane。 Yes _ No _ option,
JOptionPane。 WARNING_MESSAGE,null);
Switch (optional)
//Confirm file saving
case JOptionPane。 Yes _ Option:
save file(); //Save the file
Break;
//Give up saving the file
case JOptionPane。 NO _ option:
open();
Break;
}
}
}
private boolean isCurrentFileSaved(){
If(stateBar.getText()。 Equal to ("unmodified") {
Return true
}
Otherwise {
Returns false
}
}
Private void open() {
// fileChooser is an instance of JFileChooser.
//Displays the file selection dialog box.
int option = file chooser . showdialog(null,null);
//The user presses the Enter key.
if(option == JFileChooser。 APPROVE_OPTION) {
/*
TODO: Add code to read the file.
*/
}
}
Private void saveFile() {
/*
TODO: Add code to save the file.
*/
}
Private void saveFileAs() {
/*
TODO: Add code saved as.
*/
}
Private void closeFile() {
//Did you save the file?
if(isCurrentFileSaved()) {
//Release window resources and then close the program.
dispose();
}
Otherwise {
int option = jooptionpane . showconfirmdialog(
Empty, "The file has been modified. Do you want to save it? ”,
"Save the file?" ,JOptionPane。 Yes _ No _ option,
JOptionPane。 WARNING_MESSAGE,null);
Switch (optional)
case JOptionPane。 Yes _ Option:
save file();
Break;
case JOptionPane。 NO _ option:
dispose();
}
}
}
Private void cut() {
textarea . cut();
state bar . settext(" modified ");
popup menu . set visible(false);
}
Private void copy () {
textarea . copy();
popup menu . set visible(false);
}
Private void paste () {
textarea . paste();
state bar . settext(" modified ");
popup menu . set visible(false);
}
Private void processTextArea() {
state bar . settext(" modified ");
}
Public static void main(String[] args) {
new JNotePadUI();
}
}