Java 试图更新我的JFrame,为什么赢了';你不能重新油漆吗?

Java 试图更新我的JFrame,为什么赢了';你不能重新油漆吗?,java,swing,user-interface,jframe,jlabel,Java,Swing,User Interface,Jframe,Jlabel,我将运行该程序,但当我激活该事件时,JFrame将不会更新(它仅删除JLabel),除非我手动拖动窗口以调整其大小,即使在事件发生后调用repaint()。怎么了 public Driver() { setLayout( new FlowLayout() ); pass = new JPasswordField( 4 ); add( pass ); image = new ImageIcon( "closedD.png" ); label =

我将运行该程序,但当我激活该事件时,JFrame将不会更新(它仅删除JLabel),除非我手动拖动窗口以调整其大小,即使在事件发生后调用repaint()。怎么了

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();
    }
}

无论何时添加或删除组件,都必须告诉其容器重新布局它所持有的当前组件。您可以通过对其调用
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();
}
注意:你的问题的措辞好像你假设我们知道你想做什么。下次请给我们更多的信息。问题越好,信息量越大,答案就越好,信息量也就越大

编辑2:
我想知道你是否可以简化一下你的代码。与其删除和添加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);
    }
}

无论何时添加或删除组件,都必须告诉其容器重新布局它所持有的当前组件。您可以通过对其调用
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();
}
注意:你的问题的措辞好像你假设我们知道你想做什么。下次请给我们更多的信息。问题越好,信息量越大,答案就越好,信息量也就越大

编辑2:
我想知道你是否可以简化一下你的代码。与其删除和添加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);
    }
}

我的错。我错过了你的编辑。我同意,在这里,CL会有点过分+我很糟糕。我错过了你的编辑。我同意,在这里,CL会有点过分+1.