Java------Graphical Interface (i)

Source: Internet
Author: User

Import Java.awt.color;import java.awt.dimension;import Java.awt.font;import Java.awt.graphicsenvironment;import Java.awt.event.actionevent;import Java.awt.event.actionlistener;import Java.awt.event.mouseevent;import Java.awt.event.mouselistener;import Javax.swing.buttongroup;import Javax.swing.jcheckbox;import Javax.swing.jcombobox;import Javax.swing.jframe;import Javax.swing.jmenu;import Javax.swing.JMenuBar;import Javax.swing.jmenuitem;import Javax.swing.joptionpane;import Javax.swing.jpopupmenu;import Javax.swing.jradiobutton;import Javax.swing.jscrollpane;import Javax.swing.jtextarea;import Javax.swing.JToolBar; public class Editorjframe extends JFrame implements actionlistener,mouselistener{private JTextArea text;//text Area private JComboBox comboxname,comboxsize;//combo Box private jcheckbox chkbold,chkitalic;//glyph check box private jradiobutton[] Radiocolors ;//Color radio button Array Private jpopupmenu popmenu;//shortcut menu Private Boolean istrue=true;private color Color;private boolean bool=true; Public Editorjframe () {Super ("Text compiler"); This.setdefaultcloseoperation (exit_on_close);//dimension Dim=this.gettoolkit (). Getscreensize ();// Get screen resolution//this.setbounds (DIM.WIDTH/4, DIM.HEIGHT/4, DIM.WIDTH/2, DIM.HEIGHT/2); This.setsize (800, 600);// Set window size this.setlocationrelativeto (null);//Set window position centered text = new JTextArea ("Welcome ..."); This.getcontentpane (). Add ( New JScrollPane (text)); JToolBar toolbar=new JToolBar ();//toolbar, default horizontal orientation This.getcontentpane (). Add (ToolBar, "North");// Toolbar added to the Frame Contents pane North Graphicsenvironment ge=graphicsenvironment.getlocalgraphicsenvironment (); String[] Fontsname=ge.getavailablefontfamilynames ();//Get system font comboxname =new JComboBox (fontsname);// The combo box displays the system font Comboxname.addactionlistener (this);//Register the Action event listener Toolbar.add (Comboxname) on the combo box; String[] sizestr={"," "" "," "", "" "," "};comboxsize=new JComboBox (SIZESTR);//font combo box comboxsize.seteditable (True )///Set combo box as editable comboxsize.addactionlistener (this);//combo Box Register action event Listener toolbar.add (comboxsize); Chkbold =new Jcheckbox ("bold "); Chkbold.addactionlistener (this); Toolbar.add (chkbold); Chkitalic=new JcheCkbox ("italic"); Chkitalic.addactionlistener (this); Toolbar.add (chkitalic); string[] colorstrs={"Red", "green", "Blue"};radiocolors=new jradiobutton[colorstrs.length]; Buttongroup bgroup=new Buttongroup (); for (int i=0;i<radiocolors.length;i++) {radiocolors[i]=new JRadioButton ( Colorstrs[i]);//Color radio button Radiocolors[i].addactionlistener (this); Bgroup.add (Radiocolors[i]); Toolbar.add ( Radiocolors[i]);} Addmymenu (); Text.addmouselistener (this); This.setvisible (true);} private void Addmymenu () {JMenuBar menubar=new jmenubar (); This.setjmenubar (MenuBar);//Add menu bar string[] menustrs={"file", " Edit "," help "}; Jmenu[] menu=new jmenu[menustrs.length];for (int i=0;i<menu.length;i++) {menu[i]=new JMenu (menuStrs[i]);// Menu Menubar.add (Menu[i]);} Menu[0].add (New JMenuItem ("Open")); Menu[0].add (New JMenuItem ("Save")); Menu[0].addseparator ();//Add menu item And with Exit JMenuItem menuitemexit=new JMenuItem ("Exit") separated by line; Menuitemexit.setactioncommand ("Exit"); Menuitemexit.addactionlistener (this); Menu[0].add (Menuitemexit); JMenuItem menucolor=new jmenuitem ("color"); menu[1].aDD (menucolor); Menucolor.addactionlistener (this);p opmenu=new jpopupmenu ();//shortcut menu string[] menuitemstrs={"Cut", "copy", "Paste"}; Jmenuitem[] popmenuitems=new jmenuitem[menuitemstrs.length];for (int i=0;i<popmenuitems.length;i++) { Popmenuitems[i]=new JMenuItem (menuitemstrs[i]);p Opmenu.add (popmenuitems[i]);p Opmenuitems[i].addactionlistener ( this);} Text.add (Popmenu);} public static void Main (string[] args) {new Editorjframe ();} public void mouseclicked (MouseEvent e) {if (E.getmodifiers () ==mouseevent.button3_mask) {//Display shortcut menu Popmenu.show (text, E.getx (), e.gety ());}}  public void mousepressed (MouseEvent e) {}public void mousereleased (MouseEvent e) {}public void mouseentered (MouseEvent e) {}public void mouseexited (MouseEvent e) {}public void actionperformed (ActionEvent e) {if (E.getsource () instanceof Jcombo Box | | E.getsource () instanceof Jcheckbox) {//control input font size is a valid number try{bool=!bool;//prevent pop-up two exceptions if (bool) {return;} String Fontname= (String) comboxname.getselecteditem (); int Size=integer.parseint (string) comboxsize.geTselecteditem ()); if (size<4 | | size>120) {istrue=false;throw new Exception ("Sizeexception");} Font Font=text.getfont (); int Style=font.getstyle (); if (E.getsource () ==chkbold) {//Glyph integer No. 0 is bold, first bit represents italic style=style^ 1;} if (E.getsource () ==chkitalic) {style=style^2;} Text.setfont (New Font (fontname,style,size)); bool=true;} catch (NumberFormatException E1) {Joptionpane.showmessagedialog (this, "\" "+ (String) Comboxsize.getselecteditem () +" \ "cannot be converted to integers, please re-enter!" "),//\" denotes quotation marks}catch (Exception E2) {if (E2.getmessage (). Equalsignorecase ("Sizeexception")) { Joptionpane.showmessagedialog (This, "the font is not suitable, please re-enter!") ");}}} if (E.getsource () instanceof Jradiobutton) {//press to respond to radio button if (E.getsource () ==radiocolors[0]) {color=new color (255,0,0) ;} if (E.getsource () ==radiocolors[1]) {color=new color (0,255,0);} if (E.getsource () ==radiocolors[2]) {color=new color (0,0,255);} Text.setforeground (color);} if (E.getsource () ==comboxsize) {if (!istrue) {//If not compliant, do not place the font size in the font-size combo box Istrue=true;return;} String size= (String) comboxsize.getselecteditem (); int I=0,n=comboxSize.getitemcount (); while (I<n&&integer.parseint (size) >=integer.parseint ((String) Comboxsize.getitemat (i)) {if (Integer.parseint (size) ==integer.parseint ((String) Comboxsize.getitemat (i)) {return ;} i++;} Comboxsize.insertitemat (size, i);}}}



Java------Graphical Interface (i)

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.