[JavaSwing] HelloWorld
In this article we will learn some basic steps to create a JFrame – That's roughly the application framework.
Example 1: Create 1 frame 250×200 title “HelloWorld” appears in the coordinates (300, 200) on-screen.
package nguyenvanquan7826.helloworld; import javax.swing.JFrame; public class HelloWorld { public static void main(String[] args) { // create frame with title "HelloWorld" JFrame frame = new JFrame("HelloWorld"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // set Size and location frame frame.setSize(250, 200); frame.setLocation(300, 200); // display frame frame.setVisible(true); } }
We have 4 constructor 1 JFrame:
JFrame() : Create a new frame invisible
JFrame(GraphicsConfiguration qc) : Create a Frame is indicated by the display device GraphicsConfiguration and white title
JFrame(String title) : Create a new frame invisibile title specified
JFrame(String title, GraphicsConfiguration qc) : Create a Frame is indicated by the title and GraphicsConfiguration screen devices.
In the example above we use the constructor th 3 to set the title for the frame.
Ham setDefaultCloseOperation(int operation): Set the default action will occur when the user closes the JFrame. If this function is not used to set the default is HIDE_ON_CLOSE (1) – When closed frame will be hidden but not completely closed.
Other options include:
DO_NOTHING_ON_CLOSE (0) – otiose
DISPOSE_ON_CLOSE (2) – Just close this frame, Other related frames will not be closed.
EXIT_ON_CLOSE (3) – Close entire frame related to it.
The function setSize set size for the frame.
Ham setLocation placement of the frame appears on the screen. If you want the frame to appear in the middle of the screen we use the function setLocationRelativeTo(null)
Ham setVisible set the display to the frame. If you do not call or set value false for this function, the frame will not be displayed.
Example 2: Create 1 JFrame class inherited by JFrame.
package nguyenvanquan7826.helloworld; import javax.swing.JFrame; import javax.swing.JLabel; public class HelloWorld1 extends JFrame { public HelloWorld1() { // create frame with title "HelloWorld" setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setTitle("HelloWorld1"); // set location setLocationRelativeTo(null); // add a JLabel add(new JLabel("HelloWorld")); // display frame pack(); setVisible(true); } public static void main(String[] args) { new HelloWorld1(); } }
In the example 2 This class inherits our JFrame class, it is 1 JFrame, so in initialization method HelloWorld1 we can call the method of JFrame.
Also in this example was used 2 method add() and pack(). The method helps us add one more object in frame (here is 1 JLabel), method helps frame pack size sufficient for the content of the frame even when we put the larger frame size.
Method setResizable(boolean resizable) used to set the frame can be resized or not.
Refer to: class JFrame
a có tài liệu lý thuyết về java swing k cho e xin. e tự học nên hơi chậm 1 tí. nếu có thì a cho e xin nha.
Alo anh ko. Toàn google 🙂
E mới học. Bài viết thực sự dễ hiểu với e. Cảm ơn a!
bt đầu của bạn mình chạy trên IDE netbean nó có lỗi bạn ơi
mình có code này bạn giúp mình với mình k nghĩ ra lỗi
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Inpout;
import java.util.Scanner;
/**
*
* @author Administrator
*/
public class Inpout {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String ten;
int your;
Scanner inp = new Scanner(System.in);
System.out.print(“Enter your name: “);
ten=inp.nextLine();
System.out.print(“enter your Age: “);
= your inp.nextLine();
System.out.println(“thee ten: %with, % of you tuoi”,ten,your);
inp.close();
}
} // TODO code application logic here
Sorry, you look to see what error message but you can edit it. Note the name of the standard package view do not agree.
tuoi = inp.nextInt();
difficult too . hic IHC
much the setSize, setLocation,… must be called method but eh ? Why called function ?
Right, method you.
I think method vs function is 1 Asian friends