我修改了代码,想把所有的类都写到主类里,但改后main方法里就报错!
/*
 * 聊天窗口主方法
 */
package chatinterface;import java.io.*;import javax.swing.*;import java.awt.*;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.*;public class ChatUI {
class frame extends JFrame {
public frame() {
display pan1 = new display();
Container c = getContentPane();
c.add(pan1);
}
} class display extends JPanel {
JTextArea sentarea; public display() {
sentarea = new JTextArea(1, 20); // 发送消息栏
sentarea.setFont(new Font("PMingLiU", Font.PLAIN, 12));
sentarea.setBackground(new Color(255, 250, 240));
sentarea.setLineWrap(true);
sentarea.setBounds(5, 232, 280, 38); JLabel user = new JLabel("昵称"); // 昵称标签
user.setBounds(5, 275, 36, 20); // 将组件添加到面板 add(sentarea); } } public static void main(String args[]) {
frame fr = new frame();
fr.show(); // 设置窗口可显示
fr.setSize(500, 400); // 窗口大小
fr.setDefaultCloseOperation(fr.EXIT_ON_CLOSE); // 窗口最小化,关闭功能
fr.setResizable(false); // 是否窗口可调整大小(false)
fr.setTitle("ChatUI"); // 窗口标题
fr.setBounds(300, 200, 500, 400); // 窗口初始坐标,初始大小
}
}

解决方案 »

  1.   

    在静态(static)域内是无法访问非静态变量、方法的。具体原因看看一些基础书吧
    而且楼主最好按照java规定的类命名方法即首字母大写如display应为Display
    我在你的基础下改了,可以运行,你在看看吧:package   chatinterface; 
    import   java.io.*; import   javax.swing.*; import   java.awt.*; 
    import   java.awt.Font; 
    import   java.awt.event.ActionEvent; 
    import   java.awt.event.ActionListener; 
    import   java.net.*; public   class   ChatUI   { 
    public ChatUI(){
    frame   fr   =   new   frame(); 
    fr.setSize(500,   400);   //   窗口大小 
    fr.setDefaultCloseOperation(fr.EXIT_ON_CLOSE);   //   窗口最小化,关闭功能 
    fr.setResizable(false);   //   是否窗口可调整大小(false) 
    fr.setTitle("ChatUI");   //   窗口标题 
    fr.setBounds(300,   200,   500,   400);   //   窗口初始坐标,初始大小 
    fr.show();   //   设置窗口可显示 
    }
    class   frame   extends   JFrame   { 
    public   frame()   { 
    display   pan1   =   new   display(); 
    Container   c   =   getContentPane(); 
    c.add(pan1); 

    }  class   display   extends   JPanel   { 
    JTextArea   sentarea;  public   display()   { 
    sentarea   =   new   JTextArea(1,   20);   //   发送消息栏 
    sentarea.setFont(new   Font("PMingLiU",   Font.PLAIN,   12)); 
    sentarea.setBackground(new   Color(255,   250,   240)); 
    sentarea.setLineWrap(true); 
    sentarea.setBounds(5,   232,   280,   38);  JLabel   user   =   new   JLabel("昵称");   //   昵称标签 
    user.setBounds(5,   275,   36,   20);  //   将组件添加到面板  add(sentarea);  }  } public   static   void   main(String   args[])   { 
    new ChatUI(); 

    }
      

  2.   

    frame是内部类 这样初始化
    new ChatUI().new frame();