Java 由浅入深GUI编程实战练习(一)

2018-11-28 08:52:37来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

项目简介:

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());
            }
        });
    }

}

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:spring boot2集成ES详解

下一篇:Java并发编程系列(二)-synchronized同步锁