JAVA learning Swing chapter button component JButton simple learning, swingjbutton

Source: Internet
Author: User

JAVA learning Swing chapter button component JButton simple learning, swingjbutton

Package com. swing; import java. awt. container; import java. awt. dimension; import java. awt. gridLayout; import java. awt. event. actionEvent; import java. awt. event. actionListener; import java.net. URL; import javax. swing. icon; import javax. swing. imageIcon; import javax. swing. JButton; import javax. swing. JFrame; import javax. swing. JOptionPane; import javax. swing. windowConstants;/*** 1: A button is a common component in Swing, used to trigger a specific action * Swin G provides multiple buttons, including the submit button, check box, and single-choice button. * these buttons are inherited from the AbstractButton class. ** 2: The submit button component (JButton) in Swing) the JButton object indicates that * JButton contains four main constructor Methods * parameter text. The icon indicates that the text label and icon are displayed. ** 3: This example uses two methods to create buttons, the first is to assign the button Icon and text * to the initialization button. You must first obtain the image path and then instantiate the path to the Icon, then, load the Button. * The second method is to create a Button object without defining icons and text, and then use the * setIcon () method to create an icon for the Button. * The setToolTipText () method sets the prompt text for the button, and you can move the cursor over the button. * The setBorderPainted () method sets whether the boundary shows * setMaximumSize () the size of the button set by the method is the same as that of the icon. The parameter type required by this method is * Dimension class object, so that the image is placed in the form just like a button, * You can also use the setEnabled () method to set whether the button is available ** @ author biexiansheng **/public class JButtonTest extends JFrame {public JButtonTest () {// define a constructor // obtain the URL of the image. Pay attention to the following two lines of code: URL url = JButtonTest. class. getResource ("imageButtoo.jpg"); Icon icon = new ImageIcon (url ); // instantiate the Icon object // set the grid layout manager to 3 rows, 2 columns, horizontal 5 Vertical 5 setLayout (new GridLayout )); // create Container container Container = getContentPane (); for (int I = 0; I <5; I ++) {// create button, set the button text and icon JButton jb = new JButton ("button" + I, icon); container. add (jb); // add the button to the container if (I % 2 = 0) {jb. setEnabled (false); // set some of the buttons to be unavailable} // instantiate JButton jb2 = new JButton (); // instantiate a button without text and image jb2.setMaximumSize (new Dimension (90,30); // set the button to the same size as the image jb2.setIcon (icon ); // set the icon jb2.setHideActionText (true) for the button; jb2.setToolTipText ("image button"); // The prompt for setting the button is jb2.setBorderPainted (false ); // The jb2.addActionListener (new ActionListener () {@ Override public void actionreceivmed (ActionEvent e) {// TODO Auto-generated method stub // The JOptionPane dialog box is displayed. showMessageDialog (null, "pop-up dialog box") ;}}); container. add (jb2); // add the button to the container setTitle ("submit button component button"); // set the window title setVisible (true ); // set the window visualization setSize (500,550); // set the window size // set the window closing method setdefaclocloseoperation (WindowConstants. EXIT_ON_CLOSE);} public static void main (String [] args) {// TODO Auto-generated method stub JButtonTest jb = new JButtonTest ();}}

The running result of the case is as follows:

 

 

 

Package com. swing; import java. awt. borderLayout; import java. awt. container; import java. awt. event. actionEvent; import java. awt. event. actionListener; import javax. swing. JCheckBox; import javax. swing. JFrame; import javax. swing. JPanel; import javax. swing. JScrollPane; import javax. swing. JTextArea; import javax. swing. windowConstants;/*** 1: check box component JCheckBox * is widely used in Swing components and has a square icon, A section of descriptive text is added. * The check box is different from the single-choice button. Each check box provides two statuses: select and not select. ** @ author biexiansheng **/public class CheckBoxTest extends JFrame {private JPanel jp1 = new JPanel (); // initialize the Panel object private JPanel jp2 = new JPanel (); //// if a scrolling panel is added, JTextArea must be added first and the size of the compiler must be specified. private JTextArea jt = new JTextArea (20, 50 ); // create the text domain component private JCheckBox jc1 = new JCheckBox (); // instantiate the check button private JCheckBox jc2 = new JCheckBox (); // private JCheckBox jc3 = new JCheckBox (); // public CheckBoxTest () {// set to a constructor Container container = getContentPane (); // create a container // set the layout manager Container. setLayout (new BorderLayout (); // initialize the border layout manager container. add (jp1, BorderLayout. NORTH); // Add to the container final JScrollPane js = new JScrollPane (jt); // create a JScrollPane panel object/* if you need to place multiple components in the JScrollPane panel, you need to place multiple components on the JPanel * and add the JPanel as a whole component to the JScrollPane component **/jp1.add (js); container. add (jp2, BorderLayout. SOUTH); jp2.add (jc1); jc1.addActionListener (new ActionListener () {@ Override public void actionreceivmed (ActionEvent e) {// TODO Auto-generated method stub jt. append ("check box 1 Selected \ n") ;}}); jp2.add (jc2); jc2.addActionListener (new ActionListener () {@ Override public void actionreceivmed (ActionEvent e) {// TODO Auto-generated method stub jt. append ("check box 2 Selected \ n") ;}}); jp2.add (jc3); jc3.addActionListener (new ActionListener () {@ Override public void actionreceivmed (ActionEvent e) {// TODO Auto-generated method stub jt. append ("check box 3 selected \ n") ;}}); setTitle ("use of check box"); setSize (600,650); setVisible (true); setdefaclocloseoperation (WindowConstants. EXIT_ON_CLOSE);} public static void main (String [] args) {// TODO Auto-generated method stub CheckBoxTest cb = new CheckBoxTest (); // call the constructor during instantiation }}

The instance running result is as follows:

In order to differentiate the running, special colors are added.

Be familiar with the color control of each panel

 

 

Package com. swing; import java. awt. container; import javax. swing. buttonGroup; import javax. swing. JButton; import javax. swing. JFrame; import javax. swing. JLabel; import javax. swing. JPanel; import javax. swing. JRadioButton; import javax. swing. windowConstants;/*** 1: by default, the single-choice button JRadioButton displays a circular icon, and usually places * Descriptive text next to the icon, while in the application, generally, multiple single-choice buttons are placed in the button group to make these single-choice buttons * show a certain function. When a user selects a single-choice button, other buttons in the button group will be automatically canceled. ** 2: the single-choice button is the JRadioButton class object in the Swing component, which is a subclass of JToggleButton * and the JToggleButton class is a subclass of the javasactbutton class, therefore, many methods to control single-choice buttons are in the * AbstractButton class. ** 3: A ButtonGroup class exists in Swing to generate a button group. * If you want to place all the single-choice buttons in the button group, you need to instantiate a JRadioButton object * and use this object to call add () method Add single-choice button * @ author biexiansheng **/public class extends JFrame {private JRadioButton jr1 = new JRadioButton (); private JRadioButton jr2 = new JRadioButton (); private JRadioButton jr3 = new JRadioButton (); // initialize a single-choice button // instantiate a single-choice button group private ButtonGroup group = new ButtonGroup (); private JPanel jp = new JPanel (); // initialize the Panel public JRadioButtonTest () {Container container = getContentPane (); // create a Container group. add (jr1); group. add (jr2); group. add (jr3); jp. add (jr1); jp. add (jr2); jp. add (jr3); // add the radio button to the Panel container. add (jp); // add the panel to the container // set the container feature setTitle ("simple exercise for single-choice buttons"); // The container title setVisible (true ); // visualized setSize (200,150); // container size // set the container closing method setdefaclocloseoperation (WindowConstants. EXIT_ON_CLOSE);} public static void main (String [] args) {// TODO Auto-generated method stub JRadioButtonTest jb = new JRadioButtonTest ();}}

The running result of a simple instance is as follows:

This example briefly describes how to implement the single-choice button without any response. It is only a Case demonstration.

 

Package com. swing; import java. awt. color; import java. awt. container; import java. awt. gridLayout; import javax. swing. buttonGroup; import javax. swing. JButton; import javax. swing. JCheckBox; import javax. swing. JFrame; import javax. swing. JLabel; import javax. swing. JPanel; import javax. swing. JRadioButton; import javax. swing. windowConstants; public class Test extends JFrame {private JRadioButton jr1; private extends jr2; private ButtonGroup bg1; private JCheckBox cb1; private JCheckBox cb2; private JCheckBox cb3; private JLabel jl1; private JLabel jl2; private JPanel jp1; private JPanel jp2; private JPanel jp3; private JButton jb1; private JButton jb2; public Test () {Container container = getContentPane (); // create a container cb1 = new JCheckBox ("basketball"); cb2 = new JCheckBox ("soccer"); cb3 = new JCheckBox ("Volleyball "); jl1 = new JLabel ("your favorite sports"); jl2 = new JLabel ("your gender"); jb1 = new JButton ("Submit "); jb2 = new JButton ("reset"); jr1 = new JRadioButton ("male"); jr2 = new JRadioButton ("female"); bg1 = new ButtonGroup (); setLayout (new GridLayout (3, 1); // set jp1 = new JPanel (); jp1.add (jl1); jp1.add (cb1 ); // Add the single-choice button to the Panel jp1.add (cb2), jp1.add (cb3), and container. add (jp1); jp2 = new JPanel (); jp2.add (jl2); bg1.add (jr1); // add the single button to the ButtonGroup bg1.add (jr2 ); jp2.add (jr1); jp2.add (jr2); container. add (jp2); jp3 = new JPanel (); jp3.add (jb1); jp3.add (jb2); container. add (jp3); setTitle ("button usage"); setSize (400,300); setVisible (true); setdefaclocloseoperation (WindowConstants. EXIT_ON_CLOSE);} public static void main (String [] args) {// TODO Auto-generated method stub new Test ();}}

The running result of the case is as follows:

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.