JDialog 位于屏幕中心,而不是 JApplet 所需的中心
JDialog in center of screen instead of desired center of JApplet
在我的 Java 小程序中,我有带有几个单选按钮的 JDialog。
问题在于此小程序的尺寸为 700x300,并且 JDialog 出现在屏幕中央。我希望这个 JDialog 出现在 JApplet 布局的中心。我的构造函数调用如下所示:
final JDialog dialog = new JDialog(
SwingUtilities.windowForComponent(GUIComponentContainer.getInstance().getDocumentTable()),
I18nCommonsImpl.constants.selectCertificate(), ModalityType.APPLICATION_MODAL);
此方法:
GUIComponentContainer.getInstance().getDocumentTable()
returns JTable 组件,它是我的 JApplet 的子组件。
我也用了JDialog"setLocationRelativeTo"方法:
dialog.setLocationRelativeTo(GUIComponentContainer.getInstance().getApplet());
None 其中似乎有效。还有其他方法可以实现我的目标吗?
我搜索了类似的问题,但没有看到任何有效的解决方案。
提前致谢。
获取小程序的 Window 对我有用,至少当小程序在 Eclipse 中启动时是这样。例如:
import java.awt.Dimension;
import java.awt.Window;
import java.awt.Dialog.ModalityType;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Box;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
@SuppressWarnings("serial")
public class AppletCentering extends JApplet {
@Override
public void init() {
final JPanel panel = new JPanel();
panel.add(new JButton(new AbstractAction("Press Me") {
@Override
public void actionPerformed(ActionEvent e) {
Window win = SwingUtilities.getWindowAncestor(panel);
System.out.println("win class: " + win.getClass().getCanonicalName());
JDialog dialog = new JDialog(win, "My Dialog", ModalityType.APPLICATION_MODAL);
dialog.add(Box.createRigidArea(new Dimension(200, 200)));
dialog.pack();
dialog.setLocationRelativeTo(win);
dialog.setVisible(true);
}
}));
add(panel);
}
}
但是话虽如此,您的问题还是引出了一个问题:您为什么还要为小程序编写代码?这些多年来一直不受青睐,最近才失去 Oracle 的支持,Oracle 决定最终放弃对 applet 浏览器插件的支持。
在我的 Java 小程序中,我有带有几个单选按钮的 JDialog。
问题在于此小程序的尺寸为 700x300,并且 JDialog 出现在屏幕中央。我希望这个 JDialog 出现在 JApplet 布局的中心。我的构造函数调用如下所示:
final JDialog dialog = new JDialog(
SwingUtilities.windowForComponent(GUIComponentContainer.getInstance().getDocumentTable()),
I18nCommonsImpl.constants.selectCertificate(), ModalityType.APPLICATION_MODAL);
此方法:
GUIComponentContainer.getInstance().getDocumentTable()
returns JTable 组件,它是我的 JApplet 的子组件。
我也用了JDialog"setLocationRelativeTo"方法:
dialog.setLocationRelativeTo(GUIComponentContainer.getInstance().getApplet());
None 其中似乎有效。还有其他方法可以实现我的目标吗? 我搜索了类似的问题,但没有看到任何有效的解决方案。
提前致谢。
获取小程序的 Window 对我有用,至少当小程序在 Eclipse 中启动时是这样。例如:
import java.awt.Dimension;
import java.awt.Window;
import java.awt.Dialog.ModalityType;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Box;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
@SuppressWarnings("serial")
public class AppletCentering extends JApplet {
@Override
public void init() {
final JPanel panel = new JPanel();
panel.add(new JButton(new AbstractAction("Press Me") {
@Override
public void actionPerformed(ActionEvent e) {
Window win = SwingUtilities.getWindowAncestor(panel);
System.out.println("win class: " + win.getClass().getCanonicalName());
JDialog dialog = new JDialog(win, "My Dialog", ModalityType.APPLICATION_MODAL);
dialog.add(Box.createRigidArea(new Dimension(200, 200)));
dialog.pack();
dialog.setLocationRelativeTo(win);
dialog.setVisible(true);
}
}));
add(panel);
}
}
但是话虽如此,您的问题还是引出了一个问题:您为什么还要为小程序编写代码?这些多年来一直不受青睐,最近才失去 Oracle 的支持,Oracle 决定最终放弃对 applet 浏览器插件的支持。