如何更新Jtextarea一次? (mysql方面 - ?)

时间:2012-03-26 23:02:01

标签: java mysql database jtextarea

好的,我一直想做的就是弄清楚如何制作它当我按下程序上的搜索按钮时,当前刚刚打印到控制台的代码将打印到我所拥有的文本区域。我无法弄清楚如何做到这一点,我搜索谷歌仍然没有找到答案。

如果有人能帮助我弄清楚如何将相同的信息行发送到一个有助于大大帮助的mysql数据库,那么我就是这样。

package GTE;
import java.awt.*;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.swing.*;
import twitter4j.*;

public class GTE {

private JFrame frmGte;
public String hashq = "...";
public String twtresults;
public int refresh = 1;

public static void main(String[] args) {
    java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
            try {
                GTE window = new GTE();
                window.frmGte.setVisible(true);
            } catch (Exception e) {}
        }
    });
}
/**
 * Create the application.
 * @throws IOException 
 * @throws FontFormatException 
 */
public GTE(){
    try {
        initialize();
    } catch (FontFormatException e) {} 
    catch (IOException e) {}
}

/**
 * Initialize the contents of the frame.
 * @throws IOException 
 * @throws FontFormatException 
 */
private void initialize() throws FontFormatException, IOException {
    frmGte = new JFrame();
    frmGte.setResizable(false);
    frmGte.setTitle("GTE");
    frmGte.setBounds(100, 100, 450, 390);
    frmGte.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    GridBagLayout gridBagLayout = new GridBagLayout();
    gridBagLayout.columnWidths = new int[]{434, 0};
    gridBagLayout.rowHeights = new int[]{21, 0, 0, 0, 0, 0, 0, 0, 0};
    gridBagLayout.columnWeights = new double[]{0.0, Double.MIN_VALUE};
    gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
    frmGte.getContentPane().setLayout(gridBagLayout);

    JLabel GTETitle = new JLabel("Personal Tweet Extractor");
    InputStream is = this.getClass().getResourceAsStream("ultraviolentbb_reg.ttf");
    Font GTEFont = Font.createFont(Font.TRUETYPE_FONT,is);
    Font f = GTEFont.deriveFont(24f);
    GTETitle.setFont(f);
    GTETitle.setHorizontalAlignment(SwingConstants.CENTER);
    GridBagConstraints gbc_GTETitle = new GridBagConstraints();
    gbc_GTETitle.insets = new Insets(0, 0, 5, 0);
    gbc_GTETitle.anchor = GridBagConstraints.NORTH;
    gbc_GTETitle.fill = GridBagConstraints.HORIZONTAL;
    gbc_GTETitle.gridx = 0;
    gbc_GTETitle.gridy = 0;
    frmGte.getContentPane().add(GTETitle, gbc_GTETitle);

    Label label_2 = new Label("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
    GridBagConstraints gbc_label_2 = new GridBagConstraints();
    gbc_label_2.insets = new Insets(0, 0, 5, 0);
    gbc_label_2.gridx = 0;
    gbc_label_2.gridy = 1;
    frmGte.getContentPane().add(label_2, gbc_label_2);

    JLabel SearchTweets = new JLabel("Search For Tweets With" + hashq + ":");
    GridBagConstraints gbc_SearchTweets = new GridBagConstraints();
    gbc_SearchTweets.insets = new Insets(0, 0, 5, 0);
    gbc_SearchTweets.gridx = 0;
    gbc_SearchTweets.gridy = 2;
    frmGte.getContentPane().add(SearchTweets, gbc_SearchTweets);

    JLabel label = new JLabel("#");
    GridBagConstraints gbc_label = new GridBagConstraints();
    gbc_label.insets = new Insets(0, 0, 5, 0);
    gbc_label.gridx = 0;
    gbc_label.gridy = 3;
    frmGte.getContentPane().add(label, gbc_label);

    JButton Search = new JButton("Start Search");
    Search.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            TS(hashq);
            GTE.this.refresh = 0;
            try {
                nulll dialog = new nulll();
                dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                dialog.setVisible(true);
            } catch (Exception e) {}
        }

        public void TS(String hashtag){
            Twitter twitter = new TwitterFactory().getInstance();
            try {
                System.out.println(hashtag);
                QueryResult result = twitter.search(new Query("#" + hashtag));
                List<Tweet> tweets = result.getTweets();
                for (Tweet tweet : tweets) {
                    System.out.println("@" + tweet.getFromUser() + " : " + tweet.getText());
                    GTE.this.twtresults = ("@" + tweet.getFromUser() + " : " + tweet.getText());
                }
            } catch (TwitterException te) {
                te.printStackTrace();
                System.out.println("Failed to search tweets: " + te.getMessage());
                System.exit(-1);
            }
        }

    });

    TextField textField = new TextField();
    textField.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            GTE.this.hashq = evt.getActionCommand();
        }
    });
    GridBagConstraints gbc_textField = new GridBagConstraints();
    gbc_textField.ipadx = 99;
    gbc_textField.insets = new Insets(0, 0, 5, 0);
    gbc_textField.gridx = 0;
    gbc_textField.gridy = 4;
    frmGte.getContentPane().add(textField, gbc_textField);

    GridBagConstraints gbc_Search = new GridBagConstraints();
    gbc_Search.insets = new Insets(0, 0, 5, 0);
    gbc_Search.gridx = 0;
    gbc_Search.gridy = 5;
    frmGte.getContentPane().add(Search, gbc_Search);

    Label label_1 = new Label("Search Results For Tweets With");
    GridBagConstraints gbc_label_1 = new GridBagConstraints();
    gbc_label_1.insets = new Insets(0, 0, 5, 0);
    gbc_label_1.gridx = 0;
    gbc_label_1.gridy = 6;
    frmGte.getContentPane().add(label_1, gbc_label_1);

    TextArea textArea = new TextArea();
    textArea.setText(twtresults);
    textArea.setEditable(false);
    GridBagConstraints gbc_textArea = new GridBagConstraints();
    gbc_textArea.gridx = 0;
    gbc_textArea.gridy = 7;
    frmGte.getContentPane().add(textArea, gbc_textArea);


    JMenuBar menuBar = new JMenuBar();
    frmGte.setJMenuBar(menuBar);

    JMenu Filemenu = new JMenu("File");
    menuBar.add(Filemenu);

    JMenuItem Exititem = new JMenuItem("Exit");
    Exititem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            System.exit(0);
        }
    });
    Filemenu.add(Exititem);

    JMenu Helpmenu = new JMenu("Help");
    menuBar.add(Helpmenu);

    JMenuItem Aboutitem = new JMenuItem("About");
    Helpmenu.add(Aboutitem);
}

}

1 个答案:

答案 0 :(得分:0)

好的,我将尝试以代码编写的方式回答这个问题。

我认为您希望更新Search动作侦听器中的JTextArea。如果您将textArea移动为GTE的私有属性,例如:

private JFrame frmGte;
private JTextArea textArea;

继续其余的,然后你只需做:

GTE.this.textArea.setText(GTE.this.twtresults)

一旦你构建了twtresults。我想你会在检索推文后立即更新JTextArea。

现在我觉得很脏,我要去洗澡。