67,513
社区成员
发帖
与我相关
我的任务
分享
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);
}
}
错误信息要给出来,这么长的代码,没有耐心看完的
[quote=引用 7 楼 yaoIin 的回复:] 没有调用ChatPanel的setSounthPanel,button没有被初始化。 // 南区域 southPanel = this.setSouthPanel(); //这里调用的是父类自己的setSouthPanel(); add(this.setSouthPanel(), BorderLayout.SOUTH);
@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;// 返回包含上述组件的面板对象
}
没有调用ChatPanel的setSounthPanel,button没有被初始化。 // 南区域 southPanel = this.setSouthPanel(); //这里调用的是父类自己的setSouthPanel(); add(this.setSouthPanel(), BorderLayout.SOUTH);