本文介绍了repaint()和revalidate()上出现灰屏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
计时器运行后,我的应用程序显示灰色屏幕.如所建议的,我现在有一个扩展JFrame的MainPage和一个扩展JPanel的MenuPage.我希望在运行MainPage之后加载MenuPage. repaint()和revalidate()对我不起作用.请指出正确的方向.
My application gives a grey screen after the timer is run. As advised, I have now a MainPage which extends JFrame and a MenuPage that extends JPanel. I wish to load MenuPage after MainPage is run. repaint() and revalidate() does not work out for me. Please point me in the right direction.
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
public class MainPage extends JFrame {
private static JPanel contentPane;
//timer
private final static int interval = 40;
private int i;
private Timer t;
private JProgressBar pbar;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainPage frame = new MainPage();
frame.setVisible(true);
}
catch (Exception e) {
e.printStackTrace();
}
}
});
}
public MainPage() {
dim = Toolkit.getDefaultToolkit().getScreenSize();
System.out.println(dim);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(null);
contentPane.setBounds(0,0,dim.width,dim.height);
setContentPane(contentPane);
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
pbar = new JProgressBar (0,20);
pbar.setBounds(600, 500, 200, 45);
pbar.setValue(0);
pbar.setStringPainted(true);
pbar.setForeground(Color.RED);
Border border = BorderFactory.createTitledBorder("Loading...");
pbar.setBorder(border);
t = new Timer (interval, new ActionListener(){
public void actionPerformed(ActionEvent e) {
if (i == 20){
t.stop();
//start.setEnabled(true);
//refresh + load next page
contentPane.removeAll();
MenuPage menuPage = new MenuPage();
//setContentPane(menuPage);
contentPane.add(menuPage);
contentPane.revalidate();
contentPane.repaint();
contentPane.setVisible(true);
}
else{
i++;
pbar.setValue(i);
}
}
});
t.start();
contentPane.add(pbar, BorderLayout.NORTH);
contentPane.add(lblTitle);
contentPane.add(imgLogo);
contentPane.add(imgBackground);
}
}
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class MenuPage extends JPanel {
private JPanel contentPane;
public MenuPage() {
//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setSize(500, 500);
contentPane.setLayout(null);
add (contentPane);
JButton btnSadfsafsa = new JButton("sadfsafsa");
btnSadfsafsa.setBounds(10, 52, 89, 23);
btnSadfsafsa.setEnabled(true);
btnSadfsafsa.setVisible(true);
contentPane.add(btnSadfsafsa);
}
}
推荐答案
您的MenuPage构造函数是问题.
Your MenuPage constructor is the problem.
您创建了一个新的JPanel-contentPane,但从未添加它,也从未设置大小.因此,实际上您只是创建一个空面板.
You create a new JPanel - contentPane but never add it and never set the size. So in fact you just create an empty panel.
这篇关于repaint()和revalidate()上出现灰屏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!