gpt4 book ai didi

java - JDialog返回值?

转载 作者:行者123 更新时间:2023-12-02 06:13:57 24 4
gpt4 key购买 nike

很抱歉,这个问题已经被问过很多次了,但我有一个自定义 JDialog。它具有自定义背景,并显示在调用它的框架上,接收消息,然后在为调用它的框架选择一个选项后应返回 YES 或 NO(0 或 1)。

public class JYOptionPane {

@SuppressWarnings("deprecation")
public static void showJY_YES_NO_Dialog(final JFrame frame, String Question) {
darken(frame);
frame.disable();
final JDialog OptionPane = new JDialog();
OptionPane.setAlwaysOnTop(true);
OptionPane.setBounds(
(int) (frame.getX() + (frame.getSize().getWidth() / 2))
- (350 / 2), (int) (frame.getY() + (frame.getSize()
.getHeight() / 2)) - (200 / 2), 350, 200);
OptionPane.setResizable(false);
OptionPane.setUndecorated(true);
ImageIcon icon = new ImageIcon(
LoginFrame.class.getResource("/Images/bgprompt.png"));
Image backImage = icon.getImage();
ImagePanel contentPane = new ImagePanel();
contentPane.setBackgroundImage(backImage);
OptionPane.setContentPane(contentPane);
OptionPane.setBackground(new Color(0, 0, 0, 0));
JLabel lblQuestion = new JLabel(Question);
lblQuestion.setBounds(0, 40, 350, 30);
lblQuestion.setFont(new Font("Calibria", Font.PLAIN, 20));
lblQuestion.setForeground(Color.white);
lblQuestion.setHorizontalAlignment(SwingConstants.CENTER);
lblQuestion.setVerticalAlignment(SwingConstants.CENTER);
lblQuestion.setAlignmentY(JComponent.CENTER_ALIGNMENT);
OptionPane.add(lblQuestion);

JYButton btnYes = new JYButton(
new ImageIcon(LoginFrame.class.getResource("/buttons/default.png")),
new ImageIcon(LoginFrame.class.getResource("/buttons/defaultprs.png")),
new ImageIcon(LoginFrame.class.getResource("/buttons/defaulthv.png")));
btnYes.setText("Yes");
btnYes.setFont(new Font("Arial", Font.BOLD, 17));
btnYes.setForeground(Color.white);
btnYes.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
LoginFrame.setOPintNo(0);
Class<? extends JFrame> f = frame.getClass();
try {
f.newInstance().setVisible(true);
frame.dispose();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
OptionPane.dispose();
}
});
btnYes.setBounds(40, 200 / 2 - 31 / 2 + 50, 121, 31);
OptionPane.add(btnYes);

OptionPane.getRootPane().setDefaultButton(btnYes);

JYButton btnNo = new JYButton(new ImageIcon(
LoginFrame.class.getResource("/buttons/default.png")),
new ImageIcon(LoginFrame.class.getResource("/buttons/defaultprs.png")),
new ImageIcon(LoginFrame.class.getResource("/buttons/defaulthv.png")));
btnNo.setText("No");
btnNo.setFont(new Font("Arial", Font.BOLD, 17));
btnNo.setForeground(Color.white);
btnNo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
LoginFrame.setOPintNo(1);
Class<? extends JFrame> f = frame.getClass();
try {
f.newInstance().setVisible(true);
frame.dispose();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
OptionPane.dispose();
}
});
btnNo.setBounds(189, 200 / 2 - 31 / 2 + 50, 121, 31);
OptionPane.add(btnNo);

OptionPane.setVisible(true);
}
}

我试图让它返回一个 int,但它似乎无法将 int 分配给变量。因此,我在 LoginFrame 中创建了一个 int 变量(它永远不会被释放,只是不可见)并将其设置在那里并为其创建一个 Get() 函数。但是,在 JDialog 中进行选择后,如何获取调用 Get() 函数的框架?必须从我作为最终 JFrame 框架传入的框架中调用它。谢谢!

最佳答案

您可能想查看How to Use Modality in Dialogs文档。首先,您不需要传入 JFrame对于您的类(class),您可以简单地摆脱它。看一下这个例子:

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.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Demo {

private void createAndShowGUI() {

JButton button = new JButton("Show dialog");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
MyOptionPane optionPane = new MyOptionPane();
int option = optionPane.showYesNoMessage("Close frame", "Do you really want to close the frame?");
if(option == MyOptionPane.YES) {
JButton button = (JButton)e.getSource();
SwingUtilities.getWindowAncestor(button).dispose();
}
}
});

JPanel content = new JPanel();
content.add(new JLabel("Test:"));
content.add(button);

JFrame frame = new JFrame("Demo");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(content);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public class MyOptionPane {

public static final int YES = 0;
public static final int NO = -1;

private int choice = NO;

public int showYesNoMessage(String title, String message) {

JLabel label = new JLabel(message);

JButton yesButton = new JButton("Yes");
yesButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
choice = YES;
JButton button = (JButton)e.getSource();
SwingUtilities.getWindowAncestor(button).dispose();
}
});

JButton noButton = new JButton("No");
noButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
choice = NO;
JButton button = (JButton)e.getSource();
SwingUtilities.getWindowAncestor(button).dispose();
}
});

JPanel buttons = new JPanel();
buttons.add(yesButton);
buttons.add(noButton);

JPanel content = new JPanel(new BorderLayout(8, 8));
content.add(label, BorderLayout.CENTER);
content.add(buttons, BorderLayout.SOUTH);

JDialog dialog = new JDialog();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setModal(true);
dialog.setTitle(title);
dialog.getContentPane().add(content);
dialog.pack();
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);

return choice;
}
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Demo().createAndShowGUI();
}
});
}

}
<小时/>

离题

关于setBounds()的使用正如@AndrewThompson 总是说的:

Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or combinations of them, along with layout padding & borders for white space.

所以你应该完全避免使用 setBounds()set(Preferred | Minimum | Maximum)Size()当您使用 Swing 组件时。

关于java - JDialog返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21649835/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com