700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > java jframe 对话框_java-如何将值从JDialog框返回到父JFrame?

java jframe 对话框_java-如何将值从JDialog框返回到父JFrame?

时间:2024-03-18 13:25:51

相关推荐

java jframe 对话框_java-如何将值从JDialog框返回到父JFrame?

java-如何将值从JDialog框返回到父JFrame?

我创建了一个模态对话框,上面带有一个自定义图形和一个JButton。 当我单击JButton时,对话框应该关闭并且应该返回一个值。

我在父JFrame中创建了一个名为setModalPiece的函数,该函数接收一个值并将其设置为本地JFrame变量。

问题是该功能从JDialog框不可见(即使JDialog框具有对父JFrame的引用)。

两个问题:1)是否有更好的方法将值从JDialog框返回到其父JFrame?

2)为什么不能使用对传递给JDialog的JFrame的引用来访问我的JFrame函数setModalPiece?

6个解决方案

108 votes

我通常这样做:

Dialog dlg = new Dialog(this, ...);

Value result = dlg.showDialog();

Dialog.showDialog()函数如下所示:

ReturnValue showDialog() {

setVisible(true);

return result;

}

由于在JDialog上将可见性设置为true是模态操作,因此“确定”按钮可以将实例变量(showDialog())设置为对话框的选定结果(如果取消则为null)。 使用“确定/取消”按钮方法处理后,请执行以下操作:

setVisible(false);

dispose();

将控制权返回给showDialog()函数。

Jonathan answered -08-01T21:00:25Z

23 votes

您应该通过向自定义JFrame添加自定义方法CustomDialog来执行相反的操作。

这样,您可以从2992684038688867367328询问对话框的值,而不是通过在JFrame本身上调用某些内容来设置对话框。

如果您在此处查看有关对话框的Oracle教程,它将指出

如果要设计自定义对话框,则需要设计对话框的API,以便可以向对话框查询用户的选择。 例如,CustomDialog有一个getValidatedText方法,该方法返回用户输入的文本。

(您可以找到CustomDialog的源,以了解他们认为您将如何设计自定义对话框)

Jack answered -08-01T20:59:52Z

4 votes

我不知道我是否可以用一种很酷的方式来解释我的方法...假设我需要一个要从用户那里获取该信息的JDialog的productPrice和amount,我需要从JFrame进行调用。

在JDialog中将产品价格和金额声明为公共的非静态全局变量。

public float productPrice;

public int amount;

*这在对话框的类的全局范围内。

在JDialog构造函数中添加这些行以确保模态

super((java.awt.Frame) null, true);

setModalityType(java.awt.Dialog.ModalityType.APPLICATION_MODAL);

*这属于对话框的类构造函数

假设您调用此类操作时,您的JDialog的类名称为“ MyJDialog”

MyJDialog question = new MyJDialog();

MyJDialog.setVisible(true);

// Application thread will stop here until MyJDialog calls dispose();

// this is an effect of modality

//

// When question calls for dispose(), it will leave the screen,

// but its global values will still be accessible.

float myTotalCostVar = question.productPrice * question.ammount;

// this is acceptable.

// You can also create public getter function inside the JDialog class,

// its safer and its a good practice.

*这可以在JFrame中的任何函数中使用,并将调用JDialog获取信息。

Felype answered -08-01T21:01:12Z

0 votes

当您将任何值传递给JDialog到JDialog时,请在每次调用时在jframe中创建参数化构造函数或jdialog。例如 参数化的构造函数如:

public EditProduct(java.awt.Frame parent, boolean modal, int no) {

//int no is number of product want to edit.

//Now we can use this pid in JDialog and perform whatever you want.

}

当您要将值从JDialog传递到JFrame时,请使用set和get方法创建一个bean类,并使用vector值,并在jframe中获取这些值。更多信息

Rajshri answered -08-01T21:01:36Z

0 votes

这是我通常的做法。 我不确定,这就是我创建该帖子的原因:

从JDialog返回值; dispose(),setVisible(false)-示例

guitar_freak answered -08-01T21:02:00Z

0 votes

向您的构造函数添加接口?

public class UploadConfimation extends JDialog {

private final JPanel contentPanel = new JPanel();

public interface GetDialogResponse{

void GetResponse(boolean response);

}

/**

* Create the dialog.

*/

public UploadConfimation(String title, String message, GetDialogResponse result) {

setBounds(100, 100, 450, 300);

setTitle(title);

getContentPane().setLayout(new BorderLayout());

contentPanel.setLayout(new FlowLayout());

contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));

getContentPane().add(contentPanel, BorderLayout.CENTER);

{

JLabel lblMessage = new JLabel(message);

contentPanel.add(lblMessage);

}

{

JPanel buttonPane = new JPanel();

buttonPane.setLayout(new FlowLayout(FlowLayout.CENTER));

getContentPane().add(buttonPane, BorderLayout.SOUTH);

{

JButton okButton = new JButton("YES");

okButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

result.GetResponse(true);

dispose();

}

});

buttonPane.add(okButton);

getRootPane().setDefaultButton(okButton);

}

{

JButton cancelButton = new JButton("NO");

cancelButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

result.GetResponse(false);

dispose();

}

});

buttonPane.add(cancelButton);

}

}

}

}

Wil_Ryan answered -08-01T21:02:24Z

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。