In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article, Xiaobian introduces in detail how to apply Java Swing's JButton components, with detailed content, clear steps and proper handling of details. I hope this article "how to apply Java Swing's JButton components" can help you solve your doubts.
Common construction methods for the JButton class:
JButton () is used to create buttons without labels and icons.
JButton (Icon icon) is used to create buttons with icons.
JButton (String text) is used to create buttons with labels.
JButton (String text, Icon icon) is used to create buttons with labels and icons.
Common methods of the JButton class:
Specific use process:
The first step is to create a visible window.
JFrame jf = new JFrame ("Demo02"); / / initialize a window instance object named "Demo02" jf.setDefaultCloseOperation (WindowConstants.EXIT_ON_CLOSE); / / set window closing and exit program jf.setBounds (100100800600); / / set window coordinates and width and height jf.setVisible (true); / / set window visibility
The second step is to create a panel and button, add the button to the panel, and then add the panel to the window.
JPanel jp = new JPanel (); / / create a panel JButton btn1 = new JButton ("normal button"); / / create a button jp.add (btn1); / / add a button to the panel jf.add (jp); / / add the panel to the window
Running result:
Third, then we can try to create different buttons with different constructors of JButton and enrich buttons with methods of the JButton class.
JButton btn1 = new JButton ("normal button"); JButton btn2 = new JButton ("button with background color"); JButton btn3 = new JButton ("button not available"); JButton btn4 = new JButton ("bottom aligned button"); jp.add (btn1); btn2.setBackground (Color.CYAN); / / set background color jp.add (btn2); btn3.setEnabled (false) / / set the mouse not available jp.add (btn3); Dimension preferredSize = new Dimension (200jing50); btn4.setPreferredSize (preferredSize); / / set the size of the button btn4.setVerticalAlignment (SwingConstants.BOTTOM); / / set the button alignment jp.add (btn4)
Complete code:
Import javax.swing.*;import java.awt.*;public class Demo02 {public static void main (String [] args) {JFrame jf = new JFrame ("Demo02"); jf.setDefaultCloseOperation (WindowConstants.EXIT_ON_CLOSE); jf.setBounds (100100800600); JPanel jp = new JPanel (); JButton btn1 = new JButton ("normal button"); JButton btn2 = new JButton ("button with background color") JButton btn3 = new JButton ("unavailable button"); JButton btn4 = new JButton ("bottom aligned button"); jp.add (btn1); btn2.setBackground (Color.CYAN); jp.add (btn2); btn3.setEnabled (false); jp.add (btn3); Dimension preferredSize = new Dimension (200Co50); btn4.setPreferredSize (preferredSize) Btn4.setVerticalAlignment (SwingConstants.BOTTOM); jp.add (btn4); jf.add (jp); jf.setVisible (true);}}
[note]: it is worth noting that the setVisible method must be placed at the back, otherwise after you set up the above content, the result will not be displayed in the window.
After reading this, the article "how to apply the JButton components of Java Swing" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about the article, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.