简体   繁体   English

尝试更新我的JFrame,为什么不重新粉刷工作?

[英]Trying to update my JFrame, why won't repaint work?

I will run the program, but when I activate the event, the JFrame will not update (it only removes the JLabel ) unless I manually drag the window to resize it, even with the repaint() being called after the event takes place. 我将运行程序,但是当我激活事件时,除非我手动拖动窗口以调整其大小,否则JFrame不会更新(它只会删除JLabel),即使事件发生后调用了repaint()。 What's wrong? 怎么了?

public Driver() {
    setLayout( new FlowLayout() );

    pass = new JPasswordField( 4 );
        add( pass );

    image = new ImageIcon( "closedD.png" );
    label = new JLabel( "Enter the password to enter the journal of dreams" , image , JLabel.LEFT );
        add( label );

    button = new JButton( "Enter" );
        add( button );

    event e = new event();
        button.addActionListener( e );

    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    setVisible( true );
    setSize( 1600/2 , 900/2 );
    setTitle( "Diary" );
}

//main method
//
//
public static void main( String[] args ) {
    win = new Driver();
}

public class event implements ActionListener {
    private boolean clickAgain = false;

    public void actionPerformed( ActionEvent e ) {
        if ( passEquals( password ) && clickAgain == false ) {
            image2 = new ImageIcon( "openD.png" );
            remove( label );

            label = new JLabel( "Good Job! Here is the journal of dreams." , image2 , JLabel.LEFT );
                add( label );

                clickAgain = true;
        }

        repaint();
    }
}

Any time you add or remove a component, you must tell its container to re-layout the current components it holds. 任何时候添加或删除组件时,都必须告诉其容器重新布局其所拥有的当前组件。 You do this by calling revalidate() on it. 您可以通过在其上调用revalidate()来实现。 You would then call repaint() after the revalidate call to have the container repaint itself. 然后,您将在revalidate调用之后调用repaint() ,以使容器重新绘制自身。

public void actionPerformed( ActionEvent e ) {
    if ( passEquals( password ) && clickAgain == false ) {
        image2 = new ImageIcon( "openD.png" );
        remove( label );

        label = new JLabel( "Good Job! Here is the journal of dreams.", 
             image2 , JLabel.LEFT );
            add( label );

            clickAgain = true;
    }
    revalidate(); // **** added ****
    repaint();
}

Note: your question is worded in such a way as if you assume that we know what you're trying to do. 注意:您的问题用措辞来表达,就好像您假设我们知道您要做什么。 Please give us more information next time. 下次请给我们更多信息。 The better and more informative the question, the better and more informative the answer. 问题越好,信息量越大,答案也就越好。

Edit 2: 编辑2:
I wonder if you could simplify your code a bit. 我想知道您是否可以简化您的代码。 Instead of removing and adding a JLabel, better to just simply set the current JLabel's text and Icon: 与其删除并添加JLabel,不如简单地设置当前JLabel的文本和图标,效果更好:

public void actionPerformed( ActionEvent e ) {
    if ( passEquals( password ) && clickAgain == false ) {
        image2 = new ImageIcon( "openD.png" );
        // remove( label );  // removed

        label.setText( "Good Job! Here is the journal of dreams.");
        label.setIcon(image2);
    }
}

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

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