java 试图更新我的 JFrame,为什么重绘不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14638767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trying to update my JFrame, why won't repaint work?
提问by Mark de la Cruz
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. What's wrong?
我将运行该程序,但是当我激活该事件时,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();
}
}
回答by Hovercraft Full Of Eels
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. You would then call repaint()
after the revalidate call to have the container repaint itself.
任何时候添加或删除组件时,都必须告诉它的容器重新布局它所持有的当前组件。你通过调用revalidate()
它来做到这一点。然后,您将repaint()
在 revalidate调用之后调用以重新绘制容器本身。
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:
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:
编辑 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);
}
}