-1

我有这个简单的 GUI,它要求输入字符串,然后将其写入文本文件,但如果没有输入,JLabel 会显示错误消息,我希望该错误消息保持 5 秒

    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import javax.swing.*;    
    import javax.swing.JFrame;    
    import net.miginfocom.swing.MigLayout;

    public class Q1 extends JFrame {

    private JLabel lblString, lblMessage;
    private JTextField txtString;
    private JButton btnStore;
    private JPanel thePanel;

    public static void main(String[] args) {    
        new Q1();    
    }// End of main

    public Q1() {

        super("Store your text");
        this.setSize(600, 100);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    
        thePanel = new JPanel(new MigLayout());    
        lblString = new JLabel("Enter Your Text :");    
        txtString = new JTextField(50);    
        btnStore = new JButton("Store");
        // Listener for Store Button
        ListenerForButton lForButton = new ListenerForButton();    
        btnStore.addActionListener(lForButton);    
        lblMessage = new JLabel();    
        thePanel.add(lblString);
        thePanel.add(txtString, "wrap");
        thePanel.add(btnStore, "skip,split2");
        thePanel.add(lblMessage, "gapleft 200");    
        this.add(thePanel);    
        this.setResizable(false);
        this.setVisible(true);    
    }// End of constructor

    // Listener implement    
    public class ListenerForButton implements ActionListener {

        public void actionPerformed(ActionEvent e) {

            if (e.getSource() == btnStore) {    
                if (txtString.getText().equals("")) {    
                    lblMessage.setText("ERROR-NO Text is given !");
                    lblMessage.setForeground(Color.red);    
                } else {
                    try {
                        String str = txtString.getText();    
                        File file = new File("appending-Text-File.txt");    
                        // if file doesnt exists, then create it
                        if (!file.exists()) {    
                            file.createNewFile();
                        }    
                        FileWriter fileWritter = new FileWriter(file.getName(),
                                true);    
                        BufferedWriter bufferWritter = new BufferedWriter(
                                fileWritter);    
                        bufferWritter.newLine();// Write a new line
                        bufferWritter.write(str);
                        bufferWritter.close();    
                        lblMessage.setText("String is successfully stored !");    
                        txtString.setText("");    
                    } catch (IOException ex) {    
                        ex.printStackTrace();
                    }
                }
            }
        }// End actionPerfomred
    }    
}// End of Class
4

1 回答 1

0

尝试将您的 if 块更新为:

  if (txtString.getText().equals("")) {    
     lblMessage.setText("ERROR-NO Text is given !");
     lblMessage.setForeground(Color.red);
     Thread.currentThread().sleep(5000);
     lblMessage.setText("");
  }
于 2013-05-13T16:53:13.893 回答