專案簡介:

1.實現利用下拉選單的方式選擇傳送快捷語句;

2.實現對留言資訊內容的置頂處理以及至尾處理;

3.實現清屏處理或現實保留部分留言內容;

執行介面:

程式碼展示:

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Choice;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;


public class Demo4 {

    // 測試
    public static void main(String[] args) {
        MessageBoard mb = new MessageBoard();
        mb.go();
    }
}

class MessageBoard {

    Frame f;
    WindowAdapter w;
    TextArea center;
    Label la1, la2, la3;
    Panel east, south, north;
    Button b1, b2, b3, b4;
    Choice l1;
    TextField t1;

    // 建構函式
    MessageBoard() {
        f = new Frame("留言板");
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent evt) {
                f.setVisible(false);
                System.exit(0);
            }
        });
        f.setBounds(0, 0, 600, 400);
        f.setLayout(new BorderLayout());
        f.setResizable(false);

        north = new Panel();
        south = new Panel();
        east = new Panel();

        center = new TextArea("留言內容:");
        center.setEditable(false);

        b1 = new Button("清屏");
        b2 = new Button("至頂");
        b3 = new Button("至尾");

        la1 = new Label("留言版");
        la2 = new Label("你");
        la3 = new Label(" 地說:");

        t1 = new TextField(20);

        b4 = new Button("提交");

        l1 = new Choice();
        l1.add("微笑  ");
        l1.add("生氣  ");
        l1.add("傷心  ");

    }

    public void go() {
        f.add(BorderLayout.NORTH, north);
        f.add(BorderLayout.SOUTH, south);
        f.add(BorderLayout.EAST, east);
        f.add(BorderLayout.CENTER, center);
        north.add(BorderLayout.CENTER, la1);
        south.add(la2);
        south.add(l1);
        south.add(la3);
        south.add(t1);
        south.add(b4);

        east.setLayout(new GridLayout(9, 0, 1, 10));
        east.add(b1);
        east.add(b2);
        east.add(b3);
        f.setVisible(true);

        b4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String text1, text2;
                text1 = l1.getSelectedItem();
                text2 = t1.getText();
                t1.setText(null);

                if (t1.getText() != "") {
                    center.append("\n");
                    center.append(text1);
                    center.append(text2);
                }
            }
        });

        b1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                center.setText("留言內容:");
            }
        });

        b2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                center.requestFocusInWindow();
                center.setCaretPosition(8);
            }
        });

        b3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                center.requestFocusInWindow();
                center.setCaretPosition(center.getText().length());
            }
        });
    }

}