Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 制作自定义JOptionPane对话框时遇到问题_Java_Eclipse_Button_Joptionpane - Fatal编程技术网

Java 制作自定义JOptionPane对话框时遇到问题

Java 制作自定义JOptionPane对话框时遇到问题,java,eclipse,button,joptionpane,Java,Eclipse,Button,Joptionpane,所以,在你说我应该做更多的研究之前,我已经试着弄清楚这一点大约一周了,这是我最后的选择。接下来,我尝试创建一个包含多个按钮的对话框;计算器。尽管如此,我几乎不知道javax.swing的任何方法。我只知道JOptionPane方法和一点JTextField。请,如果可能的话,给我一个提示或一些线索,我需要做什么,以便我能自己弄清楚。谢谢您的时间。JOptionPane没有您想要的功能。如果你想要更多的按钮填充你的对话框,你需要一些其他的组件。这是一个计算器的框架。您必须添加逻辑: import

所以,在你说我应该做更多的研究之前,我已经试着弄清楚这一点大约一周了,这是我最后的选择。接下来,我尝试创建一个包含多个按钮的对话框;计算器。尽管如此,我几乎不知道javax.swing的任何方法。我只知道JOptionPane方法和一点JTextField。请,如果可能的话,给我一个提示或一些线索,我需要做什么,以便我能自己弄清楚。谢谢您的时间。

JOptionPane没有您想要的功能。如果你想要更多的按钮填充你的对话框,你需要一些其他的组件。这是一个计算器的框架。您必须添加逻辑:

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Calculator extends JDialog {

    private final JPanel contentPanel = new JPanel();
    private JTextField textField;

    public static void main(String[] args) {
        try {
            Calculator dialog = new Calculator();
            dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            dialog.setVisible(true);
        } catch (Exception e) {
        }
    }

    public Calculator() {
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent arg0) {
                JOptionPane.showMessageDialog(null, "Bye");
            }
        });
        setBounds(100, 100, 348, 234);
        getContentPane().setLayout(new BorderLayout());
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        getContentPane().add(contentPanel, BorderLayout.CENTER);
        contentPanel.setLayout(null);
        {
            textField = new JTextField();
            textField.setHorizontalAlignment(SwingConstants.RIGHT);
            textField.setBounds(12, 13, 323, 19);
            contentPanel.add(textField);
            textField.setColumns(10);
        }

        JButton button_8 = new JButton("8");
        button_8.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                textField.setText("8 pressed");
            }
        });
        button_8.setBounds(79, 46, 55, 25);
        contentPanel.add(button_8);

        JButton button_9 = new JButton("9");
        button_9.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                textField.setText("9 pressed");
            }
        });
        button_9.setBounds(146, 46, 55, 25);
        contentPanel.add(button_9);

        JButton button_4 = new JButton("4");
        button_4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                textField.setText("4 pressed");
            }
        });
        button_4.setBounds(12, 83, 55, 25);
        contentPanel.add(button_4);

        JButton button_5 = new JButton("5");
        button_5.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                textField.setText("5 pressed");
            }
        });
        button_5.setBounds(79, 83, 55, 25);
        contentPanel.add(button_5);

        JButton button_6 = new JButton("6");
        button_6.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                textField.setText("6 pressed");
            }
        });
        button_6.setBounds(146, 83, 55, 25);
        contentPanel.add(button_6);

        JButton button_1 = new JButton("1");
        button_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                textField.setText("1 pressed");
            }
        });
        button_1.setBounds(12, 120, 55, 25);
        contentPanel.add(button_1);

        JButton button_2 = new JButton("2");
        button_2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                textField.setText("2 pressed");
            }
        });
        button_2.setBounds(79, 120, 55, 25);
        contentPanel.add(button_2);

        JButton button_3 = new JButton("3");
        button_3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                textField.setText("3 pressed");
            }
        });
        button_3.setBounds(146, 120, 55, 25);
        contentPanel.add(button_3);

        JButton button_0 = new JButton("0");
        button_0.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                textField.setText("0 pressed");
            }
        });
        button_0.setBounds(12, 157, 122, 25);
        contentPanel.add(button_0);

        JButton button_point = new JButton(".");
        button_point.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                textField.setText(". pressed");
            }
        });
        button_point.setBounds(146, 157, 55, 25);
        contentPanel.add(button_point);

        JButton button_divide = new JButton("/");
        button_divide.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                textField.setText("/ pressed");
            }
        });
        button_divide.setBounds(213, 46, 55, 25);
        contentPanel.add(button_divide);

        JButton button_multiply = new JButton("*");
        button_multiply.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                textField.setText("* pressed");
            }
        });
        button_multiply.setBounds(213, 83, 55, 25);
        contentPanel.add(button_multiply);

        JButton button_subtract = new JButton("-");
        button_subtract.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                textField.setText("- pressed");
            }
        });
        button_subtract.setBounds(213, 120, 55, 25);
        contentPanel.add(button_subtract);

        JButton button_sum = new JButton("+");
        button_sum.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                textField.setText("+ pressed");
            }
        });
        button_sum.setBounds(213, 157, 55, 25);
        contentPanel.add(button_sum);

        JButton button_back = new JButton("<");
        button_back.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                textField.setText("Back pressed");
            }
        });
        button_back.setBounds(280, 46, 55, 25);
        contentPanel.add(button_back);

        JButton button_clear = new JButton("C");
        button_clear.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                textField.setText("Clear pressed");
            }
        });
        button_clear.setBounds(280, 83, 55, 25);
        contentPanel.add(button_clear);

        JButton button_equals = new JButton("=");
        button_equals.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                textField.setText("= pressed");
            }
        });
        button_equals.setBounds(280, 120, 55, 62);
        contentPanel.add(button_equals);

        JButton button_7 = new JButton("7");
        button_7.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                textField.setText("7 pressed");
            }
        });
        button_7.setBounds(12, 46, 55, 25);
        contentPanel.add(button_7);
        {
            JPanel buttonPane = new JPanel();
            buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
            getContentPane().add(buttonPane, BorderLayout.SOUTH);
        }
    }
}

希望能有帮助。提示:这是使用WindowBuiler for Eclipse制作的。您也应该试试。

非常感谢您的帮助!尽管它不适用于JOptionPane,但这对我来说是巨大的帮助,我将在我的一个学习大厅中探索更多关于WindowBuilder的主题。再次感谢,祝你度过美好的一天/一夜。