Java的GUI练习之——留言板实现

来源:互联网 发布:淘宝网购物天猫 编辑:程序博客网 时间:2024/06/06 06:31

代码实现如下:

package GUI;

importjava.awt.*;

importjava.awt.event.ActionEvent;

importjava.awt.event.ActionListener;

importjava.io.BufferedReader;

importjava.io.File;

importjava.io.FileOutputStream;

importjava.io.FileReader;

importjava.text.SimpleDateFormat;

importjava.util.Date;

importjavax.swing.*;

public classTestLiuyan extends JApplet {

      private static final long serialVersionUID= -1859951364467014767L;

      static JFrame frame = new JFrame();

      // 创建一个容器

      private JPanel jpComponents = newJPanel();

      // 创建一个文本域

      private JTextArea jtfDescription = newJTextArea();

      static String text = null;

      private JButton button_check = newJButton("查看/刷新留言");

      private JButton button_add = newJButton("添加留言");

 

      public static void main(String[] args) {

             TestLiuyan applet = new TestLiuyan();

             // EXIT_ON_CLOSE == 3

             frame.setDefaultCloseOperation(3);

             frame.setTitle("ShowLayout");

             frame.add(applet);

             //frame.getContentPane().add(applet);

             // applet.init(); applet.start();

             frame.setSize(400, 320);

             frame.setLocationRelativeTo(null);

             frame.setVisible(true);

      }

      public TestLiuyan() {

             jpComponents.setLayout(newGridLayout(1, 2,2,4));

             jpComponents.revalidate();

       jtfDescription.setLineWrap(true);

         jtfDescription.setWrapStyleWord(true);

             jpComponents.add(button_check);

             jpComponents.add(button_add);

             JSplitPane jSplitPane = newJSplitPane(JSplitPane.VERTICAL_SPLIT,

                           jpComponents, newJScrollPane(jtfDescription));

             add(jSplitPane,BorderLayout.CENTER);

             add(jSplitPane,BorderLayout.CENTER);

             addMyListener();

      }

 

      private void addMyListener() {

             button_check.addActionListener(newActionListener() {

                    public voidactionPerformed(ActionEvent e) {

                           jtfDescription.setText("");

                           try {

                                  BufferedReaderbr = new BufferedReader(new FileReader(

                                                newFile("F://1.txt")));

                                  String line;

                                  while ((line =br.readLine()) != null) {

                                         jtfDescription.append(line+ "\r\n");

                                  }

                                  br.close();

                           } catch (Exceptione2) {

                           }

                    }

             });

             button_add.addActionListener(newActionListener() {

                    //设置变量用于判断是否循环

                    boolean right;

                    public voidactionPerformed(ActionEvent e) {

                           try {

                                  do{

                                         right=false;

                                         if(button_add.getText() == "添加留言") {

                                                button_add.setText("提交留言");

                                                jtfDescription.setText("");

                                         } else{

                                                button_add.setText("添加留言");

                                                Filefile = new File("F://1.txt");

                                                FileOutputStreamfos = new FileOutputStream(file,

                                                              true);

                                                SimpleDateFormatsdf = new SimpleDateFormat(

                                                              "yyyy-MM-ddhh:mm:ss");

                                                Stringdatetime = sdf.format(new Date());

                                                text= jtfDescription.getText();

                                                if(text.length() == 0) {

                                                       jtfDescription.append("留言为空,请重新输入");

                                                }else {

                                                       fos.write(text.getBytes());

                                                       fos.write("\r\n".getBytes());

                                                       fos.write(datetime.getBytes());

                                                       Stringtext1 = "\r\n------------------------------------\r\n";

                                                       fos.write(text1.getBytes());

                                                       jtfDescription.setText("");

                                                       jtfDescription.append("留言添加成功");

                                                       fos.flush();

                                                       fos.close();

                                                }

                                         }

                                  }while(right==true);

                           } catch (Exceptione1) {

                           }

                    }

             });

      }

}

如有疑问或发现问,欢迎讨论

0 0
原创粉丝点击