java - 刷新主框架时没有任何反应(JAVA)

标签 java twitter jframe frame actionlistener

我尝试在用户成功连接时显示一条(已登录)消息,但在执行 repaint() 时没有任何反应。你可以看看代码:

public class MainFrame extends JFrame implements ActionListener {

    private static final long serialVersionUID = 1L;
    private static final int FRAME_HEIGHT = 400;
    private static final int FRAME_WIDTH = 250;
    private static final String TITLE = new String("TweeX");
    private static String TWITTERID = new String();
    private static String TWITTERPW = new String();
    private boolean logged = false;
    private JTextField loginField = new JTextField(10);
    private JPasswordField passField = new JPasswordField(10);
    private JButton login = new JButton("Connect");
    private GridBagConstraints c = new GridBagConstraints();
    private String UserStatus = new String("Please login...");

    /*
     * Constructor ! 
     */
    MainFrame() {

        setSize(FRAME_WIDTH, FRAME_HEIGHT);
        setTitle(TITLE);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setResizable(false);
        loginUser();
    }

    /*
     * Login Forms
     */
    protected void loginUser() {

        this.setLayout(new GridBagLayout());

        //add Login Fiels + Label
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.insets = new Insets(5, 5, 5, 20);
        c.gridy = 0;
        add(new JLabel("Username:"), c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 0;
        add(loginField, c);

        //add Password Fiels + Label
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 1;
        add(new JLabel("Password:"), c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 1;
        add(passField, c);

        //add Login button
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 2;
        add(login, c);

        //add listener to login button
        login.addActionListener((ActionListener) this);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 3;
        add(new JLabel(UserStatus), c);
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        TWITTERID = loginField.getText();
        TWITTERPW = passField.getText();
        Twitter twitter = new TwitterFactory()
                .getInstance(TWITTERID, TWITTERPW);
        logged = true;

        try {
            twitter.verifyCredentials();

        } catch (TwitterException e1) {
            logged = false;
        }
    }

    protected void connect() {
        if (logged) {
            UserStatus = "Loged In :)";
            repaint();
        }
    }

    static public void main(String[] argv) {
        new MainFrame();
    }
}

最佳答案

假设verifyCredentials()不会无限期地阻止 EDT,您需要匿名 JLabel一个字段并使用 setText()connect() , 每当它被调用时。打电话repaint()应该是多余的。例如,

private JLabel label = new JLabel("Please login...");
...
protected void loginUser() {
    ...
    add( label, c);
    setVisible(true);
}
...
public void actionPerformed(ActionEvent e) {
    ...
    logged = true;
    connect();
}
...
protected void connect() {
    ...
    label.setText("Loged In: ");
    ...
}

关于java - 刷新主框架时没有任何反应(JAVA),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2987016/

相关文章:

java - 在 Java 中创建包含随机字节的 2GB 文件的最快方法是什么?

Twitter typeahead 只显示了 Bloodhound 返回的一些项目

java - 将 JFrame 作为参数传递给 BufferStrategy 的 JPanel 子类

java - JButton 在调整大小时以一种奇怪的方式复制自己

java - 为什么退出应用程序时会出现应用程序崩溃?

Java 8 按属性过滤

java - 我想了解 Java 的 SecureRandom 对象

html - 使用“AddThis”类型脚本与手动添加每个社交按钮

ios - 在ios7中隐藏键盘

java - 如何在 JFrame 中显示 JButton 和 JLabels?