源代码:
- package com.frame;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class TextAreaFrame1 extends JFrame{
- private static final long serialVersionUID = 1L;
-
- Toolkit tk = Toolkit.getDefaultToolkit();
- Dimension d = tk.getScreenSize();
-
- public TextAreaFrame1(){
- setTitle("发送--接收");
- setSize(500,400);
- setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
- centerWindow(this);
- JPanel panel = new TextAreaPanel();
- this.add(panel);
- }
-
- private void centerWindow(JFrame frame){
- int centerWidth = ((int)d.getWidth() - frame.getWidth())/2;
- int centerHeight = ((int)d.getHeight() - frame.getHeight())/2;
- setLocation(centerWidth, centerHeight);
- }
- public static void main(String[] args) {
- JFrame frame = new TextAreaFrame1();
- frame.setVisible(true);
- }
- }
- class TextAreaPanel extends JPanel implements ActionListener {
- private static final long serialVersionUID = 1L;
- final int vScroll = JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED;//纵向滚动条设置为,需要时出现
- final int hScroll = JScrollPane.HORIZONTAL_SCROLLBAR_NEVER;//横向滚动条设置为,永远不出现
- private JTextArea summaryArea,sendArea;//定义两个文本区域,一个是显示所有信息,一个是用于输入发送信息
- private JButton submitBt,exitBt;//定义两个按钮,一个是发送,一个是退出
- private JScrollPane summaryScroll,sendScroll;//定义滚动条,分别是两个文本域的滚动条
- // private String summaryText,sendText;
-
- public TextAreaPanel(){
- //定义显示所有消息的文本域
- summaryArea = new JTextArea(6,40);
- summaryArea.setWrapStyleWord(true);
- summaryArea.setLineWrap(true);
- summaryArea.setEditable(false);
- //定义发送消息的文本域
- sendArea = new JTextArea(3,40);
- sendArea.setWrapStyleWord(true);
- sendArea.setLineWrap(true);
- //定义滚动条
- summaryScroll = new JScrollPane(summaryArea,vScroll,hScroll);
- sendScroll = new JScrollPane(sendArea,vScroll,hScroll);
- //定义两个按钮
- submitBt = new JButton("发送");
- exitBt = new JButton("退出");
- //将所有元素添加到panel中
- add(summaryArea);
- add(summaryScroll);
- add(sendArea);
- add(sendScroll);
- add(submitBt);
- add(exitBt);
- //监听两个按钮
- submitBt.addActionListener(this);
- exitBt.addActionListener(this);
- }
-
- /**
- * 监听,处理事件
- */
- public void actionPerformed(ActionEvent e) {
- Object source = e.getSource();
- if(source == submitBt){
- String summaryText = summaryArea.getText();
- String sendText = sendArea.getText();
- if(!sendText.equals("") && sendText != null){
- summaryText = summaryText + sendText + "\n";
- summaryArea.setText(summaryText);
- }
- else{
- JOptionPane.showMessageDialog(null, "不能发送空消息!");
- }
- sendArea.setText("");
- }
- if(source == exitBt){
- System.exit(0);
- }
- }
-
- }
复制代码
异常信息:
- Exception in thread "main" java.lang.NullPointerException
- at javax.swing.ScrollPaneLayout.preferredLayoutSize(ScrollPaneLayout.java:495)
- at java.awt.Container.preferredSize(Container.java:1670)
- at java.awt.Container.getPreferredSize(Container.java:1655)
- at javax.swing.JComponent.getPreferredSize(JComponent.java:1646)
- at java.awt.FlowLayout.layoutContainer(FlowLayout.java:610)
- at java.awt.Container.layout(Container.java:1486)
- at java.awt.Container.doLayout(Container.java:1475)
- at java.awt.Container.validateTree(Container.java:1573)
- at java.awt.Container.validateTree(Container.java:1580)
- at java.awt.Container.validateTree(Container.java:1580)
- at java.awt.Container.validateTree(Container.java:1580)
- at java.awt.Container.validateTree(Container.java:1580)
- at java.awt.Container.validate(Container.java:1545)
- at java.awt.Window.show(Window.java:890)
- at java.awt.Component.show(Component.java:1456)
- at java.awt.Component.setVisible(Component.java:1408)
- at java.awt.Window.setVisible(Window.java:871)
- at com.frame.TextAreaFrame1.main(TextAreaFrame1.java:30)
- Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
- at javax.swing.ScrollPaneLayout.preferredLayoutSize(ScrollPaneLayout.java:495)
- at java.awt.Container.preferredSize(Container.java:1670)
- at java.awt.Container.getPreferredSize(Container.java:1655)
- at javax.swing.JComponent.getPreferredSize(JComponent.java:1646)
- at java.awt.FlowLayout.layoutContainer(FlowLayout.java:610)
- at java.awt.Container.layout(Container.java:1486)
- at java.awt.Container.doLayout(Container.java:1475)
- at java.awt.Container.validateTree(Container.java:1573)
- at java.awt.Container.validateTree(Container.java:1580)
- at java.awt.Container.validateTree(Container.java:1580)
- at java.awt.Container.validateTree(Container.java:1580)
- at java.awt.Container.validateTree(Container.java:1580)
- at java.awt.Container.validate(Container.java:1545)
- at java.awt.Window.dispatchEventImpl(Window.java:2553)
- at java.awt.Component.dispatchEvent(Component.java:4287)
- at java.awt.EventQueue.dispatchEvent(EventQueue.java:605)
- at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:276)
- at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:201)
- at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:191)
- at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:186)
- at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:178)
- at java.awt.EventDispatchThread.run(EventDispatchThread.java:139)
复制代码 |