这个项目已经做了三天了,还没解决,希望有心人看见帮帮忙任务要求:模仿聊天室界面首先是一个输入框和按钮--(当点击按钮后弹出下一个窗口)--登陆窗口(两个输入框和按钮)--按钮后弹出下一个窗口--个人信息窗口(名字、性别、年龄等属性的输入框和按钮)--当点击按钮后弹出聊天窗口:聊天窗口又包括两种,一种是私聊窗口,一种是群聊窗口出现问题:当弹出第二个窗口后,点击按钮,出现报错。无法解决。。。。求解部分代码的流程设计思路,通过图片,怕大家看不懂我写的代码import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import javax.swing.*;
public class ClientGUIDemo {
private Socket s;
private static OutputStream out;
private InputStream in;
static String ipServer;
int deterCnt;
int buttonCnt;
public static JFrame signInFrame = new JFrame("QQ2035");
public static JFrame waitJFrame = new JFrame("QQ2035");
public static JFrame ipFrame = new JFrame("QQ2035");
public static JTextField ipText = new JTextField("127.0.0.1", 15);
// 输出窗提示信息
public static JTextField countText = new JTextField("QQ号码/手机/邮箱", 15);
public static JPasswordField pwdText = new JPasswordField(15);
// *个人信息窗
public static JFrame P = new JFrame("个人信息");
public static JTextField numberText = new JTextField("请输入学号", 15);
public static JTextField nameText = new JTextField("请输入姓名", 15);
public static JTextField sexText = new JTextField("请输入性别", 15);
public static JTextField ageText = new JTextField("请输入年龄", 15);
public static JPasswordField pwdText2 = new JPasswordField(15);
// 建立输入服务器IP窗体
public static void createIPFrame() {
JPanel panel = new JPanel();
JPanel panel2 = new JPanel();
panel2.setLayout(new GridLayout(2, 1, 80, 8));
JButton ipButton = new JButton(" 连 接 ");
panel2.add(ipText);// 提示信息功能
panel2.add(ipButton);// 添加按钮功能
panel.add(panel2, BorderLayout.CENTER);// 用一个总的JPnel来调用JPnel2的功能,添加到边框中间
ipButton.addActionListener(new ActionListener() {
// 获取主机ip事件监听
@Override
public void actionPerformed(ActionEvent e) {
ipServer = ipText.getText();// 接收所输入的IP地址
ipFrame.dispose();// 属于界面功能的关闭按钮,否则就后退到最初的窗口
createsignInFrame();// 点击按钮进入到另外一个方法
}
});
ipFrame.setContentPane(panel);
ipFrame.setSize(440, 335);
ipFrame.setVisible(true);
}
public static void createsignInFrame() {
// 登陆界面
JPanel panel = new JPanel();
signInFrame.setLayout(new BorderLayout());
panel.add(createCenterLayout(), BorderLayout.CENTER);
signInFrame.setContentPane(panel);
signInFrame.setSize(440, 335);
signInFrame.setVisible(true);
}
public static JPanel createCenterLayout() {
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new BorderLayout());
centerPanel.add(createUserPwdOptionLayout(), BorderLayout.CENTER);
centerPanel.add(createSignUpButtonLayout(), BorderLayout.SOUTH);
return centerPanel;
}
public static JPanel createUserPwdOptionLayout() {
JPanel panelCenter = new JPanel();
panelCenter.setLayout(new BorderLayout());
panelCenter.add(createUserPwdLayout(), BorderLayout.NORTH);
panelCenter.add(createOptionLayout(), BorderLayout.CENTER);
return panelCenter;
}
public static JPanel createUserPwdLayout() {
JPanel userPwdLayout = new JPanel();
userPwdLayout.setLayout(new GridLayout(2, 2, 3, 4));
userPwdLayout.add(createUserLayout());
userPwdLayout.add(createPwdLayout());
return userPwdLayout;
}
public static JPanel createUserLayout() {
JPanel userLayout = new JPanel();
userLayout.setLayout(new BorderLayout());
JLabel singUp = new JLabel(" 注册帐号");
// countText
userLayout.add(countText, BorderLayout.WEST);
userLayout.add(singUp, BorderLayout.CENTER);
return userLayout;
}
public static JPanel createPwdLayout() {
JPanel pwdLayout = new JPanel();
pwdLayout.setLayout(new BorderLayout());
JLabel findPwd = new JLabel(" 找回密码");
pwdLayout.add(pwdText, BorderLayout.WEST);
pwdLayout.add(findPwd, BorderLayout.CENTER);
return pwdLayout;
}
public static JPanel createOptionLayout() {
// 创建选项
JPanel optionLayout = new JPanel();
JCheckBox rememberPwd = new JCheckBox("记住密码", false);
JCheckBox autoSginIn = new JCheckBox("自动登录", true);
optionLayout.setLayout(new GridLayout(1, 2, 7, 2));
optionLayout.add(rememberPwd);
optionLayout.add(autoSginIn);
return optionLayout;
}
public static JPanel createSignUpButtonLayout() {
JPanel buttonLayout = new JPanel();
JButton sginInButton = new JButton(" 登 录 ");
buttonLayout.add(sginInButton);
sginInButton.addActionListener(new ActionListener() {
// 登录事件监听
@Override
public void actionPerformed(ActionEvent e) {
signInFrame.dispose();
createP();
}
});
return buttonLayout;
}
// ---个人信息界面
public static void createP() {
JPanel panel = new JPanel();
P.setLayout(new BorderLayout());
panel.add(createLayout(), BorderLayout.CENTER);
P.setContentPane(panel);
P.setSize(440, 335);
P.setVisible(true);
}
// 总界面布局
private static JPanel createLayout() {
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(creatLieBiao(), BorderLayout.CENTER);
panel.add(createokButton(), BorderLayout.SOUTH);
return createLayout();
}
// 布局列表(总)
private static Component creatLieBiao() {
JPanel userLayout = new JPanel();
userLayout.setLayout(new GridLayout(2, 1, 3, 4));
userLayout.add(createNumberLayout());
userLayout.add(createNameLayout());
userLayout.add(createSexLayout());
userLayout.add(createAge());
userLayout.add(createPassword());
return userLayout;
}
// 学号栏
private static Component createNumberLayout() {
JPanel numberLayout = new JPanel();
numberLayout.setLayout(new BorderLayout());
JLabel number = new JLabel("学号");
// 添加Text窗口
numberLayout.add(numberText, BorderLayout.WEST);
numberLayout.add(number, BorderLayout.CENTER);
return numberLayout;
}
// 姓名栏
private static Component createNameLayout() {
JPanel numberLayout = new JPanel();
numberLayout.setLayout(new BorderLayout());
JLabel number = new JLabel("姓名");
// 添加Text窗口
numberLayout.add(nameText, BorderLayout.WEST);
numberLayout.add(number, BorderLayout.CENTER);
return numberLayout;
}
// 性别栏
private static Component createSexLayout() {
JPanel numberLayout = new JPanel();
numberLayout.setLayout(new BorderLayout());
JLabel number = new JLabel("性别");
// 添加Text窗口
numberLayout.add(sexText, BorderLayout.WEST);
numberLayout.add(number, BorderLayout.CENTER);
return numberLayout;
}
// 年龄栏
private static Component createAge() {
JPanel numberLayout = new JPanel();
numberLayout.setLayout(new BorderLayout());
JLabel number = new JLabel("年龄");
// 添加Text窗口
numberLayout.add(ageText, BorderLayout.WEST);
numberLayout.add(number, BorderLayout.CENTER);
return numberLayout;
}
// 密码栏
private static Component createPassword() {
JPanel numberLayout = new JPanel();
numberLayout.setLayout(new BorderLayout());
JLabel number = new JLabel("密码");
// 添加Text窗口
numberLayout.add(ageText, BorderLayout.WEST);
numberLayout.add(number, BorderLayout.CENTER);
return numberLayout;
}
// 确认按钮(总)
private static JPanel createokButton() {
JPanel buttonLayout = new JPanel();
JButton okButton = new JButton("确认信息并登陆");
buttonLayout.add(okButton);
okButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
P.dispose();
chatHouse();
}
});
return null;
}
public static JFrame chatHouse() {
JFrame chatHouseFrame = new JFrame("QQ聊天室");
JPanel chatMainPanel = new JPanel();
chatMainPanel.setLayout(new BorderLayout());
chatMainPanel.add(craeteChatArea(), BorderLayout.WEST);
// 信息显示区,显示通知,广告,成员信的信息
chatMainPanel.add(createInfoArea(), BorderLayout.CENTER);
chatHouseFrame.setContentPane(chatMainPanel);
chatHouseFrame.setSize(700, 505);
chatHouseFrame.setVisible(true);
return chatHouseFrame;
}
public static JPanel craeteChatArea() {
JPanel chatAreaPanel = new JPanel();
chatAreaPanel.setLayout(new BorderLayout());
chatAreaPanel.add(createRecvArea(), BorderLayout.NORTH);
chatAreaPanel.add(createSendArea(), BorderLayout.CENTER);
chatAreaPanel.add(createSendButtonArea(), BorderLayout.SOUTH);
return chatAreaPanel;
}
// 接收信息显示框
public static JTextArea recviveArea = new JTextArea(20, 40);
// 发送信息显示框
public static JTextArea sendArea = new JTextArea(5, 40);
// 聊天成员信息框
public static JTextArea infoOfMemberText = new JTextArea(2, 2);
public static JPanel createRecvArea() {
JPanel recviveAreaPanel = new JPanel();
JScrollPane recviveScroll = new JScrollPane(recviveArea);
// 接收信息框竖直滚动条自动出现
recviveScroll
.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
recviveArea.setEditable(false);
recviveArea.setLineWrap(true); // 超出显示长度自动换行
recviveAreaPanel.add(recviveScroll);
return recviveAreaPanel;
}
public static JPanel createSendArea() {
JPanel sendAreaPanel = new JPanel();
JScrollPane sendScroll = new JScrollPane(sendArea);
// 接收信息框竖直滚动条自动出现
sendScroll
.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
sendArea.setLineWrap(true); // 超出显示长度自动换行
sendAreaPanel.setLayout(new BorderLayout());
sendAreaPanel.add(sendScroll, BorderLayout.SOUTH);
return sendAreaPanel;
}
public static JPanel createSendButtonArea() {
JPanel SendButtonAreaPanel = new JPanel();
SendButtonAreaPanel.setLayout(new BorderLayout());
// 这一句对应于QQ聊天界面的小广告条
SendButtonAreaPanel.add(craetSendButton(), BorderLayout.EAST);
return SendButtonAreaPanel;
}
public static JPanel craetSendButton() {
// 聊天区关闭和发送按钮面板
JPanel buttonPanel = new JPanel();
JButton close = new JButton("关闭(C)");
JButton send = new JButton("发送(S)");
buttonPanel.add(close);
buttonPanel.add(send);
send.addActionListener(new ActionListener() {
// 发送事件监听
@Override
public void actionPerformed(ActionEvent e) {
sendArea.setText(null);
}
});
close.addActionListener(new ActionListener() {
// 关闭事件监听
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
return buttonPanel;
}
public static JPanel createInfoArea() {
JPanel infoPanel = new JPanel();
infoPanel.setLayout(new GridLayout(2, 1));
infoPanel.add(createInfoOfMemberArea());
return infoPanel;
}
public static JPanel createInfoOfMemberArea() {
JPanel infoOfMemberPanel = new JPanel();
infoOfMemberPanel.setLayout(new BorderLayout());
JScrollPane infoScroll = new JScrollPane(infoOfMemberText);
infoScroll
.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
infoOfMemberText.setEditable(false);
infoOfMemberPanel.add(new JLabel("聊天成员"), BorderLayout.NORTH);
infoOfMemberPanel.add(infoScroll);
return infoOfMemberPanel;
}
public static void main(String[] args) {
// 输入服务器ip窗体
createIPFrame();
}
}
添加回答
举报
0/150
提交
取消