简体   繁体   English

以 GUI swing JFrame 作为其父级创建 JDialog?

[英]Creating JDialog with GUI swing JFrame as its parent?

This might be a very stupid question, but I just don't know how to do it.这可能是一个非常愚蠢的问题,但我只是不知道该怎么做。

I use swing GUI to create my JFrame.我使用 swing GUI 来创建我的 JFrame。 So it is public class client extends javax.swing.JFrame .所以它是public class client extends javax.swing.JFrame

In the main method I am doing:在我正在做的主要方法中:

public void run() {
    new Client().setVisible(true);
}

When a button is clicked I want to do this:单击按钮时,我想这样做:

JDialog d = new JDialog(frame, "Example", true);

Except I have no idea what goes into 'frame'.除了我不知道“框架”是什么。 I tried to say Client, but that didn't work.我试着说客户,但这没有用。 What is suppose to go there so that my parent of JDialog is the frame where I am calling it from?那里的 go 假设是什么,以便我的 JDialog 的父级是我从中调用它的框架?

When a button is clicked I want to do this:单击按钮时,我想这样做:

You must have added an ActionListener to the button.您必须已将ActionListener添加到按钮。 The ActionEvent will contain the source object that you clicked. ActionEvent将包含您单击的源 object。

So you can write generic code in the ActionListener to get the parent Window of the button:所以可以在ActionListener中编写通用代码来获取按钮的父 Window:

JButton button = (JButton)event.getSource();
Window window = SwingUtilities.windowForComponent( button );
JDialog d = new JDialog(window, "Example", true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM