在这段代码中,我向jframe添加了两个组件,还使用了重新验证和重新绘制,但是只查看了其中一个组件。我需要一种刷新jframe的方法
class A extends JPanel{
int i ,j;
A(int i,int j){
this.i = i;
this.j = j;
}
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.drawOval(i,j,10,10);
}
}
public class T2d extends JPanel
{
public static void main(String[] args) {
JFrame jFrame = new JFrame("hi");
jFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
jFrame.add(new A(50,50));
jFrame.revalidate();//revalidate and repaint doesn't refresh the frame
jFrame.repaint();//i have read all the past question about this
//but none of them solved my problem.
jFrame.add(new A(1000,100));
jFrame.revalidate();
jFrame.repaint();
jFrame.setVisible(true);
}
}发布于 2015-06-23 06:39:40
JFrame默认使用BorderLayout,因此只有最后添加的组件是由布局管理器(BorderLayout)更新的。
您可以尝试更改布局管理器,可能是FlowLayout或GridLayout,但由于A不覆盖getPrefferedSize方法,因此布局管理器将将其大小调整为0x0。
有关更多细节,请参见在容器中布局组件
在一个窗口上调用revalidate和repaint也是毫无意义的,而这个窗口是不可见的。
https://stackoverflow.com/questions/30995496
复制相似问题