Java JDialog弹出窗口太小
每次我运行这段代码时,输出都是一个对话框,显示在屏幕的左上角,标题不显示 是否有任何方法可以更改此设置,使对话框显示在中间并具有可接受的大小 代码:Java JDialog弹出窗口太小,java,swing,timer,jdialog,preferredsize,Java,Swing,Timer,Jdialog,Preferredsize,每次我运行这段代码时,输出都是一个对话框,显示在屏幕的左上角,标题不显示 是否有任何方法可以更改此设置,使对话框显示在中间并具有可接受的大小 代码: 绝对是。默认情况下,JDialog(或JFrame)将这样显示。您需要对其设置边界: dialog.setBounds(xPosition, yPosition, width, height); 然而,如果你只是为它设置一些神奇的数字,这将无法很好地扩展到其他系统。取而代之的是,获取屏幕尺寸,并将其抵消: //static and final b
绝对是。默认情况下,
JDialog
(或JFrame
)将这样显示。您需要对其设置边界:
dialog.setBounds(xPosition, yPosition, width, height);
然而,如果你只是为它设置一些神奇的数字,这将无法很好地扩展到其他系统。取而代之的是,获取屏幕尺寸,并将其抵消:
//static and final because no reason not to be. Insert this at the class definition
static final Dimension SCREEN_DIMENSION = Toolkit.getDefaultToolkit().getScreenSize();
...
//I'd also make this static and final and insert them at the class definition
int dialogWidth = SCREEN_DIMENSION.width / 4; //example; a quarter of the screen size
int dialogHeight = SCREEN_DIMENSION.height / 4; //example
...
int dialogX = SCREEN_DIMENSION.width / 2 - dialogWidth / 2; //position right in the middle of the screen
int dialogY = SCREEN_DIMESNION.height / 2 - dialogHeight / 2;
dialog.setBounds(dialogX, dialogY, dialogWidth, dialogHeight);
或者,如果将组件添加到JDialog
,则调用
dialog.pack();
对话框现在将是容纳组件的最小尺寸。如果您使用的是应紧密包装的部件,请使用此方法;这样你就不必费劲地手工构建正确的宽度和高度了。绝对如此。默认情况下,
JDialog
(或JFrame
)将这样显示。您需要对其设置边界:
dialog.setBounds(xPosition, yPosition, width, height);
然而,如果你只是为它设置一些神奇的数字,这将无法很好地扩展到其他系统。取而代之的是,获取屏幕尺寸,并将其抵消:
//static and final because no reason not to be. Insert this at the class definition
static final Dimension SCREEN_DIMENSION = Toolkit.getDefaultToolkit().getScreenSize();
...
//I'd also make this static and final and insert them at the class definition
int dialogWidth = SCREEN_DIMENSION.width / 4; //example; a quarter of the screen size
int dialogHeight = SCREEN_DIMENSION.height / 4; //example
...
int dialogX = SCREEN_DIMENSION.width / 2 - dialogWidth / 2; //position right in the middle of the screen
int dialogY = SCREEN_DIMESNION.height / 2 - dialogHeight / 2;
dialog.setBounds(dialogX, dialogY, dialogWidth, dialogHeight);
或者,如果将组件添加到JDialog
,则调用
dialog.pack();
对话框现在将是容纳组件的最小尺寸。如果您使用的是应紧密包装的部件,请使用此方法;这样,您就不必费劲地手工构建正确的宽度和高度。添加这一行
dialog.setBounds(0, 0, 1000, 500);
添加这一行
dialog.setBounds(0, 0, 1000, 500);
“是否有任何方法可以更改此设置,使对话框显示在中间并具有可接受的大小?”
如果您只是向它添加组件,打包它,并将它的相对位置设置为null,那么它应该可以正常工作
最好是.pack()
,而不是设置大小。要使pack正常工作,您需要实际添加组件。.pack()
将完全按照其名称进行操作-按照所有添加组件的首选尺寸包装框架
另外,使用setLocationRelativeTo()
可以相对于组件设置对话框位置。如果使用null
,它将始终位于屏幕中央。但是,如果设置相对于其父帧的位置,它将显示在帧的中心
我完全不知道你想用定时器实现什么,所以我不想发表评论
参见示例
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.Timer;
public class Test {
public static void main(String[] args) {
JFrame f = new JFrame();
final MyDialog dialog = new MyDialog(f, "Title", true);
Timer timer = new Timer(10000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
dialog.setVisible(false);
dialog.dispose();
}
});
timer.setRepeats(false);
timer.start();
dialog.setVisible(true);
System.out.println("Dialog closed");
}
private static class MyDialog extends JDialog {
public MyDialog(JFrame frame, String title, boolean modal) {
super(frame, title, modal);
setLayout(new BorderLayout());
add(new JButton("NORTH"), BorderLayout.NORTH);
add(new JButton("SOUTH"), BorderLayout.SOUTH);
add(new JButton("EAST"), BorderLayout.EAST);
add(new JButton("WEST"), BorderLayout.WEST);
add(new JButton("CENTER"), BorderLayout.CENTER);
pack();
setLocationRelativeTo(null);
}
}
}
作为补充说明,您应该像这样从事件调度线程(EDT)运行Swing应用程序
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable(){
public void run() {
// the code from your main method here
}
});
}
“是否有任何方法可以更改此设置,使对话框显示在中间并具有可接受的大小?”
如果您只是向它添加组件,打包它,并将它的相对位置设置为null,那么它应该可以正常工作
最好是.pack()
,而不是设置大小。要使pack正常工作,您需要实际添加组件。.pack()
将完全按照其名称进行操作-按照所有添加组件的首选尺寸包装框架
另外,使用setLocationRelativeTo()
可以相对于组件设置对话框位置。如果使用null
,它将始终位于屏幕中央。但是,如果设置相对于其父帧的位置,它将显示在帧的中心
我完全不知道你想用定时器实现什么,所以我不想发表评论
参见示例
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.Timer;
public class Test {
public static void main(String[] args) {
JFrame f = new JFrame();
final MyDialog dialog = new MyDialog(f, "Title", true);
Timer timer = new Timer(10000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
dialog.setVisible(false);
dialog.dispose();
}
});
timer.setRepeats(false);
timer.start();
dialog.setVisible(true);
System.out.println("Dialog closed");
}
private static class MyDialog extends JDialog {
public MyDialog(JFrame frame, String title, boolean modal) {
super(frame, title, modal);
setLayout(new BorderLayout());
add(new JButton("NORTH"), BorderLayout.NORTH);
add(new JButton("SOUTH"), BorderLayout.SOUTH);
add(new JButton("EAST"), BorderLayout.EAST);
add(new JButton("WEST"), BorderLayout.WEST);
add(new JButton("CENTER"), BorderLayout.CENTER);
pack();
setLocationRelativeTo(null);
}
}
}
作为补充说明,您应该像这样从事件调度线程(EDT)运行Swing应用程序
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable(){
public void run() {
// the code from your main method here
}
});
}
在
timer.start()之间添加这些代码行代码>和对话框.setVisible(true)代码>语句-
timer.start();
dialog.setPreferredSize(new Dimension(50, 150));//set your desired size
dialog.pack();
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenSize = toolkit.getScreenSize();
int iWidth = (screenSize.width - dialog.getWidth()) / 2;
int iHeight = (screenSize.height - dialog.getHeight()) / 2;
dialog.setLocation(iWidth, iHeight);
dialog.setVisible(true);
此代码将对话框设置为屏幕的中心。在timer.start()之间添加这些代码行代码>和对话框.setVisible(true)代码>语句-
timer.start();
dialog.setPreferredSize(new Dimension(50, 150));//set your desired size
dialog.pack();
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenSize = toolkit.getScreenSize();
int iWidth = (screenSize.width - dialog.getWidth()) / 2;
int iHeight = (screenSize.height - dialog.getHeight()) / 2;
dialog.setLocation(iWidth, iHeight);
dialog.setVisible(true);
此代码会将对话框设置为屏幕的中心。ToolKit
不适合确定屏幕大小,因为它没有考虑任务栏之类的内容。通常最好使用setLocationRelativeTo(null)
要使窗口居中…通常最好允许内容自行确定首选大小并使用pack
而不是直接将其设置到对话框中…ToolKit
对于确定屏幕大小不是很好,因为它没有考虑任务栏之类的内容。通常最好使用setLocationRelativeTo(null)
将窗口居中…通常也最好允许内容自行确定首选大小并使用pack
而不是直接将其设置到对话框中…或。