The main thing is to mimic a panel design like Notepad, but it also improves
Class Name: MyNote
Main package: java.awt*;
javax.swing*;
Code:
Import Java.awt.BorderLayout;
Import Java.awt.Container;
Import Java.awt.event.ActionListener;
Import Javax.swing.JButton;
Import Javax.swing.JFrame;
Import Javax.swing.JMenu;
Import Javax.swing.JMenuBar;
Import Javax.swing.JMenuItem;
Import Javax.swing.JTextArea;
Import Javax.swing.JToolBar;
public class MyNote extends jframe{
Private ActionListener ActionListener = null;
Private JTextArea TextArea = null;
Public MyNote (String title) {
TODO auto-generated Constructor stub
To create a menu event listener object
ActionListener = new Menulistener (this);
This.settitle (title);
This.setjmenubar (Createmenubar ());//Create menu bar
Container Container = This.getcontentpane ();
Texteditframe editframe = new Texteditframe ();
This.textarea = Editframe.gettextedit ();
Set up border layout management
Container.setlayout (New BorderLayout ());
Toolbar position
Container.add (Createtoolbar (), Borderlayout.north);
Container.add (Editframe,borderlayout.center);
This.setdefaultcloseoperation (this. Exit_on_close);
This.setbounds (300, 150, 600, 400);
This.setvisible (TRUE);
}
/**
* Create menu bar
* @param menuItem
* @param menutitle
* @return
*/
Private JMenu CreateMenu (string[] menuitems,string menutitle) {
JMenu menu = new JMenu ();
JMenuItem menuItem = null;
for (int i = 0; i < menuitems.length; i + +) {
// If the menu item title text is separator, a split row is added
// Otherwise add a menu
if (Menuitems[i].equals ("Separator")) {
Menu.addseparator ();
}else{
MenuItem = new JMenuItem ();
Menuitem.settext (Menuitems[i]);//Set menu item title
Menuitem.addactionlistener (ActionListener);//Monitor
Menu.add (MenuItem);
}
}
Menu.settext (Menutitle);
return menu;
}
Private JMenuBar Createmenubar () {
String[] Filemenutitle = {"New", "Open", "Separator", "Saved", "Save As", "Separator", "Exit"};//set menu item
String[] Editmenutitle = {"Copy", "Cut", "paste"};
String[] Helpmenutitle = {"About"};
Create a menu bar object
JMenuBar menubar = new JMenuBar ();
JMenu menu = null;
Create File menu
menu = CreateMenu (Filemenutitle, "file (F)");
Menubar.add (menu);
menu = CreateMenu (Editmenutitle, "edit (E)");
Menubar.add (menu);
menu = CreateMenu (Helpmenutitle, "Help (H)");
Menubar.add (menu);
return menubar;
}
Creating the Editor's toolbar
Private JToolBar Createtoolbar () {
Defines the caption text for a toolbar button
String[] filetitle={"new", "Open", "save", "exit"};
String[] edittitle={"Normal mode", "Eye protection Mode", "Night Mode"};
JToolBar ToolBar = new JToolBar ();//Creating a Toolbar object
JButton button = null;//buttons
Add a button to a toolbar
for (int i = 0; i < edittitle.length; i++) {
button = new JButton (Edittitle[i]);
Button.addactionlistener (ActionListener);
Toolbar.add (button);
}
Toolbar.setvisible (TRUE);
return toolBar;
}
/**
* @param args
*/
public static void Main (string[] args) {
TODO auto-generated Method Stub
MyNote Note = new MyNote ("text editor");
}
Public JTextArea Gettextarea () {
return this.textarea;
}
}
Follow-up monitoring to be continued ...
Java GUI Reader Panel design