繁体   English   中英

刷新主框架(JAVA)时无反应

[英]Nothing happen when refreshing the main Frame (JAVA)

当用户成功连接时,我尝试显示(已登录)消息,但是当执行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 ,都需要将其设置为字段,并在connect()使用setText() 调用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: ");
    ...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM