⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 actiondemo.java

📁 简单聊天程序
💻 JAVA
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. *//** * * @author Administrator */import java.awt.*;import java.awt.event.*;import javax.swing.*;public class ActionDemo extends JFrame{    JTextField jtfName;    JTextArea jtaChat;    JTextArea jtaInput;    JButton jbSend;    JButton jbClear;    public ActionDemo(){        super("控件的相互控制");                Container container=this.getContentPane();        JPanel p=new JPanel();        jtfName=new JTextField(10);        p.add(new JLabel("主题:Write Once,Run Anywhere. "));        p.add(new JLabel("昵称"));        p.add(jtfName);        container.add(p,BorderLayout.NORTH );        jtaChat=new JTextArea();        container.add(new JScrollPane(jtaChat),BorderLayout.CENTER );        Box box=new Box(BoxLayout.X_AXIS);        jtaInput=new JTextArea(3,20);        jbSend=new JButton();        jbClear=new JButton();        jbClear.setText("清除");        box.add(new JScrollPane(jtaInput));        box.add(jbClear);        box.add(jbSend);        container.add(box,BorderLayout.SOUTH );        jbClear.addActionListener(new ActionListener(){        public void actionPerformed(ActionEvent e){        jtaInput.setText("");        }        });        Action sendMessage=new AbstractAction(){        public void actionPerformed(ActionEvent e){            replaceMessage();        }        };        jtaInput.getInputMap().put(KeyStroke.getKeyStroke("ENTER"),"send");        jtaInput.getActionMap().put("send",sendMessage);        jbSend.setAction (sendMessage);        jbSend.setText("发送");        setSize(400,200);        setVisible(true);        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    }    private void replaceMessage(){    String message=jtfName.getText()+">"+jtaInput.getText()+"\n";    jtaChat.insert(message,jtaChat.getDocument().getLength());    jtaInput.setText("");    }public static void main(String[] args){        new ActionDemo();}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -