Packageswingtest;Importjava.awt.BorderLayout;Importjava.awt.event.ActionEvent;ImportJava.awt.event.ActionListener;Importjavax.swing.*; Public classHelloAppextendsjframe{ Public Static voidmain (String [] args) {Swingutilities.invokelater (NewRunnable () { Public voidrun () {HelloApp Inst=NewHelloApp (); Inst.setlocationrelativeto (NULL); Inst.setvisible (true); } } ); } PublicHelloApp () {Super(); Initgui (); } Private voidInitgui () {Try{setdefaultcloseoperation (windowconstants.dispose_on_close); JMenuBar mbar=NewJMenuBar (); Setjmenubar (mbar); JMenu Mfile=NewJMenu (); Mbar.add (Mfile); Mfile.settext ("File"); JMenuItem Showhello=NewJMenuItem (); Mfile.add (Showhello); Showhello.settext ("Hello"); Showhello.addactionlistener (NewActionListener () { Public voidactionperformed (ActionEvent e) {Joptionpane.showmessagedialog (NULL, "Hello World", "This is a message dialog", joptionpane.default_option); } } ); JMenuItem Exitem=NewJMenuItem (); Mfile.add (Exitem); Exitem.settext ("Exit"); Pack (); SetSize (400,300); JToolBar ToolBar=NewJToolBar (); Getcontentpane (). Add (Toolbar,borderlayout.south); JButton Binbar=NewJButton (); Toolbar.add (Binbar); Binbar.settext ("Say Hello"); JTextField TEXTF=NewJTextField (); Getcontentpane (). Add (Textf,borderlayout.north); Textf.settext ("Please input the text"); } Catch(Exception e) {e.printstacktrace (); } } }
Swing Programming for Java program testing