GUI (3) Procedure for a menu

xiaoxiao2021-04-11  368

Import java.awt. *; import java.awt.event. *; import javax.swing. *; import *; class aa {public static void main (string [] args) {final jframe f = new jframe ("aa"); // jframe's default layout manager is borderLayout () F.setSize (600, 400); F.SetLocation (100, 100); Final JtextArea TF = New jtextarea (); // Add a text domain, will below open files, read into the JScrollPane scrollPane = new JScrollPane (tf, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); // add a scroll bar to a text box f.add (scrollPane, "Center"); // the scrollable Text box is added to the JFrame on F.AddWindowListener (New Windowadapter () {public void windowclosing (windowevent e) {system.exit (0);}});

JMenuBar MB = new jmenubar (); jMenu m1 = new jMenu ("file"); jMenu m2 = new jMenu ("edit"); jMenuitem mi1 = new jMenuitem ("new"); jMenuItem Mi2 = new Jmenuitem ("open" ); Mi2.addActionListener (new activityListener () {// point Open, set an open dialog public void actionPerformed (ActionEvent E) {fileDialog fd = New FileDialog (F, "Open File Dialog", FileDialog.Load); FD .setVisible (true); String strFile = fd.getDirectory () fd.getFile (); if (strFile = null!) {try {FileInputStream fis = new FileInputStream (strFile); StringBuffer s = new StringBuffer (); int c WHILE ((c = ())! = - 1) {s = S.Append ((char) c);} Byte B [] = S.toString (). GetBytes ("ISO8859-1") TF.Append (new string (b)); fis.close ();} catch (exception ex) {ex.printstacktrace ();}}} }); JMenuitem mi3 = new jMenuitem ("save"); jMenuitem mi4 = new jMenuItem ("exit"); MI4.AddActionListener (new actionListener () {// sets EXIT's listener, exit public void actionPerformed (ActionEvent E) {system.exit (0);}}); jMenuitem mi5 = new jmenuitem ("copy"); jMenuitem mi6 = new jmenuitem ("paste"); m1.add (mi1); m1.add (Mi2) ); M1.Add (mi3); m2.Add (mi5); m2.add (mi6); Mb.Add (m1); mb.add (m2); F.setjmenubar (MB) ; // setmenubar () is used to set the menu bar f.setvisible (true);}}

-------------------------------------------------- -------------------------------------------------- ---------------------------- Component A component class, is the abstract base class of all Java graphics interface components. It is derived with Container A container object with a container class is a component Container that can accommodate other components. The Window window class is derived, and a Frame Framework window is derived by WINDOW.

Experience: When using JBuilder, if the project catalog has space, some problems often occur when compiling

// Modify the content btn.addactinListener (new actionListener) {Public Void ActionPerformed ("AA"). SetLabel ("aa"); // GetSource () Method, you can get an event original}}

Menu menu bar's class MenuBar menu class Menu menu item class MenuItem

Create a file dialog box FileDialog (File Parent, String Title) FileDialog (File Parent, String Title) FileDialog (File Parent, String Title, INT Mode) // Mode, FileDialog (FileDialog (INT MODE) // Mode The dialog box is the Save dialog box for FileDialog.sava. You can also call the SetMode () Settings dialog box is to open the dialog, or close the dialog box with a method getFile () Get the open file name and one method getDirectory () Return Current Document List of Choice drop-down list box

JFRAME Method SetDefaultCloseOperation Set the default shutdown operation static int exit_on_close Exit the default window after the application is closed. JFRAME JF = New JFrame ("aaa"); jf.setdefaultcloseOperation (jframe.exit_on_close);

Import java.awt. *; import java.awt.event. *; class aa {public static void main (string [] args) {frame f = new frame ("aa") F.setsize (600, 400); F.SetLocation (100, 100); f.addwindowlistener (new windowadapter () {public void windowclosing (windowevent e) {system.exit (0);}});


New Post(0)