Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.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 不显示带有JOptione窗格的消息diologe_Java_Swing_Document_Jtextfield_Joptionpane - Fatal编程技术网

Java 不显示带有JOptione窗格的消息diologe

Java 不显示带有JOptione窗格的消息diologe,java,swing,document,jtextfield,joptionpane,Java,Swing,Document,Jtextfield,Joptionpane,我已经写了一个代码来创建一个sms表单,我想添加在文本区域为空时显示错误消息的功能。我把JOptionpane放在我的代码中,但是当我运行程序时,diologe没有出现!这是我的密码 private void initialize() { frame = new JFrame("?????? ? ????? ?????"); frame.setBounds(100, 100, 450, 300); frame.setDefaultCloseOperation(JFrame

我已经写了一个代码来创建一个sms表单,我想添加在文本区域为空时显示错误消息的功能。我把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);
            }
        });


    }
    }

GUI是事件驱动的环境。如果发生了什么事,你会做出反应

您的
if-else
语句永远不会是
false
,因为执行该语句时,
textarea
将为空(没有文本)

您需要响应某个事件(例如,
send
),此时您将进行检查以使表单有效

请查看以了解更多详细信息

用示例更新

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
是否包含文本

已更新

mKobel提出了一个极好的观点。您确实应该避免使用
null
布局。您无法控制应用程序可能需要使用的字体大小或屏幕DPI/分辨率。布局管理员进行猜测工作


您应该尝试签出,有关更多详细信息,您的
textarea
行数为0的原因是什么?什么时候会发生这种情况?当用户在字段中不输入任何字符时,我想显示报警窗口,例如逻辑。如果文本区域不包含文本,是否显示错误消息?或者如果文本区域包含文本,您想显示错误消息吗?@mKorbel哦,我有一个粉丝俱乐部,他们就像向我扔飞镖一样;)@mKorbel我不介意投票,只要有人能解释为什么