JtextField显示无法正常工作

时间:2013-12-06 17:56:30

标签: java

大家晚上,

所以我试图使用Javaspace创建一个简单的主题板,我似乎遇到了我的JTextFields,最初AddTopic和AddMessage按钮将信息发送到相应的JTextField(尽管只需要不断更新) ,现在他们只是崩溃java应用程序并冻结它,如果有人可以查看“addMessage,AddTopic,Processtopic和ProcessMessage函数,这将是非常好的。

感谢。

史密斯先生

import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.JButton;

import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JTextArea;

import net.jini.core.lease.Lease;
import net.jini.space.*;
import javax.swing.*;

@SuppressWarnings("serial")
public class MainInterface extends  javax.swing.JFrame{
//Variable names
    private JavaSpace space;
    private JPanel jPanel1;
private JTextArea MessageList1;
    private JTextField MessageSenderField;
private JScrollBar TopicScrollBar, MessageScrollBar;
private JTextField topicauthorfield;
private JButton RefreshMessages1;
private JButton Refreshtopic1;
private JTextField MessageAdderField1;
    private JTextField TopicAdderField;
private JButton AddMessage1;
    private JButton AddTopic1;
private JTextArea TopicList1;

public MainInterface() {
    space = SpaceUtils.getSpace();
    if (space == null){
        System.err.println("Failed to find the javaspace");
        System.exit(1);}
    {
//The Names of methods to be found further in the program.
        MainComponents ();
        pack ();
        setVisible(true);
        processtopics();
        processmessages();

    }}


private void MainComponents () {
    setTitle ("Topic Board");
    addWindowListener (new java.awt.event.WindowAdapter () {
        public void windowClosing (java.awt.event.WindowEvent evt) {
            System.exit(0);
        }
    }   );

    Container cp = getContentPane();
    cp.setLayout (new BorderLayout ());

    jPanel1 = new JPanel();
    jPanel1.setLayout(null);
    jPanel1.setPreferredSize(new java.awt.Dimension(997, 527));

    TopicList1 = new JTextArea();
    jPanel1.add(TopicList1);
    TopicList1.setBounds(39, 12, 400, 400);

    TopicScrollBar = new JScrollBar();
    TopicList1.add(TopicScrollBar);

    MessageList1 = new JTextArea();
    jPanel1.add(MessageList1);
    MessageList1.setBounds(564, 12, 400, 400);

    MessageScrollBar = new JScrollBar();
    MessageList1.add(MessageScrollBar);

    AddTopic1 = new JButton();
    jPanel1.add(AddTopic1);
    AddTopic1.setText("Add Topic");
    AddTopic1.setBounds(144, 489, 120, 22);
    AddTopic1.addActionListener (new java.awt.event.ActionListener () {
        public void actionPerformed (java.awt.event.ActionEvent evt) {
    addTopic (evt);            }
    }  );


    AddMessage1 = new JButton();
    jPanel1.add(AddMessage1);
    AddMessage1.setText("Add Message");
    AddMessage1.setBounds(709, 489, 140, 22);       
    AddMessage1.addActionListener (new java.awt.event.ActionListener () {
        public void actionPerformed (java.awt.event.ActionEvent etc) {
    addMessage (etc);            }
    }  );



    TopicAdderField = new JTextField();
    jPanel1.add(TopicAdderField);
    TopicAdderField.setText("Please Enter Topic Title");
    TopicAdderField.setBounds(39, 461, 400, 22);


    MessageAdderField1 = new JTextField();
    jPanel1.add(MessageAdderField1);
    MessageAdderField1.setText("Please Enter your Message");
    MessageAdderField1.setBounds(564, 455, 400, 22);

    Refreshtopic1 = new JButton();
    jPanel1.add(Refreshtopic1);
    Refreshtopic1.setText("Refresh Topics");
    Refreshtopic1.setBounds(306, 489, 106, 22);


    RefreshMessages1 = new JButton();
    jPanel1.add(RefreshMessages1);
    RefreshMessages1.setText("Refresh Message");
    RefreshMessages1.setBounds(564, 489, 122, 22);

    topicauthorfield = new JTextField();
    jPanel1.add(topicauthorfield);
    topicauthorfield.setText("Enter Author name");
    topicauthorfield.setBounds(39, 433, 400, 22);

    MessageSenderField = new JTextField();
    jPanel1.add(MessageSenderField);
    MessageSenderField.setText("Senders Name");
    MessageSenderField.setBounds(564, 427, 400, 22);

    cp.add(jPanel1,"Center");
    pack();}

public void addMessage(java.awt.event.ActionEvent etc){
    try {
        MessageQueueStatus qsTemplate = new MessageQueueStatus();
        MessageQueueStatus qStatus = (MessageQueueStatus)space.take(qsTemplate,null,Long.MAX_VALUE);


        String messagename = MessageAdderField1.getText();
        String messagesender = MessageSenderField.getText();
        JHB3MessageItem newMessage = new JHB3MessageItem(messagename, messagesender);
        space.write( newMessage, null, Lease.FOREVER);

        qStatus.addMessage();
        space.write( qStatus, null, Lease.FOREVER);
    }  catch ( Exception e) {
        e.printStackTrace();
    }
    }

public void addTopic(java.awt.event.ActionEvent evt){
    try {
        JHB3QueueStatus qsTemplate = new JHB3QueueStatus();
        JHB3QueueStatus qStatus = (JHB3QueueStatus)space.take(qsTemplate,null,Long.MAX_VALUE);


        String topicname = TopicAdderField.getText();
        String topicauthor= topicauthorfield.getText();
        JHB3TopicItem newTopic = new JHB3TopicItem(topicname, topicauthor);
        space.write( newTopic, null, Lease.FOREVER);

        qStatus.addTopic();
        space.write( qStatus, null, Lease.FOREVER);
    }  catch ( Exception e) {
        e.printStackTrace();}
    }

public void processtopics(){
        try {
            JHB3TopicItem qiTemplate = new JHB3TopicItem();
            JHB3TopicItem nextTopic = (JHB3TopicItem)space.readIfExists(qiTemplate,null,Long.MAX_VALUE);
            String nextTopicName = nextTopic.topicname;
            String nextTopicAuthor = nextTopic.topicauthor;
            TopicList1.append(" Author: " + nextTopicAuthor + " Topic Name: " + nextTopicName + "\n" );
        }  catch ( Exception e) {
            e.printStackTrace();}
        }

public void processmessages(){
        try {
            JHB3MessageItem qiTemplate = new JHB3MessageItem();
            JHB3MessageItem newMessage = (JHB3MessageItem)space.readIfExists(qiTemplate,null,Long.MAX_VALUE);
            String nextMessage = newMessage.messagename;
            String nextSender = newMessage.messagesender;
            MessageList1.append(" Sender: " + nextSender + " Message: " + nextMessage + "\n" );
        }  catch ( Exception f) {
            f.printStackTrace();}
        }

    public static void main(java.lang.String[] args) {
        new MainInterface();

}}

2 个答案:

答案 0 :(得分:2)

  

现在他们只是崩溃java应用程序并冻结它,如果有人可以查看“addMessage,AddTopic,Processtopic和ProcessMessage函数,这将是非常好的。

我们无法访问您的第三方API,因此我们无法真正帮助您。

每当我看到“冻结”这个词时,我都认为你阻止了事件调度线程。阅读Concurrency in Swing上的Swing教程中的部分,了解更多信息以及使用ThreadsSwingWorker解决问题的方法。

此外:

  1. 如果您希望人们花时间阅读您的代码,请使用正确的Java变量名称。变量名称不应以大写字符开头。

  2. 按照设计使用的方式使用Swing。那就是使用布局管理器。你不应该使用setBounds()。我给了你一个教程的链接。还有一个关于使用应该阅读的布局管理器的部分。

答案 1 :(得分:0)

所以它似乎是由我的类和我正在与其他程序员共享的Java空间相关联的名称引起的错误,我们中的几个人最终使用相同的名称来定义我们的类的对象模板在使用过程中冻结。

重命名课程解决了这个问题。

谢谢

史密斯先生