weixin_42299858 2009-05-23 19:27
浏览 342
已采纳

用java做聊天窗口的问题....

做了一个聊天窗口,可是只要一设置jTextArea的字体就运行不出来啦..请帮忙找找错误...谢谢..
package pack_Chat;

import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import java.awt.Font;

import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JScrollPane;

import java.awt.Dimension;
import javax.swing.JTextArea;
import java.awt.Rectangle;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.sql.Date;
import java.text.DateFormat;
import java.util.*;
import java.text.* ;
import javax.swing.JButton;
import javax.xml.crypto.Data;
import javax.swing.JLabel;
import java.awt.Color;
import javax.swing.JComboBox;
import java.awt.List;

public class Chat extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JTextArea jTextArea2 = null;
private JButton jButton1 = null;
private JLabel jLabel1 = null;
private JComboBox jComboBox1 = null;
private JLabel jLabel2 = null;
private JComboBox jComboBox2 = null;
private JLabel jLabel3 = null;
private JComboBox jComboBox3 = null;
public Chat() {
super();
initialize();

}
private void initialize() {
this.setSize(375, 364);
this.setContentPane(getJContentPane());
this.setTitle("聊天室");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}
private JPanel getJContentPane() {
    if (jContentPane == null) {
        jLabel3 = new JLabel();
        jLabel3.setBounds(new Rectangle(159, 220, 28, 31));
        jLabel3.setForeground(new Color(0, 51, 204));
        jLabel3.setText("大小");
        jLabel2 = new JLabel();
        jLabel2.setBounds(new Rectangle(81, 220, 26, 32));
        jLabel2.setForeground(new Color(0, 51, 204));
        jLabel2.setText("样式");
        jLabel1 = new JLabel();
        jLabel1.setBounds(new Rectangle(2, 220, 26, 32));
        jLabel1.setForeground(new Color(0, 51, 204));
        jLabel1.setText("字体");
        jContentPane = new JPanel();
        jContentPane.setLayout(null);
        jContentPane.add(getJTextArea2(), null);
        jContentPane.add(getJButton1(), null);
        jContentPane.add(jLabel1, null);
        jContentPane.add(getJComboBox1(), null);
        jContentPane.add(jLabel2, null);
        jContentPane.add(getJComboBox2(), null);
        jContentPane.add(jLabel3, null);
        jContentPane.add(getJComboBox3(), null);
        jContentPane.add(getJScrollPane1(), null);
    }
    return jContentPane;
}
private JTextArea getJTextArea2() {
    if (jTextArea2 == null) {
        jTextArea2 = new JTextArea();
        jTextArea2.setBounds(new Rectangle(1, 253, 282, 73));
        jTextArea2.setLineWrap(true);
        jTextArea2.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent e) {
                if(e.getKeyChar()==KeyEvent.VK_ENTER){
                    String s="你说:"+jTextArea2.getText();
                     String date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
                     jTextArea1.append(date+"\n");
                     jTextArea1.append(s);  
                     jTextArea2.setText("");
            }
            }
        });
    }
    return jTextArea2;
}
private JButton getJButton1() {
    if (jButton1 == null) {
        jButton1 = new JButton();
        jButton1.setBounds(new Rectangle(293, 288, 62, 32));
        jButton1.setText("发送");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {
                 String s="你说:"+jTextArea2.getText();
                 String date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
                 jTextArea1.append(date+"\n");
                 jTextArea1.append(s+"\n");
                 jTextArea2.setText("");

            }
        });
    }
    return jButton1;
}

