Java GUI Reader Panel design

Source: Internet
Author: User
Tags event listener

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.