小弟做毕业设计,从网上当的代码;是awt的,改成swing就出错了UserPad不能正确显示
耽误您几分钟,看看代码 调试一下,把错误告知小弟,
顺便改正 
小第不胜感激!!!!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ChessFrame extends JFrame  {
   UserPad userpad = new UserPad();// 显示客户端名称的面板
ChatPad chatpad = new ChatPad(); // 聊天面板
ControlPad controlpad = new ControlPad(); // 控制面板
ChessPad chesspad = new ChessPad(this); // 下棋 面板
InputPad inputpad = new InputPad();  //  修改名字,聊天输人 JPanel leftPanel; 
JPanel rightPanel;
JPanel buttonPanel; public ChessFrame() {

super("Java五子棋客户端   ");
Container contentPane = this.getContentPane();
setBackground(Color.blue);
setSize(670, 548);

// 设置 客户端界面出现位置
Dimension size = this.getSize();
Dimension scrsize = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation((scrsize.width - size.width)/2,
                 (scrsize.height - size.height)/2 );
    
    leftPanel = new JPanel();
    leftPanel.setLayout(new GridLayout(2,1));
    leftPanel.setPreferredSize(new Dimension(229,441));
    
    rightPanel = new JPanel();
    rightPanel.setLayout(new BorderLayout());
    rightPanel.setPreferredSize(new Dimension(441,441));
    
leftPanel.add(userpad);
leftPanel.add(chatpad);
leftPanel.setBackground(Color.green); rightPanel.add(chesspad, BorderLayout.CENTER);
rightPanel.add(inputpad, BorderLayout.SOUTH);
rightPanel.setBackground(Color.green);
controlpad.creatGameButton.setEnabled(false);
controlpad.joinGameButton.setEnabled(false);
controlpad.cancelGameButton.setEnabled(false);

buttonPanel = new JPanel(new BorderLayout());
buttonPanel.add(controlpad, BorderLayout.CENTER);
buttonPanel.setBackground(Color.green); addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

contentPane.add(leftPanel, BorderLayout.WEST);
contentPane.add(rightPanel, BorderLayout.CENTER);
contentPane.add(buttonPanel, BorderLayout.SOUTH); pack();
setVisible(true);
setResizable(false);
} public static void main(String args[]) {
ChessFrame chessClient = new ChessFrame();
}
}
//*******************************end of ChessFrame.javaimport java.awt.*;
import javax.swing.*;
import java.awt.Dimension;import javax.swing.border.TitledBorder;
import javax.swing.JScrollPane;class UserPad extends JPanel {

java.awt.List userList = new java.awt.List(10);
//userList.setSize(220,210);

  JScrollPane pane = new JScrollPane(userList);
public UserPad() {
super();
setSize(new Dimension(229,220));
add(pane,BorderLayout.CENTER);

for (int i = 0; i < 100; i++) {
userList.add(i + "." + "没有用户");
}

//userList.add("没有用户");
//userList.add("请连接服务器");
}
}class ChatPad extends JPanel {
JTextArea chatLineArea = new JTextArea("", 15, 30);
ChatPad() {
setSize(new Dimension(229,220));
setLayout(new BorderLayout());
add(chatLineArea, BorderLayout.CENTER);
}}class ControlPad extends JPanel {
JLabel IPlabel = new JLabel("IP", JLabel.LEFT);
JTextField inputIP = new JTextField("localhost", 10);
JButton connectButton = new JButton("连接主机");
JButton creatGameButton = new JButton("建立游戏");
JButton joinGameButton = new JButton("加入游戏");
JButton cancelGameButton = new JButton("放弃游戏");
JButton exitGameButton = new JButton("关闭程序"); ControlPad() {
setLayout(new FlowLayout(FlowLayout.LEFT));
setBackground(Color.green); add(IPlabel);
add(inputIP);
add(connectButton);
add(creatGameButton);
add(joinGameButton);
add(cancelGameButton);
add(exitGameButton);
}}class InputPad extends JPanel {
JTextField inputWords = new JTextField("", 40);
Choice userChoice = new Choice(); InputPad() {
setBackground(Color.green);
setLayout(new FlowLayout(FlowLayout.LEFT));
for (int i = 0; i < 100; i++) {
userChoice.addItem(i + "." + "没有用户");
}
userChoice.setSize(60, 30);
add(userChoice);
add(inputWords);
}
}
// end of UserPad.java 以上几个类在一个文件里import java.awt.*;
import java.util.*;
import javax.swing.*;public class ChessPad extends JPanel {

ChessFrame fcFrame; JTextField statusText = new JTextField("请先连接服务器"); public ChessPad(ChessFrame fcFrame) {

this.fcFrame = fcFrame;
        
setSize(new Dimension(440,440));
setLayout(new BorderLayout());

setBackground(Color.green);

statusText.setVisible(true);
add(statusText,BorderLayout.NORTH);

statusText.setBounds(40, 5, 360, 24);
statusText.setEditable(false);
setVisible(true);
  }
  
  public void paint(Graphics g) {
for (int i = 40; i <= 380; i = i + 20) {
g.drawLine(40, i, 400, i);
}
g.drawLine(40, 400, 400, 400);
for (int j = 40; j <= 380; j = j + 20) {
g.drawLine(j, 40, j, 400);
}
g.drawLine(400, 40, 400, 400);
g.fillOval(97, 97, 6, 6);
g.fillOval(337, 97, 6, 6);
g.fillOval(97, 337, 6, 6);
g.fillOval(337, 337, 6, 6);
g.fillOval(217, 217, 6, 6);
}}

解决方案 »

  1.   

    JPanel默认使用FlowLayout,因此在UserPad中使用add(pane,BorderLayout.CENTER);是不对的。奇怪的是你在其他几个pad中都使用了setLayout,唯独在UserPad中没有调用,问题估计就出在这里了。在UserPad的构造方法中添加setLayout(new BorderLayout());语句(当然得在add方法之前),应该就能解决问题。我没有调你的代码,再有问题,贴出来再看。
      

  2.   

    先跟大家说声对不起!!没能及时回复各位。
        今天学校停电,刚刚来电。
    听了3楼大哥的,我在UserPad的构造方法中添加setLayout(new BorderLayout());后,UserPad能正确显示了,还存在的问题:
    InputPad中的JTextField inputWords 不显示
    ChessPad中的JTextField statusText 不显示,点击后才显示
      

  3.   

    inputWords不显示是因为JTextField设定为40列,太长,无法显示,如果原来的语句改成
    JTextField inputWords = new JTextField("InputPad"); 
    这时就可以看到这个JTextField了。
    statusText不能显示,估计是你覆盖的方法不对,凡是继承JComponent的组件,要绘图都是覆盖paintComponent方法,而不是paint方法,你去覆盖paint方法就有不可预知的结果。AWT与Swing的区别有很多在这里。
    还有的问题是,你大量使用BorderLayout,FlowLayout,却不知道使用GridBagLayout,实在是个缺陷,你的ChatPad与周围的大小与不匹配,你在里面多敲回车可以试试。建议学习比较复杂,但更强大的GridBagLayout。