java 网络编程 聊天界面,为什么空指针?小白求助!!!

kevin31022 2016-12-07 04:51:25

public class MyPanel2 extends JPanel {

// 北区域,可根据size的值创建多个标签和文本框
protected int size;
protected JLabel labels[];
protected JButton doTask1;
protected JTextField fields[];
protected JLabel promptLabel;
// 中区域,文本区
protected JTextArea textArea;
// 南区域,根据派生子类的要求可放置不同的图形组件
JPanel southPanel;

public MyPanel2(int mySize) {
setLayout(new BorderLayout());
// 北区域
size = mySize;
labels = new JLabel[size];
fields = new JTextField[size];
for (int count = 0; count < labels.length; count++)
labels[count] = new JLabel("标签" + count, SwingConstants.RIGHT);
for (int count = 0; count < fields.length; count++)
fields[count] = new JTextField(12);
JPanel innerPanelCenter = new JPanel();
for (int count = 0; count < size; count++) {
JPanel innerPanel = new JPanel();
innerPanel.add(labels[count]);
innerPanel.add(fields[count]);
innerPanelCenter.add(innerPanel);
}
doTask1 = new JButton("确定");
innerPanelCenter.add(doTask1);
promptLabel = new JLabel("设置提示!");
promptLabel.setForeground(Color.red);
promptLabel.setBorder(BorderFactory.createTitledBorder("提示"));
JPanel northPanel = new JPanel(new BorderLayout());
northPanel.add(innerPanelCenter, BorderLayout.CENTER);
northPanel.add(promptLabel, BorderLayout.SOUTH);
add(northPanel, BorderLayout.NORTH);
// 中区域
textArea = new JTextArea();
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setFont(new Font("幼圆", Font.PLAIN, 16));
add(new JScrollPane(textArea), BorderLayout.CENTER);
// 南区域
southPanel = this.setSouthPanel();
add(this.setSouthPanel(), BorderLayout.SOUTH);
}

// 该方法创建南区的图形界面组件,子类中根据需要覆盖它
protected JPanel setSouthPanel() {
JPanel panelSouth = new JPanel();
panelSouth.add(new JLabel("子类中需要重写南边的图形组件,以满足不同要求!"));
return panelSouth;
}
}


下边这个类继承上边这个


public class ChatPanel extends MyPanel2 implements ActionListener {

protected JButton sendButton;
protected JButton exitButton;
protected JTextField southField;
protected String nickname = "kevin";

// 初始化面板中的图形组件
public ChatPanel() {
// 图形界面继承自父类,但修改了文字提示和南区图形界面

super(1);// 调用父类构造方法:有一个标签labels[0]和一个文本框fields[0]
labels[0].setText("输入昵称");
promptLabel.setText("点击\"确定\"按钮,若服务器和客户连接上后,可开始聊天!");
doTask1.addActionListener(this);// 北区"确定"按钮增加监听
sendButton.addActionListener(this);// 南区"发送"按钮增加监听
//这行报空指针异常
exitButton.addActionListener(this);// 南区"离线"按钮增加监听
}

// “确定”、“发送”、“离线”3个按钮被单击时将执行的任务
public void actionPerformed(ActionEvent e) {
if (e.getSource() == doTask1) {
doTask1Button();
}
if (e.getSource() == sendButton) {
sendButton();
}
if (e.getSource() == exitButton) {
exitButton();
}
}

// 单击北区"确定"按钮执行的动作:只提供空方法体,由子类实现具体功能
protected void doTask1Button() {
}

// 单击南区"发送"按钮执行的动作:只提供空方法体,由子类实现具体功能
protected void sendButton() {
}

// 单击南区"离线"按钮执行的动作:只提供空方法体,由子类实现具体功能
protected void exitButton() {
}

// 覆盖父类同名方法,重新设置聊天室南区的GUI:
protected JPanel setSounthPanel() {
// 聊天面板南区:一个文本框(输入聊天内容)、两个按钮(“发送”和“离线”)
JPanel southPanel = new JPanel();// 放置南区组件的面板对象
southField = new JTextField(20);// 输入聊天内容的文本框
southPanel.add(southField);

sendButton = new JButton("发送");// "发送"按钮
sendButton.setEnabled(false);// 初始设置为不可用
southPanel.add(sendButton);

exitButton = new JButton("离线");// "离线"按钮
exitButton.setEnabled(false);// 初始设置为不可用
southPanel.add(exitButton);
return southPanel;// 返回包含上述组件的面板对象
}

// 测试ChatPanel
public static void main(String args[]) {
JFrame app = new JFrame("聊天界面");
app.getContentPane().add(new ChatPanel());
app.setSize(400, 300);
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setVisible(true);
}
}

