需要一个解决方案来按下第一个 jframe 中的按钮,它将我转移到另一个 jframe
我正在使用 Netbeans for Java,并且创建了一个包含 2 个 JFrame 的文件,并且在第一个 JFrame 中放置了一个按钮,我想知道如何让这个按钮将我转移到另一个 JFrame ?
I'm using netbeans for Java and I have created a file including 2 JFrames and I have placed a button in the first JFrame , and I want to know how do I make this button transfer me to the other JFrame?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下神奇的线条可以满足您的需求。显然
frame
是目标帧,即您想要将焦点移动到的帧。The following magic lines do what you want. Obviously
frame
there is the target frame, i.e. one you want to move focus to.我建议您不要换出屏幕上的实际窗口,而建议您拥有一个主 JFrame 应用程序,并根据应用程序的状态换出它显示的 JPanel。这更符合我们使用的大多数专业应用程序(包括文字处理器和 IDE),并且对用户来说看起来更自然。要在 Java 中实现此目的,请考虑让容器(通常是包含其他 JPanel 和组件的 JPanel)使用 CardLayout。然后,您可以将其保存的 JPanel 视为卡片,并通过布局中的方法交换它们。例外情况是当您需要在主 GUI 顶部有一个对话框窗口时,这可以简单地通过使用 JOptionPane 方法之一来实现,或者更深入地通过创建和使用模态或非模态 JDialog 来实现。
CardLayout 教程链接
Rather than swapping out actual windows on a screen, I suggest that you have one main JFrame application and that you swap out JPanels that it displays based on the state of the application. This is more in line with most professional applications that we use including word processors and IDEs and will seem more natural-seeming to the user. To achieve this in Java look into having your Container (usually a JPanel that holds other JPanels and components) use a CardLayout. Then you would treat the JPanels it holds as cards and swap them via methods in the layout. The exception to this is when you need a dialog window on top of the main GUI, and this can be achieved simply by use of one of the JOptionPane methods, or in more depth by creating and using a modal or non-modal JDialog.
CardLayout Tutorial Link
@AlexR 是正确的,我更喜欢 @Hovercraft Full Of Eels 方法,但您可能还想尝试一下这个按钮和框架的示例 。
@AlexR is correct and I prefer @Hovercraft Full Of Eels approach, but you might also like to experiment with this example of buttons and frames.