This video is interpretive , So I hope you have read the content of this knowledge point , And after writing the corresponding code , Watch with questions , Only in this way can we gain more . It is not recommended to watch the video at the beginning
![]() 6 branch 39 second This video uses html5 Play mode , If it cannot be played normally , Please upgrade your browser to the latest version , Recommend Firefox ,chrome,360 browser . If thunderbolt is installed , Play the video and show the direct download status , Please adjust Thunderbolt system settings - Basic settings - Start - Monitor all browsers ( Remove this option ). chrome of Video download Plug-in will affect playback , as IDM etc. , Please close or switch other browsers Step 1 : A simple example Step 2 : practice - Start the window at the last closed position Step 3 : answer - Start the window at the last closed position
JFrame yes GUI Container in
JButton Is the most common component - Button be careful :f.setVisible(true); All components will be rendered , So be sure to Put it at the back
package gui; import javax.swing.JButton; import javax.swing.JFrame; public class TestGUI { public static void main(String[] args) { // Main form JFrame f = new JFrame("LoL"); // Set the size of the main form f.setSize(400, 300); // Set the location of the main form f.setLocation(200, 200); // The components in the main form are set to absolute positioning f.setLayout(null); // Button assembly JButton b = new JButton(" One button second, the opponent's base hangs "); // Set the size and location of components at the same time b.setBounds(50, 50, 280, 30); // Add the button to the main form f.add(b); // When closing the form , Exit the program f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Make the form visible f.setVisible(true); } }
For example, use this window this time , Causes the window to be moved to the lower right corner . Close this window , The next time you restart , It will automatically appear in the lower right corner .
Thought tips : Start a thread , Everyone 100 Read the current location information in milliseconds , Save in file , For example location.txt File . When starting , Read location information from this file , If it is empty , Use the default location , If it's not empty , Just set the location information on the window . Method of reading location information : f.getX() Read abscissa information ,f.getY() Read ordinate information . notes : This exercise requires multithreading to complete . There is another way to complete , Is to use a listener , Because just started learning GUI, I haven't mastered the use of listeners yet , So temporarily use multithreading to complete this function .
Before looking at the answers , Try to finish it yourself first , See the answer when you encounter a problem , The harvest will be more
Before looking at the answers , Try to finish it yourself first , See the answer when you encounter a problem , The harvest will be more
Before looking at the answers , Try to finish it yourself first , See the answer when you encounter a problem , The harvest will be more
Viewing this answer will cost 4 Points , You currently have a total of Point integral . It doesn't cost extra points to see the same answer . Points increase method Or One time purchase JAVA Intermediate total 0 One answer ( Total required 0 Integral )
Viewing this answer will cost 4 Points , You currently have a total of Point integral . It doesn't cost extra points to see the same answer . Points increase method Or One time purchase JAVA Intermediate total 0 One answer ( Total required 0 Integral )
Account not activated
Account not activated , Limited functionality . Please click activate
This video is interpretive , So I hope you have read the content of this answer , Watch with questions , Only in this way can we gain more . It is not recommended to watch the video at the beginning
![]() 5 branch 10 second This video uses html5 Play mode , If it cannot be played normally , Please upgrade your browser to the latest version , Recommend Firefox ,chrome,360 browser . If thunderbolt is installed , Play the video and show the direct download status , Please adjust Thunderbolt system settings - Basic settings - Start - Monitor all browsers ( Remove this option ). chrome of Video download Plug-in will affect playback , as IDM etc. , Please close or switch other browsers
SavingPostionThread Used every 100 Millisecond records the current location information to location.txt In , When recording data
Data output stream You can easily save multiple integers
Then in TestGUI Design a static inner class Point Used to save x and y. Then in TestGUI Design a method in getPointFromLocationFile, Through Data input stream Read coordinates x and y, Put it in a Point Object , And return to . be careful : The first time I read , There are no documents , So it will return null, Special treatment is required .
package gui; import java.io.DataOutputStream; import java.io.File; import java.io.FileOutputStream; import javax.swing.JFrame; class SavingPostionThread extends Thread { private JFrame f; File file = new File("e:/project/j2se/location.txt"); SavingPostionThread(JFrame f) { this.f = f; } public void run() { while (true) { int x = f.getX(); int y = f.getY(); try (FileOutputStream fos = new FileOutputStream(file); DataOutputStream dos = new DataOutputStream(fos);) { dos.writeInt(x); dos.writeInt(y); } catch (Exception e) { e.printStackTrace(); } try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
package gui; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import javax.swing.JButton; import javax.swing.JFrame; public class TestGUI { public static void main(String[] args) { // Main form JFrame f = new JFrame("LoL"); // Set the size of the main form f.setSize(400, 300); // Set the location of the main form Point p =getPointFromLocationFile(); if(p!=null) f.setLocation(p.x,p.y); else f.setLocation(200, 200); // The components in the main form are set to absolute positioning f.setLayout(null); // Button assembly JButton b = new JButton(" One button second, the opponent's base hangs "); // Set the size and location of components at the same time b.setBounds(50, 50, 280, 30); // Add the button to the main form f.add(b); // When closing the form , Exit the program f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Make the form visible f.setVisible(true); new SavingPostionThread(f).start(); } static class Point { int x; int y; } public static Point getPointFromLocationFile() { File file = new File("e:/project/j2se/location.txt"); Point p = null; try (FileInputStream fis = new FileInputStream(file); DataInputStream dis = new DataInputStream(fis);) { int x = dis.readInt(); int y = dis.readInt(); p = new Point(); p.x = x; p.y = y; } catch (FileNotFoundException e) { // First run , No location file generated , So there will be FileNotFoundException } catch (IOException e1) { e1.printStackTrace(); } return p; } }
The official account of programming , Follow and get the latest tutorials and promotions in real time , thank you .
![]()
Q & A area
2021-09-17
practice - Start the window at the last closed position
The answer has been submitted successfully , Auditing . Please
My answer Check the answer record at , thank you
2021-08-25
Start the window at the last closed position
The answer has been submitted successfully , Auditing . Please
My answer Check the answer record at , thank you
2021-07-08
This is my answer , I passed the test .
2021-04-29
Punch in the first one
2021-04-18
Why add try() Not in Just in the loop
Too many questions , Page rendering is too slow , To speed up rendering , Only a few questions are displayed on this page at most . also 36 Previous questions , please Click to view
Please... Before asking questions land
The question has been submitted successfully , Auditing . Please
My question Check the question record at , thank you
|