...全文
250 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
kevin31022 2016-12-08
  • 打赏
  • 举报
回复
引用 1 楼 qq_34644919 的回复:
哪里开始空指针的? 你调试下,问题代码是哪块再分析
第二段代码的第16行
kevin31022 2016-12-08
  • 打赏
  • 举报
回复
引用 4 楼 kevin31022 的回复:
加了红色,但是不知道为什么不显示,而且为什么不能放照片呢? 在这里 sendButton.addActionListener(this);// 南区"发送"按钮增加监听 //<span style="color: #FF0000;">这行报空指针异常</span> exitButton.addActionListener(this);// 南区"离线"按钮增加监听
引用 2 楼 u010234516 的回复:
错误信息要给出来,这么长的代码,没有耐心看完的
第二个代码的第16行
kevin31022 2016-12-08
  • 打赏
  • 举报
回复
加了红色,但是不知道为什么不显示,而且为什么不能放照片呢?

在这里
sendButton.addActionListener(this);// 南区"发送"按钮增加监听
//<span style="color: #FF0000;">这行报空指针异常</span>
exitButton.addActionListener(this);// 南区"离线"按钮增加监听

引用 2 楼 u010234516 的回复:
错误信息要给出来,这么长的代码,没有耐心看完的
  • 打赏
  • 举报
回复
「已注销」 2016-12-08
  • 打赏
  • 举报
回复
引用 8 楼 kevin31022 的回复:
[quote=引用 7 楼 yaoIin 的回复:] 没有调用ChatPanel的setSounthPanel,button没有被初始化。 // 南区域 southPanel = this.setSouthPanel(); //这里调用的是父类自己的setSouthPanel(); add(this.setSouthPanel(), BorderLayout.SOUTH);
可以说详细一点,或者说下怎么改吗?[/quote] 在子类的重写方法加上@Override注解,你会发现你的方法名写错了,并没有重写父类的方法而是一个新的方法,父类的方法为setSouthPanel(),子类的方法为setSounthPanel(),sounth拼写错误。 修正一下这个方法就可以了。

	@Override
	protected JPanel setSouthPanel() {
		// 聊天面板南区:一个文本框(输入聊天内容)、两个按钮(“发送”和“离线”)
		JPanel southPanel = new JPanel();// 放置南区组件的面板对象
		southField = new JTextField(20);// 输入聊天内容的文本框
		southPanel.add(southField);

		sendButton = new JButton("发送");// "发送"按钮
		sendButton.setEnabled(false);// 初始设置为不可用
		southPanel.add(sendButton);

		exitButton = new JButton("离线");// "离线"按钮
		exitButton.setEnabled(false);// 初始设置为不可用
		southPanel.add(exitButton);
		return southPanel;// 返回包含上述组件的面板对象
	}
kevin31022 2016-12-08
  • 打赏
  • 举报
回复
引用 7 楼 yaoIin 的回复:
没有调用ChatPanel的setSounthPanel,button没有被初始化。 // 南区域 southPanel = this.setSouthPanel(); //这里调用的是父类自己的setSouthPanel(); add(this.setSouthPanel(), BorderLayout.SOUTH);
可以说详细一点,或者说下怎么改吗?
「已注销」 2016-12-08
  • 打赏
  • 举报
回复
没有调用ChatPanel的setSounthPanel,button没有被初始化。 // 南区域 southPanel = this.setSouthPanel(); //这里调用的是父类自己的setSouthPanel(); add(this.setSouthPanel(), BorderLayout.SOUTH);
_古井心 2016-12-07
  • 打赏
  • 举报
回复
错误信息要给出来,这么长的代码,没有耐心看完的
尘同学 2016-12-07
  • 打赏
  • 举报
回复
哪里开始空指针的? 你调试下,问题代码是哪块再分析

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