没有显示JOptionpane的消息对话框

时间:2013-08-12 05:20:15

标签: java swing document jtextfield joptionpane

我已经编写了一个代码来创建一个短信表单,我想添加在文本区域为空时显示错误消息的功能。我把JOptionpane放在我的代码中但是当我运行程序时不会出现diologe剂量!这是我的代码

private void initialize() {
    frame = new JFrame("?????? ? ????? ?????");
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JOptionPane optionPane = new JOptionPane();


    JPanel middlePanel = new JPanel();

    txtPath = new JTextField();
    txtPath.setBounds(150, 10, 200, 21);
    frame.getContentPane().add(txtPath);
    txtPath.setColumns(10);

    txtPath2 = new JTextField();
    txtPath2.setBounds(150, 65, 200, 21);
    frame.getContentPane().add(txtPath2);
    txtPath2.setColumns(20);

    JButton btnBrowse = new JButton("?????");
    btnBrowse.setBounds(5, 10, 87, 23);
    frame.getContentPane().add(btnBrowse);

    final JButton ok = new JButton("?????");
    ok.setBounds(250, 230, 87, 23);
    frame.getContentPane().add(ok);

    JButton cancel = new JButton("???");
    cancel.setBounds(110, 230, 87, 23);
    frame.getContentPane().add(cancel);


    final JTextArea textarea = new JTextArea();
    textarea.setBounds(50, 100, 300, 100);
    frame.getContentPane().add(textarea);
    textarea.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);




    JProgressBar progressBar = new JProgressBar(0, 100);
    progressBar.setSize(10, 1);
    progressBar.setForeground(Color.blue);
    frame.getContentPane().add(progressBar);





    btnBrowse.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JFileChooser fileChooser = new JFileChooser();

            // For Directory
            fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

            // For File
            //fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);

            fileChooser.setAcceptAllFileFilterUsed(false);

            int rVal = fileChooser.showOpenDialog(null);
            if (rVal == JFileChooser.APPROVE_OPTION) {
                txtPath.setText(fileChooser.getSelectedFile().toString());
                fileChooser.setFileFilter(new FileNameExtensionFilter("Text Files", "txt", "rtf"));

            }
        }
    });



   ok.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if(textarea.getLineCount()>=1)
            {


          test t=new test();
            ReadTextFile readTextFile=new ReadTextFile();
            t.testMethode(txtPath2.getText(), textarea.getText(),readTextFile.readFile(txtPath.getText()) );
        }
            else
                JOptionPane.showMessageDialog(null, "alert", "alert", JOptionPane.ERROR_MESSAGE);
        }
    });




        cancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                System.exit(0);
            }
        });


    }
    }

1 个答案:

答案 0 :(得分:1)

GUI是事件驱动的环境。有些事情发生了,你回应它。

if-else语句永远不会是false,因为执行时间textarea将为空(没有文字)。

您需要回复某些事件(例如send),此时您需要检查以确认表单有效。

请查看Creating a GUI with Swing了解详情

更新了示例

enter image description here

public class Example {

    public static void main(String[] args) {
        new Example();
    }

    public Example() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private final JTextArea msg;

        public TestPane() {

            msg = new JTextArea(10, 20);
            JButton send = new JButton("Send");

            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;

            add(new JScrollPane(msg), gbc);
            add(send, gbc);

            send.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (msg.getText().trim().length() > 0) {
                        // Send msg
                    } else {
                        JOptionPane.showMessageDialog(TestPane.this, "Please write something (nice)", "Error", JOptionPane.ERROR_MESSAGE);
                    }
                }
            });

        }

    }
}

根据OP

的答案更改进行了更新

if(textarea.getLineCount()>=1)将始终返回true。请尝试使用msg.getText().trim().length() > 0来确定JTextArea是否包含文字......

<强>更新

麦肯贝尔提出了一个很好的观点。你真的应该避免使用null布局。您无法控制应用程序可能需要处理的字体大小或屏幕DPI /分辨率。布局管理人员开始猜测工作。

您应该尝试查看A visual guide to layout managersUsing layout managers了解更多详情