String str = "宋体"; // @jve:decl-index=0:
private JComboBox getJComboBox1() {
if (jComboBox1 == null) {
jComboBox1 = new JComboBox();
jComboBox1.setBounds(new Rectangle(28, 220, 53, 31));
jComboBox1.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent e) {
if(e.getItem().toString()=="宋体")
str="宋体";
if(e.getItem().toString()=="隶书")
str="隶书";
if(e.getItem().toString()=="黑体")
str="黑体";
jTextArea2.setFont(new Font(str,ss,DX));
jTextArea1.setFont(new Font(str,ss,DX));//只要加上这句就运行不出来啦(还有两句相同的在设置文字样式和大小那)
}
});
jComboBox1.addItem("宋体");
jComboBox1.addItem("隶书");
jComboBox1.addItem("黑体");
}
return jComboBox1;
}
int ss=Font.PLAIN;
private JComboBox getJComboBox2() {
if (jComboBox2 == null) {
jComboBox2 = new JComboBox();
jComboBox2.setBounds(new Rectangle(106, 220, 53, 31));
jComboBox2.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent e) {
if(e.getItem().toString()=="默认")
ss=Font.PLAIN;
if(e.getItem().toString()=="粗体")
ss=Font.BOLD;
if(e.getItem().toString()=="斜体")
ss=Font.ITALIC;
jTextArea2.setFont(new Font(str,ss,DX));
jTextArea1.setFont(new Font(str,ss,DX));
}
});
jComboBox2.addItem("默认");
jComboBox2.addItem("粗体");
jComboBox2.addItem("斜体");
}
return jComboBox2;
}
int DX=12;
private JScrollPane jScrollPane1 = null;
private JTextArea jTextArea1 = null;
private JComboBox getJComboBox3() {
if (jComboBox3 == null) {
jComboBox3 = new JComboBox();
jComboBox3.setBounds(new Rectangle(187, 220, 53, 31));
jComboBox3.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent e) {

if(e.getItem().toString()=="12")
DX=12;
if(e.getItem().toString()=="24")
DX=24;
if(e.getItem().toString()=="36")
DX=36;
jTextArea2.setFont(new Font(str,ss,DX));
jTextArea1.setFont(new Font(str,ss,DX));

            }
        });
        jComboBox3.addItem("12");
        jComboBox3.addItem("24");
        jComboBox3.addItem("36");
    }
    return jComboBox3;
}

/**
 * This method initializes jScrollPane1 
 *  
 * @return javax.swing.JScrollPane  
 */
private JScrollPane getJScrollPane1() {
    if (jScrollPane1 == null) {
        jScrollPane1 = new JScrollPane();
        jScrollPane1.setBounds(new Rectangle(0, 0, 360, 220));
        jScrollPane1.setViewportView(getJTextArea1());
    }
    return jScrollPane1;
}
/**
 * This method initializes jTextArea1   
 *  
 * @return javax.swing.JTextArea    
 */
private JTextArea getJTextArea1() {
    if (jTextArea1 == null) {
        jTextArea1 = new JTextArea();
        jTextArea1.setEditable(false);
        jTextArea1.setLineWrap(true);
        //jTextArea1.setFont(new Font(str,ss,DX));
        //jTextArea1.setFont(new Font("宋体",Font.BOLD,40));
    }
    return jTextArea1;
}
public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            Chat thisClass=new Chat();
            thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            thisClass.setVisible(true);
        }
    });
}

}

下面是错误提示

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at pack_Chat.Chat$3.itemStateChanged(Chat.java:130)
at javax.swing.JComboBox.fireItemStateChanged(Unknown Source)
at javax.swing.JComboBox.selectedItemChanged(Unknown Source)
at javax.swing.JComboBox.contentsChanged(Unknown Source)
at javax.swing.AbstractListModel.fireContentsChanged(Unknown Source)
at javax.swing.DefaultComboBoxModel.setSelectedItem(Unknown Source)
at javax.swing.DefaultComboBoxModel.addElement(Unknown Source)
at javax.swing.JComboBox.addItem(Unknown Source)
at pack_Chat.Chat.getJComboBox1(Chat.java:133)
at pack_Chat.Chat.getJContentPane(Chat.java:69)
at pack_Chat.Chat.initialize(Chat.java:45)
at pack_Chat.Chat.(Chat.java:41)
at pack_Chat.Chat$6.run(Chat.java:220)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

  • 写回答

