标题:AWT 的问题
只看楼主
zhao9302
Rank: 1
等 级:新手上路
帖 子:170
专家分:0
注 册:2007-5-10
 问题点数:0 回复次数:3 
AWT 的问题
import java.awt.*;
import java.awt.event.*;


public class ChatClient extends Frame
{
    public static void main(String agrs[])
    {
        new ChatClient().getFrame();
    }
    
    private void getFrame()
    {
        this.setLocation(400, 200);
        this.setSize(300, 400);
        this.setVisible(true);
        this.setResizable(false);
        this.setTitle("ChatWindow");
        
        this.addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }
        });
        
        getTextArea();
        getTextField();
        getCancelButton();
        getSendButton();
    }
    
    private void getTextArea()
    {
        TextArea ta = new TextArea();
        ta.setSize(280,250);
        ta.setLocation(10, 40);
    
        this.add(ta);
    }
    
    private void getTextField()
    {
        TextField tf = new TextField();
        tf.setSize(280, 40);
        tf.setLocation(10, 300);
        
        this.add(tf);    
    }
    
    private void getCancelButton()
    {
        Button cancelButton = new Button();
        cancelButton.setLocation(170,350);
        cancelButton.setSize(60, 30);
        cancelButton.setLabel("取消");
        
        cancelButton.addActionListener(new ActionListener()
        {

            public void actionPerformed(ActionEvent arg0)
            {
                // TODO Auto-generated method stub
                System.exit(0);
                
            }
                
        });
        
        this.add(cancelButton);    
    }
    
    private void getSendButton()
    {
        Button sendButton = new Button();
        sendButton.setLocation(70, 350);
        sendButton.setSize(60, 30);
        sendButton.setLabel("发送");
        
        this.add(sendButton);
    }
}

上面是我的代码
 我现在有两个问题
1.如何使得TextField 和 TextArea能够自动的换行
2.this.setResizable(false);这句话不要的话 将会导致页面变的乱七八糟 有啥办法可以使得最大化页面的大体样子不变么 难道只能用 设计划分页面为N个模块
搜索更多相关主题的帖子: AWT void awt public java 
2008-08-16 14:10
zhao9302
Rank: 1
等 级:新手上路
帖 子:170
专家分:0
注 册:2007-5-10
得分:0 
咋没人来看下呢?

我要一步一步往上爬……
2008-08-16 22:05
fengwei15520
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2008-3-6
得分:0 
回复 1# zhao9302 的帖子
AWT做的图形界面一般都用Swing做,我根据你说的问题用Swing 做了一下,你看一看
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class ChatClient1 extends JFrame
{
    JTextArea jt;
    JTextField jf;
    JButton sendButton,cancelButton;
    public ChatClient1()
    {
        super("ChatWindow");
        jt=new JTextArea(15,30);
        jt.setLineWrap(true);
      sendButton=new JButton();
      cancelButton=new JButton("取消");
      jf=new JTextField();
      JPanel jp=new JPanel();
        jp.add(sendButton);
        jp.add(cancelButton);
        
        Container ca=getContentPane();
        ca.add(jt,BorderLayout.NORTH);
        ca.add(jf,BorderLayout.CENTER);
        ca.add(jp,BorderLayout.SOUTH);
        cancelButton.addActionListener(new ActionListener()
        {

            public void actionPerformed(ActionEvent arg0)
            {
               jf.setText("");
               
            }
               
        });
        Action sendMessage = new AbstractAction() {  //发送消息Action
           public void actionPerformed(ActionEvent e){
               replaceMessage();  //更新消息显示框
            }
        };
        jf.getInputMap().put(KeyStroke.getKeyStroke("ENTER"),"send");  //键盘事件处理,按受回车事件
        jf.getActionMap().put("send",sendMessage);  //回车时的处理(调用发送消息Action)
        sendButton.setAction(sendMessage);
        sendButton.setText("发送");
        
        setSize(500,400);
        setLocation(100,100);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    private void replaceMessage()
    {
        String message=jf.getText()+"\n";
        jt.insert(message,jt.getDocument().getLength());
        jf.setText("");
    }
        public static void main(String []args)
    {
      new ChatClient1();
    }
}
2008-08-18 17:27
zhao9302
Rank: 1
等 级:新手上路
帖 子:170
专家分:0
注 册:2007-5-10
得分:0 
谢谢啦

我要一步一步往上爬……
2008-08-19 22:48



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-228591-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.055179 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved