| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 388 人关注过本帖
标题:[求助]一点小问题哈!
只看楼主 加入收藏
guo001
Rank: 1
等 级:新手上路
帖 子:82
专家分:0
注 册:2006-3-19
收藏
 问题点数:0 回复次数:2 
[求助]一点小问题哈!

import javax.swing.*;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
public class Chatroom extends JFrame implements ActionListener
{
JPanel pm,pn,ps,pw,pe,pc;
BorderLayout b;
ObjectOutputStream output;
ObjectInputStream input;
public Chatroom()
{
pm=new JPanel();
pn=new JPanel();
ps=new JPanel();
pw=new JPanel();
pe=new JPanel();
pc=new JPanel();
b=new BorderLayout();
pm.setLayout(b);
pm.add(pn,BorderLayout.NORTH);
pm.add(ps,BorderLayout.SOUTH);
pm.add(pw,BorderLayout.WEST);
pm.add(pe,BorderLayout.EAST);
pm.add(pc,BorderLayout.CENTER);

JLabel lbiaoti=new JLabel("Software 2005(1) Chatroom");
Color c1=new Color(197,248,184);
pn.setBackground(c1);
Color c2=new Color(246,17,50);
lbiaoti.setForeground(c2);
lbiaoti.setFont(new Font("楷体",Font.PLAIN,36));
pn.add(lbiaoti);

Color c3=new Color(249,170,224);
ps.setBackground(c3);
JLabel lmessage=new JLabel("Message:");
ps.add(lmessage);
JTextArea tmessage=new JTextArea(2,30);
//tmessage.addActionListener(this);
ps.add(new JScrollPane(tmessage));
JButton bsend,bquxiao;
bsend.addActionListener(this);
bquxiao.addActionListener(this);
Icon i=new ImageIcon("D:\\tupian\\send.jpg");
bsend=new JButton(i);
bquxiao=new JButton("Cancel");
bsend.setBackground(c3);
ps.add(bsend);
ps.add(bquxiao);

JPanel pw1=new JPanel();
JLabel lname,lpassword;
lname=new JLabel(" Name: ");
lpassword=new JLabel("Password:");
JTextField tname,tpassword;
tname=new JTextField(10);
tpassword=new JTextField(10);
JButton blogin,bcancel;
blogin=new JButton("Login");
bcancel=new JButton("Cancel");
Color c4=new Color(218,247,230);
pw.setBackground(c4);
pw1.setBackground(c3);
GridBagLayout gbl=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
pw1.setLayout(gbl);
JLabel kongge1=new JLabel(" ");
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbl.setConstraints(kongge1,gbc);
pw1.add(kongge1);
gbc.gridwidth=1;
gbl.setConstraints(lname,gbc);
pw1.add(lname);
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbl.setConstraints(tname,gbc);
pw1.add(tname);
JLabel kongge2=new JLabel(" ");
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbl.setConstraints(kongge2,gbc);
pw1.add(kongge2);
gbc.gridwidth=1;
gbl.setConstraints(lpassword,gbc);
pw1.add(lpassword);
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbl.setConstraints(tpassword,gbc);
pw1.add(tpassword);
JLabel kongge3=new JLabel(" ");
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbl.setConstraints(kongge3,gbc);
pw1.add(kongge3);
gbc.gridwidth=1;
gbl.setConstraints(blogin,gbc);
pw1.add(blogin);
JLabel kongge4=new JLabel(" ");
gbl.setConstraints(kongge4,gbc);
pw1.add(kongge4);
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbl.setConstraints(bcancel,gbc);
pw1.add(bcancel);
pw.add(pw1);

JTextArea tac=new JTextArea(29,45);
Color c5=new Color(156,236,244);
pc.setBackground(c5);
/* JScrollBar bar=new JScrollBar(Adjustable.VERTICAL,0,0,0,0);
bar.addAdjustmentListener(this);*/
// tac.setEditable(false);
pc.add(new JScrollPane(tac));

JTextArea tae=new JTextArea(29,13);
pe.setBackground(Color.getHSBColor(248,248,206));
pe.add(new JScrollPane(tae));

this.getContentPane().add(pm);
this.setSize(870,670);
this.setResizable(false);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e1)
{
if(e1.getSource()==bsend)
{
String message=tmessage.getText();
if(message.length()!=0)
{
output.writeObject("Say:"+message);
output.flush();
tac.append("\nSay:"+message);
tmessage.setText("");
}
else
{
getAppletContext().showStatus("Message Can not be empty!");
}
}
else if(evt.getSource()==tmessage)
{
output.writeObject("Say:"+evt.getActionCommand());
output.flush();
display.append("\nSay>>>"+evt.getActionCommand());
tmessage.setText("");
}
}

public static void main(String args[])
{
Chatroom obj=new Chatroom();
}
}
找不出错在那里了?

2006-06-17 11:04
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
public class Chatroom extends JFrame implements ActionListener
{
JPanel pm,pn,ps,pw,pe,pc;
BorderLayout b;
ObjectOutputStream output;
ObjectInputStream input;
public Chatroom()
{
pm=new JPanel();
pn=new JPanel();
ps=new JPanel();
pw=new JPanel();
pe=new JPanel();
pc=new JPanel();
b=new BorderLayout();
pm.setLayout(b);
pm.add(pn,BorderLayout.NORTH);
pm.add(ps,BorderLayout.SOUTH);
pm.add(pw,BorderLayout.WEST);
pm.add(pe,BorderLayout.EAST);
pm.add(pc,BorderLayout.CENTER);

JLabel lbiaoti=new JLabel("Software 2005(1) Chatroom");
Color c1=new Color(197,248,184);
pn.setBackground(c1);
Color c2=new Color(246,17,50);
lbiaoti.setForeground(c2);
lbiaoti.setFont(new Font("楷体",Font.PLAIN,36));
pn.add(lbiaoti);

Color c3=new Color(249,170,224);
ps.setBackground(c3);
JLabel lmessage=new JLabel("Message:");
ps.add(lmessage);
JTextArea tmessage=new JTextArea(2,30);
//tmessage.addActionListener(this);
ps.add(new JScrollPane(tmessage));
JButton bsend,bquxiao;//局部变量,没有初始化
bsend.addActionListener(this);
bquxiao.addActionListener(this);
Icon i=new ImageIcon("D:\\tupian\\send.jpg");
bsend=new JButton(i);
bquxiao=new JButton("Cancel");
bsend.setBackground(c3);
ps.add(bsend);
ps.add(bquxiao);

JPanel pw1=new JPanel();
JLabel lname,lpassword;
lname=new JLabel(" Name: ");
lpassword=new JLabel("Password:");
JTextField tname,tpassword;
tname=new JTextField(10);
tpassword=new JTextField(10);
JButton blogin,bcancel;
blogin=new JButton("Login");
bcancel=new JButton("Cancel");
Color c4=new Color(218,247,230);
pw.setBackground(c4);
pw1.setBackground(c3);
GridBagLayout gbl=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
pw1.setLayout(gbl);
JLabel kongge1=new JLabel(" ");
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbl.setConstraints(kongge1,gbc);
pw1.add(kongge1);
gbc.gridwidth=1;
gbl.setConstraints(lname,gbc);
pw1.add(lname);
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbl.setConstraints(tname,gbc);
pw1.add(tname);
JLabel kongge2=new JLabel(" ");
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbl.setConstraints(kongge2,gbc);
pw1.add(kongge2);
gbc.gridwidth=1;
gbl.setConstraints(lpassword,gbc);
pw1.add(lpassword);
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbl.setConstraints(tpassword,gbc);
pw1.add(tpassword);
JLabel kongge3=new JLabel(" ");
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbl.setConstraints(kongge3,gbc);
pw1.add(kongge3);
gbc.gridwidth=1;
gbl.setConstraints(blogin,gbc);
pw1.add(blogin);
JLabel kongge4=new JLabel(" ");
gbl.setConstraints(kongge4,gbc);
pw1.add(kongge4);
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbl.setConstraints(bcancel,gbc);
pw1.add(bcancel);
pw.add(pw1);

JTextArea tac=new JTextArea(29,45);
Color c5=new Color(156,236,244);
pc.setBackground(c5);
/* JScrollBar bar=new JScrollBar(Adjustable.VERTICAL,0,0,0,0);
bar.addAdjustmentListener(this);*/
// tac.setEditable(false);
pc.add(new JScrollPane(tac));

JTextArea tae=new JTextArea(29,13);
pe.setBackground(Color.getHSBColor(248,248,206));
pe.add(new JScrollPane(tae));

this.getContentPane().add(pm);
this.setSize(870,670);
this.setResizable(false);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e1)
{
if(e1.getSource()==bsend)
{
String message=tmessage.getText();
if(message.length()!=0)
{
output.writeObject("Say:"+message);
output.flush();
tac.append("\nSay:"+message);
tmessage.setText("");
}
else
{
getAppletContext().showStatus("Message Can not be empty!");//这是APPLET里的方法,你也用上?人才!
}
}
else if(evt.getSource()==tmessage)
{
output.writeObject("Say:"+evt.getActionCommand());
output.flush();
display.append("\nSay>>>"+evt.getActionCommand());
tmessage.setText("");
}
}

public static void main(String args[])
{
Chatroom obj=new Chatroom();
}
}

建议去看一下基础的东西,你的所有的变量都是在构造函数里面存活的,怎么可能别的方法能调用到呢


可惜不是你,陪我到最后
2006-06-17 11:08
guo001
Rank: 1
等 级:新手上路
帖 子:82
专家分:0
注 册:2006-3-19
收藏
得分:0 
多谢版主!!!

2006-06-17 12:12
快速回复:[求助]一点小问题哈!
数据加载中...
 
   



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

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