inotgo.com


Tool version compatibility problem
GUI- Graphic User Interface Graphical user interface
Correct

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
 A simple example
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); } }
Step 2 :

practice - Start the window at the last closed position

edit top fracture   Wrong posture , get half the results with double the effort ! Click to see the correct posture for the exercise
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 .
Step 3 :

answer - Start the window at the last closed position

edit top fracture
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
Mono




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 .
 Loading
public class TestGUI {
	
	static int x = 5; static int y = 5; static int j = 300; static int k = 150;
	static JFrame f = new JFrame("LOL");
	//  Prepare an empty file location.txt
	static File f1 = new File("E:/project/j2se/src/location.txt");
	static List<String> ss =new ArrayList<>();
	
	public static void main(String[] args) {
		
		TestGUI sx = new TestGUI();
		// Create a file based output stream    Automatically create directories and files 
	    if (f1.exists() == false) {
	    	f1.getParentFile().mkdirs();
	    	try {
				f1.createNewFile();
			} catch (IOException e) {
				// TODO  Automatically generated  catch  block 
				e.printStackTrace();
			}
	    }
		//  Create... In a traditional way 100 Threads 
		int n = 100;
        Thread[] Threads = new Thread[n];
           
        for (int i = 0; i < n; i++) {
            Thread t = new Thread(){
                public void run(){
            		try {
            			Thread.sleep(500);
            			//  Read template 
            			String s1 = null;
            			String x1 = String.valueOf(x); String y1 = String.valueOf(y);
            			s1 = x1 + "," + y1;
            			//  Update content 
        				ss.add(s1);
        				//  Write the updated content to the new file 
        				sx.write(ss,f1);
            			if(sx.readTem(f1).length == 0) {;           				
            			} else if(sx.readTem(f1).length > 0) {
            			;}
            			sx.Frame(f);
            			sx.StartFrame(f);	//  Location updated 
            			Thread.sleep(500);
            			sx.CloseFrame(f);
            		} catch (InterruptedException e) {
            			// TODO  Automatically generated  catch  block 
            			e.printStackTrace();
            		}
                }
            };
            t.start();
            Threads[i] = t;
            try {
				t.join();
			} catch (InterruptedException e) {
				// TODO  Automatically generated  catch  block 
				e.printStackTrace();
			}
        }
        
	}
	
	public void Frame(JFrame f) {
		f.setSize(j, k);
		f.setLocation(x, y);
		
		f.setLayout(null);
		
		JButton b = new JButton(" One button second opponent's base ");
		b.setBounds(50, 50, 200, 30);
		
		f.add(b);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	public void CloseFrame(JFrame f) {
		f.dispose();
	}
	
	public void StartFrame(JFrame f) {
		x = f.getX(); y = f.getY();
		f.setLocation(x, y);
		f.setVisible(true);
		x += j; y += k;	
	}
		
	private void write(List<String> ss,File f) {
        try (
                FileWriter fw = new FileWriter(f);
                PrintWriter pw = new PrintWriter(fw);
        ) {
            for (int i = 0; i < ss.size(); i++) {
                pw.println(ss.get(i));
            }
            pw.close();
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

	public String[] readTem(File f){
        int l = 0;int i = 0;
        try (
                FileReader fr = new FileReader(f);
                BufferedReader br = new BufferedReader(fr);
        ) {
            while (true) {
                String line = br.readLine();
                if (null == line)
                    break;
                l++;
            }
            br.close();
            fr.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String[] s0= new String[l];
        try (
                FileReader fr = new FileReader(f);
                BufferedReader br = new BufferedReader(fr);
        ) {
            while (true) {
                String line = br.readLine();
                if (null == line)
                    break;
                s0[i] = line;
                i++;
            }
            br.close();
            fr.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s0;
    }	
}
 
               





The answer has been submitted successfully , Auditing . Please My answer Check the answer record at , thank you
answer Or code please Fill in at least one , If you have a problem , Please ask again , Otherwise, the webmaster may not see





2021-08-25 Start the window at the last closed position
logiczqr




practice
package gui;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;

import javax.swing.JButton;
import javax.swing.JFrame;

public class TestGUI {
	public static final File FILE = new File("C:\\Users\\Administrator\\Desktop\\location.txt");

	public static void main(String[] args) {
		//  Main form 
		JFrame jf = new JFrame("LOL");
		//  Set the size of the main form 
		jf.setSize(400, 300);
		//  Set the location of the main form 
		int[] xy = method2();
		int x, y;
		if (xy != null) {
			x = xy[0];
			y = xy[1];
			jf.setLocation(x, y);
		} else {
			jf.setLocation(200, 200);
		}
		//  The components in the main form are set to absolute positioning 
		jf.setLayout(null);
		//  Button assembly 
		JButton jb = new JButton(" One button second, the opponent's base hangs ");
		//  Set the size and location of components at the same time 
		jb.setBounds(50, 50, 280, 30);
		//  Add the button to the main form 
		jf.add(jb);
		//  When closing the form , Exit the program 
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);			
		//  Make the form visible 
		jf.setVisible(true);
		method(jf);
	}

	/**
	 *  Write out the window position information to the file 
	 * @param jf
	 */
	public static void method(JFrame jf) {
		Thread thread = new Thread() {
			public void run() {
				while (true) {
					int x = jf.getX();
					int y = jf.getY();
					try (PrintWriter pw = new PrintWriter(FILE);) {
						pw.print(x + "@" + y);
						pw.flush();
						Thread.sleep(100);
					} catch (IOException | InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		};
		thread.start();
	}

	/**
	 *  Read the location information in the file 
	 * @return
	 */
	public static int[] method2() {
		int[] is = null;
		try (FileReader fr = new FileReader(FILE); BufferedReader br = new BufferedReader(fr);) {
			String str = br.readLine();
			String[] ss = str.split("@");
			int[] is2 = new int[2];
			is2[0] = Integer.parseInt(ss[0]);
			is2[1] = Integer.parseInt(ss[1]);
			is = is2;
		} catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return is;
	}
}
 
               





The answer has been submitted successfully , Auditing . Please My answer Check the answer record at , thank you
answer Or code please Fill in at least one , If you have a problem , Please ask again , Otherwise, the webmaster may not see





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
about JAVA Intermediate - Graphical interface -Hello Swing Your questions

Try to provide Screenshot code and Abnormal information , Help to analyze and solve problems . You can also enter this station QQ Group communication : 496725845
Ask questions and try to provide complete code , Environment description , The more conducive to the recurrence of the problem , The faster your question can be answered .
Have questions about the code in the tutorial , Please provide which step , Which line is in doubt , This makes it easy to quickly locate the problem , Improve the speed at which questions are answered
In the thousands of questions that already exist , A considerable proportion , Because of the use of and webmaster Different versions of the development environment Resulting in , For example jdk, eclpise, idea, mysql,tomcat Wait, the version of the software is inconsistent .
Please use the same version as the webmaster , You can save yourself a lot of learning time . The webmaster sorted out the software versions used in teaching , It's all here , Convenient for everyone to download : /k/helloworld/helloworld-version/1718.html

Upload screenshot