JDialog返回值?

时间:2014-02-08 18:11:00

标签: java swing jframe return jdialog

很抱歉,如果已经多次询问过这个问题,但我有一个 自定义 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帧传入的帧调用它。谢谢!

1 个答案:

答案 0 :(得分:2)

您可能需要查看How to Use Modality in Dialogs文档。首先,您不需要将JFrame传递给您的班级,您可以简单地将其删除。看一下这个例子:

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 GUI可能必须在不同的平台上工作   屏幕分辨率&amp;使用不同的PLAF。因此他们不是   有助于准确放置组件。组织组件   对于健壮的GUI,而是使用布局管理器或其组合   它们,以及布局填充和白色空间的边界。

因此,当您使用Swing组件时,应该完全避免使用setBounds()set(Preferred | Minimum | Maximum)Size()