我正在制作一个Java gui项目,它由两个框架组成。
问题是,当我从第一帧调用第二帧时,我已将其设置为第一帧可见性设置为 false。问题是如何使用第二帧中的按钮使第一帧再次可见。
我应该放弃这种方法并创建一个新的 jPanel 吗???jpanel 是否有与 jframe 类似的功能?
考虑使用 CardLayout
。这样,您可以通过多个 UI 进行切换,而无需另一个帧。以下是使用它的方法。
编辑:正如纪尧姆在他的评论中发布的那样,安德鲁的这个答案也涵盖了如何使用布局。
编辑2:
当您请求有关我的最新帖子的更多信息时,这样的类可能如下所示:
import javax.swing.JFrame;
public abstract class MyFrameManager {
static private JFrame startFrame,
anotherFrame,
justAnotherFrame;
static public synchronized JFrame getStartFrame()
{
if(startFrame == null)
{
//frame isnt initialized, lets do it
startFrame = new JFrame();
startFrame.setSize(42, 42);
//...
}
return startFrame;
}
static public synchronized JFrame getAnotherFrame()
{
if(anotherFrame == null)
{
//same as above, init it
}
return anotherFrame;
}
static public synchronized JFrame getJustAnotherFrame()
{
//same again
return justAnotherFrame;
}
public static void main(String[] args) {
//let's test!
JFrame start = MyFrameManager.getStartFrame();
start.setVisible(true);
//want another window
JFrame another = MyFrameManager.getAnotherFrame();
another.setVisible(true);
//oh, doenst want start anymore
start.setVisible(false);
}
}
这样,您只需实例化每个JFrame
一次,但您始终可以通过经理类访问它们。在那之后你如何处理它们是你的决定。
我也只是让它是线程安全的,这对单例至关重要。