View Full Version : سوال: visible شدن فریم در وسط اسکرین
little.boy
یک شنبه 21 تیر 1388, 23:19 عصر
راهی هست که یک فریم وقتی نمایش داده می شه در وسط اسکرین مونیتور ظاهر بشه؟
amirfarshad
دوشنبه 22 تیر 1388, 11:12 صبح
جی بیلدر کد زیر رو برای انجام اینکار تولید میکنه
Frame1 frame = new Frame1();
//Validate frames that have preset sizes
//Pack frames that have useful preferred size info, e.g. from their layout
if (packFrame) {
frame.pack();
}
else {
frame.validate();
}
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
البته این قسمت
if (packFrame)همیشه درست کار نمیکنه و من دستی سایز رو ست میکنم. دلیلش رو درست نمیدونم چرا، شاید به خاطر layout باشه که انتخاب میکنم
java.source.ir
دوشنبه 22 تیر 1388, 17:44 عصر
سلام
از متد زیر که یکی از متدهای مربوط به JFrame است استفاده کن:
setLocationRelativeTo(null)
این هم یک نمونه ساده که در NetBeans نوشته شده است:
import javax.swing.*;
import java.awt.*;
public class Main extends JFrame {
private JTextField textField;
private Container container;
public Main() {
container = getContentPane();
textField = new JTextField(10);
textField.setFont(new Font("Arial",Font.BOLD,20));
container.add(textField);
setVisible(true);
setLocationRelativeTo(null);
pack();
}
public static void main(String[] args) {
Main app = new Main();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
}
}
در ضمن اگر علاقه مند به کار GUI هستی در سایت www.iranjavaref.ir مقالاتی درباره آن موجود می باشد.
vBulletin® v4.2.5, Copyright ©2000-1404, Jelsoft Enterprises Ltd.