Java JFrame在更改其内容和包装时闪烁()
我有一个只在页面上显示分数的显示器: 例如: 吉姆博 总分:22分 得分时,会触发一个事件,更新分数并在页面上重新绘制新分数:Java JFrame在更改其内容和包装时闪烁(),java,swing,Java,Swing,我有一个只在页面上显示分数的显示器: 例如: 吉姆博 总分:22分 得分时,会触发一个事件,更新分数并在页面上重新绘制新分数: public void showScore(String username, int score) { contentPane.remove(layout.getLayoutComponent(BorderLayout.CENTER)); score.setText("<html>" + username + "<br/>Tota
public void showScore(String username, int score)
{
contentPane.remove(layout.getLayoutComponent(BorderLayout.CENTER));
score.setText("<html>" + username + "<br/>Total Hits: " + totalHits + "</html>");
contentPane.add(score, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
public void showScore(字符串用户名,整数分数)
{
移除(layout.getLayoutComponent(BorderLayout.CENTER));
score.setText(“+username+”
总点击率:“+totalHits+”);
contentPane.add(score,BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
每次评分时,调用此函数时,它会删除旧的评分消息并添加新的评分消息,然后打包并重新绘制
问题是在绘制更新的帧时窗口会闪烁。而且似乎在一瞬间显示了未格式化的信息
contentPane是一个容器
而frame是一个JFrame调用pack()
将完全重新呈现主机平台拥有的JFrame
。相反,更新用于显示修订分数的JComponent
。举出了一个完整的例子。摘录表格
有关的详细信息,请参阅。调用pack()
将完全重新呈现主机平台拥有的JFrame
。相反,更新用于显示修订分数的JComponent
。举出了一个完整的例子。摘录表格
有关的详细信息,请参阅。调用pack()
将完全重新呈现主机平台拥有的JFrame
。相反,更新用于显示修订分数的JComponent
。举出了一个完整的例子。摘录表格
有关的详细信息,请参阅。调用pack()
将完全重新呈现主机平台拥有的JFrame
。相反,更新用于显示修订分数的JComponent
。举出了一个完整的例子。摘录表格
有关的更多信息,请参见此。首先,我建议您看看Swing中的GUI和其他GUI组件。您可以使用添加到
JFrame
的contentPane
中的。根据您的需要添加所需的标签,并根据您的意愿使用
调用showScore()
方法时,在JLabel
上使用setText(字符串文本)
,而不是作为参数提供的int score
为了使用字符串设置文本,您需要转换int分数
。您可以执行以下两项操作之一(但使用第一个选项):
如果希望分数标签出现或消失,请从添加到JFrame
的JPanel
中添加/删除它。添加后,在标签上调用setText()
,以显示分数。然后,由于重置了标签的文本,因此应该在JPanel
上调用repaint()
。如果您选择添加/删除JLabel
而不只是更改其文本,则还应在JPanel
上调用revalidate()
因此,假设totalHits
实际上是您的分数,那么所有函数都应该是这样的(这是不完整的)
// declare instance fields before constructor
// both variables are reachable by showScore()
private JPanel scorePanel;
private JLabel scoreLabel;
// in constructor of your class
scorePanel = new JPanel(); // initialize the JPanel
scorePanel.setLayout(new BorderLayout()); // give it a layout
scoreLabel = new JLabel(); // initialize the label
frame.add(scorePanel); // add JPanel to your JFrame
// method obviously outside constructor
public void showScore(String username, int score)
{
// clear the JPanel that contains the JLabel to remove it
scorePanel.removeAll();
scoreLabel.setText("<html>" + username + "<br/>Total Hits: " + String.valueOf(score) + "</html>");
scorePanel.add(label, BorderLayout.CENTER);
scorePanel.revalidate();
scorePanel.repaint();
}
//在构造函数之前声明实例字段
//showScore()可以访问这两个变量
私人JPanel记分板;
私人JLabel分数标签;
//在类的构造函数中
scorePanel=new JPanel();//初始化JPanel
scorePanel.setLayout(新的BorderLayout());//给它一个布局
scoreLabel=新的JLabel();//初始化标签
frame.add(记分板);//将JPanel添加到JFrame中
//方法显然在构造函数之外
公共无效显示分数(字符串用户名,整数分数)
{
//清除包含JLabel的JPanel以将其删除
scorePanel.removeAll();
scoreLabel.setText(“+username+”
总点击次数:“+String.valueOf(score)+”);
添加(标签、边框布局、中心);
scorePanel.revalidate();
scorePanel.repaint();
}
您还可以在showScore()
方法中声明和初始化JPanel
和JLabel
,使其更本地化。但是,您还需要在JFrame
上调用pack()
,并在每次调用函数时从中清除前面的JPanel
.首先,我建议您看看Swing中的GUI和其他GUI组件。您可以使用添加到
JFrame
的contentPane
中的。根据您的需要添加所需的标签,并根据您的意愿使用
调用showScore()
方法时,在JLabel
上使用setText(字符串文本)
,而不是作为参数提供的int score
为了使用字符串设置文本,您需要转换int分数
。您可以执行以下两项操作之一(但使用第一个选项):
如果希望分数标签出现或消失,请从添加到JFrame
的JPanel
中添加/删除它。添加后,在标签上调用setText()
,以显示分数。然后,由于重置了标签的文本,因此应该在JPanel
上调用repaint()
。如果您选择添加/删除JLabel
而不只是更改其文本,则还应在JPanel
上调用revalidate()
因此,假设totalHits
实际上是您的分数,那么所有函数都应该是这样的(这是不完整的)
// declare instance fields before constructor
// both variables are reachable by showScore()
private JPanel scorePanel;
private JLabel scoreLabel;
// in constructor of your class
scorePanel = new JPanel(); // initialize the JPanel
scorePanel.setLayout(new BorderLayout()); // give it a layout
scoreLabel = new JLabel(); // initialize the label
frame.add(scorePanel); // add JPanel to your JFrame
// method obviously outside constructor
public void showScore(String username, int score)
{
// clear the JPanel that contains the JLabel to remove it
scorePanel.removeAll();
scoreLabel.setText("<html>" + username + "<br/>Total Hits: " + String.valueOf(score) + "</html>");
scorePanel.add(label, BorderLayout.CENTER);
scorePanel.revalidate();
scorePanel.repaint();
}
//在构造函数之前声明实例字段
//showScore()可以访问这两个变量
私人JPanel记分板;
私人JLabel分数标签;
//在类的构造函数中
scorePanel=new JPanel();//初始化JPanel
scorePanel.setLayout(新的BorderLayout());//给它一个布局
scoreLabel=新的JLabel();//初始化
// declare instance fields before constructor
// both variables are reachable by showScore()
private JPanel scorePanel;
private JLabel scoreLabel;
// in constructor of your class
scorePanel = new JPanel(); // initialize the JPanel
scorePanel.setLayout(new BorderLayout()); // give it a layout
scoreLabel = new JLabel(); // initialize the label
frame.add(scorePanel); // add JPanel to your JFrame
// method obviously outside constructor
public void showScore(String username, int score)
{
// clear the JPanel that contains the JLabel to remove it
scorePanel.removeAll();
scoreLabel.setText("<html>" + username + "<br/>Total Hits: " + String.valueOf(score) + "</html>");
scorePanel.add(label, BorderLayout.CENTER);
scorePanel.revalidate();
scorePanel.repaint();
}