1条回答 默认 最新

  • arniu2006 2009-05-24 12:11
    关注

    试了一下,居然找出原因了。
    这段代码主要有两个问题:
    1.jTextArea1自始至终没有定义,所以有Unknown Source的错误提示,一种简单的解决办法是修改initialize函数如下
    private void initialize() {
    this.setSize(375, 364);
    [b]jTextArea1 = getJTextArea1();[/b]
    this.setContentPane(getJContentPane());
    this.setTitle("聊天室");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    仅仅添加粗体的那行。由于Java默认为定义对象为null,所以调用函数getJTextArea1()进行初始化。
    2.第一行package不对头,当可是执行时,JVM会在当前目录下寻找pack_Chat目录下定义的Chat,这时显然是在找不到的,当前目录就是pack_Chat。你可以把第一行删掉。

    你可以试一下。

    //package pack_Chat;

    import javax.swing.SwingUtilities;
    import java.awt.BorderLayout;
    import java.awt.Font;

    import javax.swing.JPanel;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;

    import java.awt.Dimension;
    import javax.swing.JTextArea;
    import java.awt.Rectangle;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    import java.awt.event.KeyEvent;
    import java.sql.Date;
    import java.text.DateFormat;
    import java.util.*;
    import java.text.* ;
    import javax.swing.JButton;
    import javax.xml.crypto.Data;
    import javax.swing.JLabel;
    import java.awt.Color;
    import javax.swing.JComboBox;
    import java.awt.List;

    public class iChat extends JFrame {
    private static final long serialVersionUID = 1L;
    private JPanel jContentPane = null;
    private JTextArea jTextArea2 = null;
    private JButton jButton1 = null;
    private JLabel jLabel1 = null;
    private JComboBox jComboBox1 = null;
    private JLabel jLabel2 = null;
    private JComboBox jComboBox2 = null;
    private JLabel jLabel3 = null;
    private JComboBox jComboBox3 = null;
    public iChat() {
    super();
    initialize();
    }
    private void initialize() {
    this.setSize(375, 364);
    jTextArea1 = getJTextArea1();
    this.setContentPane(getJContentPane());
    this.setTitle("聊天室");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
    private JPanel getJContentPane() {
    if (jContentPane == null) {
    jLabel3 = new JLabel();
    jLabel3.setBounds(new Rectangle(159, 220, 28, 31));
    jLabel3.setForeground(new Color(0, 51, 204));
    jLabel3.setText("大小");
    jLabel2 = new JLabel();
    jLabel2.setBounds(new Rectangle(81, 220, 26, 32));
    jLabel2.setForeground(new Color(0, 51, 204));
    jLabel2.setText("样式");
    jLabel1 = new JLabel();
    jLabel1.setBounds(new Rectangle(2, 220, 26, 32));
    jLabel1.setForeground(new Color(0, 51, 204));
    jLabel1.setText("字体");
    jContentPane = new JPanel();
    jContentPane.setLayout(null);
    jContentPane.add(getJTextArea2(), null);
    jContentPane.add(getJButton1(), null);
    jContentPane.add(jLabel1, null);
    jContentPane.add(getJComboBox1(), null);
    jContentPane.add(jLabel2, null);
    jContentPane.add(getJComboBox2(), null);
    jContentPane.add(jLabel3, null);
    jContentPane.add(getJComboBox3(), null);
    jContentPane.add(getJScrollPane1(), null);
    }
    return jContentPane;
    }
    private JTextArea getJTextArea2() {
    if (jTextArea2 == null) {
    jTextArea2 = new JTextArea();
    jTextArea2.setBounds(new Rectangle(1, 253, 282, 73));
    jTextArea2.setLineWrap(true);
    jTextArea2.addKeyListener(new java.awt.event.KeyAdapter() {
    public void keyTyped(java.awt.event.KeyEvent e) {
    if(e.getKeyChar()==KeyEvent.VK_ENTER){
    String s="你说:"+jTextArea2.getText();
    String date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
    jTextArea1.append(date+"\n");
    jTextArea1.append(s);

    jTextArea2.setText("");
    }
    }
    });
    }
    return jTextArea2;
    }
    private JButton getJButton1() {
    if (jButton1 == null) {
    jButton1 = new JButton();
    jButton1.setBounds(new Rectangle(293, 288, 62, 32));
    jButton1.setText("发送");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent e) {
    String s="你说:"+jTextArea2.getText();
    String date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
    jTextArea1.append(date+"\n");
    jTextArea1.append(s+"\n");
    jTextArea2.setText("");

    }
    });
    }
    return jButton1;
    }
    String str = "宋体"; // @jve:decl-index=0:
    private JComboBox getJComboBox1() {
    if (jComboBox1 == null) {
    jComboBox1 = new JComboBox();
    jComboBox1.setBounds(new Rectangle(28, 220, 53, 31));
    jComboBox1.addItemListener(new java.awt.event.ItemListener() {
    public void itemStateChanged(java.awt.event.ItemEvent e) {
    if(e.getItem().toString()=="宋体")
    str="宋体";
    if(e.getItem().toString()=="隶书")
    str="隶书";
    if(e.getItem().toString()=="黑体")
    str="黑体";
    jTextArea2.setFont(new Font(str,ss,DX));
    jTextArea1.setFont(new Font(str,ss,DX));//只要加上这句就运行不出来啦(还有两句相同的在设置文字样式和大小那)
    }
    });
    jComboBox1.addItem("宋体");
    jComboBox1.addItem("隶书");
    jComboBox1.addItem("黑体");
    }
    return jComboBox1;
    }
    int ss=Font.PLAIN;
    private JComboBox getJComboBox2() {
    if (jComboBox2 == null) {
    jComboBox2 = new JComboBox();
    jComboBox2.setBounds(new Rectangle(106, 220, 53, 31));
    jComboBox2.addItemListener(new java.awt.event.ItemListener() {
    public void itemStateChanged(java.awt.event.ItemEvent e) {
    if(e.getItem().toString()=="默认")
    ss=Font.PLAIN;
    if(e.getItem().toString()=="粗体")
    ss=Font.BOLD;
    if(e.getItem().toString()=="斜体")
    ss=Font.ITALIC;
    jTextArea2.setFont(new Font(str,ss,DX));
    jTextArea1.setFont(new Font(str,ss,DX));
    }
    });
    jComboBox2.addItem("默认");
    jComboBox2.addItem("粗体");
    jComboBox2.addItem("斜体");
    }
    return jComboBox2;
    }
    int DX=12;
    private JScrollPane jScrollPane1 = null;
    private JTextArea jTextArea1 = null;
    private JComboBox getJComboBox3() {
    if (jComboBox3 == null) {
    jComboBox3 = new JComboBox();
    jComboBox3.setBounds(new Rectangle(187, 220, 53, 31));
    jComboBox3.addItemListener(new java.awt.event.ItemListener() {
    public void itemStateChanged(java.awt.event.ItemEvent e) {
    if(e.getItem().toString()=="12")
    DX=12;
    if(e.getItem().toString()=="24")
    DX=24;
    if(e.getItem().toString()=="36")
    DX=36;
    jTextArea2.setFont(new Font(str,ss,DX));
    jTextArea1.setFont(new Font(str,ss,DX));

    }
    });
    jComboBox3.addItem("12");
    jComboBox3.addItem("24");
    jComboBox3.addItem("36");
    }
    return jComboBox3;
    }

    /**

    • This method initializes jScrollPane1
    • @return javax.swing.JScrollPane / private JScrollPane getJScrollPane1() { if (jScrollPane1 == null) { jScrollPane1 = new JScrollPane(); jScrollPane1.setBounds(new Rectangle(0, 0, 360, 220)); jScrollPane1.setViewportView(getJTextArea1()); } return jScrollPane1; } /*
    • This method initializes jTextArea1
    • @return javax.swing.JTextArea */ private JTextArea getJTextArea1() { if (jTextArea1 == null) { jTextArea1 = new JTextArea(); jTextArea1.setEditable(false); jTextArea1.setLineWrap(true); //jTextArea1.setFont(new Font(str,ss,DX)); //jTextArea1.setFont(new Font("宋体",Font.BOLD,40)); } return jTextArea1; } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { iChat thisClass=new iChat(); thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); thisClass.setVisible(true); } }); }

    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog