Java JFrame graphical interface - a simple window

#Start

Been applying for a blog for a while but haven't had time to blog (it's actually a slacker eating my brain)

Recently, I am learning JFrame to make windows. I have encountered a lot of problems. In order to solve the problem, I also murdered a lot of brain cells. In order to make more friends and future me immortal, I wrote down the problems encountered during learning. It's my own memo

Mengxin boss, don't spray, I referred to many blogs on the Internet during my learning process. Thank you for sharing your knowledge. I hope my code and experience can help more people :)

----------------------------------------------------------------------------------------------------------------------------------------

#code

Using Java as a window I found that a remarkable feature is that there is not only one way to implement window functions, or there are many ways to do a JFrame Hello, world first.

 1 package window;
 2 
 3 import java.awt.Container;
 4 
 5 import javax.swing.JFrame;
 6 import javax.swing.JLabel;
 7 
 8 public class JFrameWindow extends JFrame{     //需要继承JFrame
 9 
10     public JFrameWindow(String title)
11     {
12         JFrame jf = new JFrame(title);    
13         Container conn = jf.getContentPane();    //得到窗口的容器
14         JLabel L1 = new JLabel("Hello,world!");     // Create a label and set the initial content 
15          
16          conn.add(L1);
 17          
18          jf.setBounds(200,200,300,200); // Set the window's properties window position and window size 
19          jf.setVisible( true ); // Set the window to be visible 
20          jf.setDefaultCloseOperation(DISPOSE_ON_CLOSE); // Set the closing method. If it is not set, it seems that the program will not exit after closing the window. 
21      }
 22      
23      public  static  void main(String[] args) {
 24          new JFrameWindow("window");         // create window 
25      }
 26 
27 }

 

In this way, you can get a window as shown in the figure:

  

 

#Skill

  If you use ecplise If you don't know the content of the package you use, you can press CTRL+SHIFT+O to import the required package yourself

  Another shortcut is CTRL + / to comment all lines selected by the mouse

 

#Problems encountered:

  1. I kept making mistakes when naming the main class and I kept looking for mistakes. Later, I found that the name of the main class was named JFrame. The obvious conflict was never found (maybe I am the only one who makes such mistakes :-) )

  2. The twentieth line of code is not written, which leads to a problem that the program does not seem to exit completely after clicking to close the window

    

    It's the red stop button that's always on

#environment

ecplise EE JDK1.8 (all x64)

    

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324654825&siteId=291194637