Trong nhiều lập viết code Java có sử dụng JFame để tạo cửa sổ và mong muốn nó hiển thị ngay chính giữa vị trí trung tâm màn hình, tôi xử lý bằng cách tính toán tỉ lệ của màn hình và JFame để có được thông số thiết lập vị trí.
Và nó thực sự rất phiền vì phải viết rất dòng mã để thực hiện công việc này. Sau một thời gian dạo quanh các diễn đàn thì phát hiện được một cách xử lý vô cùng dễ dàng và hiệu quả.
Bên dưới là hình một ứng dụng khi không được thiết lập ví trí trên màn hình thì nó sẽ hiển thị ở góc trái trên cùng của màn hình.
Như thường lệ tôi cũng sẽ dùng một ví dụ nhỏ để làm thực nghiệm. Để tránh viết nhiều mỏi tay, đọc nhiều mỏi mắt ^^! tôi cung cấp toàn bộ srouce để giải quyết vấn đề trên.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/* FrameDemo.java requires no other files. */
public class FrameDemo {
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("FrameDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel emptyLabel = new JLabel("");
emptyLabel.setPreferredSize(new Dimension(475, 400));
frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
//Display the window.
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
import java.awt.event.*;
import javax.swing.*;
/* FrameDemo.java requires no other files. */
public class FrameDemo {
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("FrameDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel emptyLabel = new JLabel("");
emptyLabel.setPreferredSize(new Dimension(475, 400));
frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
//Display the window.
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Sau khi run source code trên thì kết quả ta thu được như sau:
Nội dung của đoạn code nhìn sơ qua cũng hiểu nó đơn giản chỉ là đoạn code tạo một JFame và hiển thị nó.
Nhưng quan trong ở đây là hàm bên dưới mới có thể hiển thị JFrame hiển thị tại vị trí chính giữa màn hình(Screen):
frame.setLocationRelativeTo(null);
Bạn cũng nên lưu ý thứ tự khi sử dụng hàm này như trong ví dụ thì thứ tự nó là:
1. frame.pack();
2. frame.setLocationRelativeTo(null);
3. frame.setVisible(true);
2. frame.setLocationRelativeTo(null);
3. frame.setVisible(true);
Tại sao lại như vậy? Vì nó là tiến trình tuần tự của JAVA, giả sử khi bạn chưa chạy dòng 1 thì JFrame sẽ chưa được xây dựng mà chưa xây dựng thì sẽ chưa có thông số của JFrame để đoạn code 2 có thể tính toán ví trị chính giữa của màn hình tương ứng với JFrame.
Còn trường 2 chuyển xuống 3 kết quả cuối cùng cũng tương tự sau nhưng khi chạy ứng dụng bạn sẽ thấy ứng dụng sẽ hiển thị tại góc nào đó trước rùi mới bị giựt về ngày chính giữa màn hình. Hiểu đơn giản là bạn đã hiển thị JFrame lên rùi mới kéo nó về vị trí chính giữa màn hình.
No comments:
Post a Comment