如何使用Java Netbeans在GUI中显示输出控制台

时间:2015-10-14 10:53:15

标签: java mysql user-interface netbeans

我正在使用netbeans与设计(调色板)。 我想在netbeans中将我的输出控制台显示到GUI中。 所以这是我想要执行的代码。 我把它放到按钮功能,它的工作原理。 但是我的结果显示在输出控制台中,我打算在界面中显示它。

User user = status.getUser();
            Date dated = status.getCreatedAt();
            PreparedStatement stmt = null;
            Connection conne = null;
            try {
                Class.forName("com.mysql.jdbc.Driver");
                System.out.println("Connecting to database...");
                conne = DriverManager.getConnection("jdbc:mysql://localhost/jat?useUnicode=true&characterEncoding=UTF-8", "root", "");
                System.out.println(status);
                System.out.println("Inserting records into the table...");
                stmt = conne.prepareStatement("set names 'utf8'");
                stmt.execute();
                stmt = conne.prepareStatement("set character set utf8");
                stmt.execute();
                stmt = conne.prepareStatement("INSERT INTO tweet(ID,date,name,statusLocation,text,source) VALUES (?,?,?,?,?,?)");
                stmt.setInt(1, (int) status.getId());
                stmt.setString(2, getTimeStamp());
                stmt.setString(3, status.getUser().getScreenName());
                stmt.setString(4, user.getLocation());
                stmt.setString(5, status.getText());
                stmt.setString(6, status.getSource());
                stmt.executeUpdate();
                System.out.println("this record inserted!");
                System.out.println("==================");
            } catch (SQLException se) {
                se.printStackTrace();
            } catch (final Exception e) {
                e.printStackTrace();
            }
        }
        @Override
        public void onTrackLimitationNotice(int arg0) {
        }
        @Override
        public void onStallWarning(StallWarning sw) {
            throw new UnsupportedOperationException("Not supported yet."); 
        }
    };       
    FilterQuery fq = new FilterQuery();
    double lat1 = 121.300000d;
    double long1 = 25.020000d;
    double lat2 = 121.380000d;
    double long2 = 25.030000d;
    twitterStream.addListener(listener);
    double[][] bb = {{lat1, long1}, {lat2, long2}};
    fq.locations(bb);
    twitterStream.filter(fq);}

当'运行'按钮执行后,结果只显示在输出控制台中,而不是在我的界面中。

这是我的界面看起来像: enter image description here

非常感谢您的回复

2 个答案:

答案 0 :(得分:1)

你想要做的是使用jTextarea或jtextfield,或者在我的情况下,我使用jlabels将我的输出显示到控制台..

例如:如果我的结果变量中有结果,请使用此结果变量将输出设置为

this.jLabel1.setText(Result); //这会在GUI中显示你的标签

与其他元素类似的情况

this.jTextField1.setText(Result);

答案 1 :(得分:0)

将对System.out.println的调用替换为jTextArea1.append,其中jTextArea1是TextArea在您的界面中的名称。您可能还想在每个字符串的末尾添加\n