Packagecom.swing;ImportJava.awt.Container;Importjava.awt.Dimension;Importjava.awt.GridLayout;Importjava.awt.event.ActionEvent;ImportJava.awt.event.ActionListener;ImportJava.net.URL;ImportJavax.swing.Icon;ImportJavax.swing.ImageIcon;ImportJavax.swing.JButton;ImportJavax.swing.JFrame;ImportJavax.swing.JOptionPane;Importjavax.swing.WindowConstants;/*** 1: Buttons are a more common component in swing to trigger specific actions * Swing provides a variety of buttons, including Submit button, check box, radio button, etc. * These buttons are inherited from the AbstractButton class * * 2:swing in the Submit button component (JButton) represented by the JButton object * JButton contains 4 main construction methods * Parameters Text,icon respectively represents the display text label and icon * 3: This example uses two ways to create a button, the first is to give the button icon and text when the button is initialized * This initialization must first obtain the picture path, then instantiate the path to the icon and then load it in the button * The second way is to first create a button object that does not have an icon and text defined, and then use the * SetIcon () method to customize an icon for the button. * Settooltiptext () method is to set the hint text for the button, the mouse hover over the button can be * setborderpainted () method to set whether the boundary display * Setmaximumsize () method set the size of the button and the size of the icon is the same, The type of parameter required by this class of method is the * Dimension class object, so that it looks like the button is placed in the form, * You can also use the setenabled () method to set whether the button is available *@authorBiexiansheng **/ Public classJbuttontestextendsjframe{ PublicJbuttontest () {//defining a construction method//get the URL where the image is located the following 2 lines of code require additional attentionURL Url=jbuttontest.class. GetResource ("Imagebuttoo.jpg"); Icon Icon=NewImageIcon (URL);//instantiate an Icon object//Set Grid layout manager 3 row 2 column horizontal 5 vertical 5SetLayout (NewGridLayout (3,2,5,5)); //Create a containerContainer container=Getcontentpane (); for(inti=0;i<5;i++) {//Create button, set button text and icon at the same timeJButton jb=NewJButton ("button" +I,icon); Container.add (JB);//Add a button to a container if(i%2==0) {jb.setenabled (false);//set Some of these buttons to be unavailable } }//instantiation of two buttons in the upper and lower positionsJButton jb2=NewJButton ();//instantiate a button without text and picturesJb2.setmaximumsize (NewDimension (90,30));//Set button and picture are the same sizeJb2.seticon (icon);//setting icons for buttonsJb2.sethideactiontext (true); Jb2.settooltiptext ("Picture button");//Set button prompts for textJb2.setborderpainted (false);//Set button bounds not displayedJb2.addactionlistener (NewActionListener () {@Override Public voidactionperformed (ActionEvent e) {//TODO auto-generated Method Stub//Popup dialog boxJoptionpane.showmessagedialog (NULL, "Pop-up dialog box"); } }); Container.add (JB2);//Add a button to a containerSettitle ("Submit button Component button Small test sledgehammer");//Set Window captionSetVisible (true);//Set Window visualizationSetSize (500,550);//set the size of the window//to set how the window is closedsetdefaultcloseoperation (windowconstants.exit_on_close); } Public Static voidMain (string[] args) {//TODO auto-generated Method StubJbuttontest jb=Newjbuttontest (); }}
The results of the case run as follows
Java Learning Swing Chapter button component JButton Simple learning