简体   繁体   English

如何在JFrame中动态重绘JPanel?

[英]How to dynamically repaint JPanels in a JFrame?

I am in the process of making a simple java swing program that will allow the user to play blackjack. 我正在制作一个简单的java swing程序,该程序将允许用户玩二十一点。 I have 10-15 classes that represent 1 "screen" in the program, they each extend JLayeredPane and implement action listener. 我有10-15个类,它们表示程序中的1个“屏幕”,它们各自扩展JLayeredPane并实现动作侦听器。 I also have other classes for deck, card, round, player etc. 我也有其他类别的套牌,卡片,回合,玩家等。

Then in another class has two properties: an array arra(which holds all the panels) and a Jframe one (which all the panels will go to), with the public static void main. 然后在另一个类中有两个属性:数组arra(用于保存所有面板)和Jframe(用于所有面板),带有公共static void main。

Then in that same class I do two things, a method which replaces panels: 然后在同一个类中,我做两件事,一个替换面板的方法:

public void screenInit(int i)
{
    one.setContentPane(arra[i-1]);
    one.invalidate();
    one.validate();
    one.repaint();
}

Then I create an object of this class in the psvm and start by putting in the first panel. 然后,我在psvm中创建此类的对象,并从第一个面板开始。

So my question is now that the first panel is up, which has the action listener attached to a JButton how does it access the JFrame created in the class holding the JFrame and the array so it can use the method above. 所以我的问题是,第一个面板打开了,它的动作侦听器附加到JButton上,它如何访问在包含JFrame和数组的类中创建的JFrame,以便可以使用上面的方法。 Right now, I'm having to create a new frame everytime, so it builds on top of each other? 现在,我每次都必须创建一个新框架,所以它可以彼此叠加吗?

If this is not possible, how should I organise my classes in a way that multiple buttons from each panels will lead to one another? 如果无法做到这一点,那么我该如何组织我的班级,使每个面板上的多个按钮相互引导?

Thanks 谢谢

1) in the case that you use Swing JComponents then you can remove code line one.invalidate(); 1)如果您使用Swing JComponents,则可以删除代码行one.invalidate(); because there no reason set status for current LayoutManager that JPanel isn't valid element of current displayed GUI 因为没有理由为当前LayoutManager设置状态,即JPanel不是当前显示的GUI的有效元素

2) if you remove / modify / add JComponent(s) top the already visible Container , then I have success with code lines revalidate() and repaint() 2)如果您remove / modify / add JComponent(s)在已经可见的Container顶部,那么我可以成功使用代码行revalidate()repaint()

3) nobody knows what's you real issue(s), nor someone can see code in your monitor, better would be to isolating the issue with repainting of GUI and edit your quesion with a SSCCE 3)没有人知道您真正的问题是什么,也没有人可以在您的监视器中看到代码,最好是通过重新绘制GUI隔离问题并使用SSCCE编辑您的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM