帮忙看下代码package com;
import java.awt.*;
import java.awt.event.*;import javax.swing.*;
public class Client{
JFrame frame = new JFrame("聊天室");
private JPanel landing = new JPanel();
private JPanel chatpanel = new JPanel();
private JTextArea content = new JTextArea();
private JTextArea edit = new JTextArea(); 
JButton loading = new JButton("登陆");
public  JTextField info = new JTextField(20);
public Client(){
frame.setSize(400,500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private void load(){
landing.add(new JLabel("请输入你的姓名"));
landing.add(info);
landing.add(loading);
frame.add(landing,BorderLayout.CENTER);
//作用是想点击“登陆”按钮后把之前那个landing面板给删了。然后显示为content面板中的内容
loading.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
frame.remove(landing);
content.add(content, BorderLayout.NORTH);
content.add(edit,BorderLayout.SOUTH);
frame.add(content);

}
});
}
public static void main(String[] args) {
Client xinming = new Client();
xinming.load();
}
}
但是当程序运行时点击“登陆”按钮,并不能把面板给换了。而且程序卡死。求帮忙解释下。。

解决方案 »

  1.   

    这个能换,你要自己设定JTextArea的大下、import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class Client {
    JFrame frame = new JFrame("聊天室");
    private JPanel landing = new JPanel();
    private JPanel chatpanel = new JPanel();
    private JTextArea content = new JTextArea();
    private JTextArea edit = new JTextArea();
    JButton loading = new JButton("登陆");
    public JTextField info = new JTextField(20); public Client() {
    load();
    frame.setSize(400, 500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    }
    private void load() {
    landing.add(new JLabel("请输入你的姓名"));
    landing.add(info);
    landing.add(loading);
    frame.add(landing, BorderLayout.NORTH);
    chatpanel.setLayout(new BorderLayout());
    chatpanel.add(content, BorderLayout.NORTH);
    chatpanel.add(edit, BorderLayout.SOUTH);

    // 作用是想点击“登陆”按钮后把之前那个landing面板给删了。然后显示为content面板中的内容
    loading.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    loading.setEnabled(false);
    landing.setVisible(false);
    frame.add(chatpanel,BorderLayout.CENTER);
    frame.validate();
    }
    });
    } public static void main(String[] args) {
    Client xinming = new Client();
    }
    }