| 编程中国 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛
全能ASP/PHP/ASP.NET主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付买域名,送MP3、MP4
高端软件开发 = 年薪十万不是梦赛孚耐:软件保护加密专家身份认证令牌USB KEY买空间,免费送域名(厦门中资源)
共有 242 人关注过本帖
标题:AWT 的问题
收藏  订阅  推荐  打印 
zhao9302
Rank: 2
等级:注册会员
帖子:170
积分:1806
注册:2007-5-10
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-8-16 14:10
zhao9302
Rank: 2
等级:注册会员
帖子:170
积分:1806
注册:2007-5-10

咋没人来看下呢?

我要一步一步往上爬……
2008-8-16 22:05
fengwei15520
Rank: 1
等级:新手上路
帖子:15
积分:318
注册:2008-3-6
回复 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-8-18 17:27
zhao9302
Rank: 2
等级:注册会员
帖子:170
积分:1806
注册:2007-5-10

谢谢啦

我要一步一步往上爬……
2008-8-19 22:48
关于我们 | 广告合作 | 编程中国 | 清除Cookies | Archiver | WAP | TOP

编程中国 版权所有,并保留所有权利。鲁ICP备08000592号
Powered by Discuz, Processed in 0.060358 second(s), 9 queries.
Copyright©2004-2008, BCCN.NET, All Rights Reserved