swing中如何跳转到另一个界面

分类:网络文章 时间:2024-01-15 07:45 浏览:0 评论:0
0

要实现swing跳转到另一个界面,可以使用以下步骤:

  1. 新建一个JFrame对象作为要跳转的界面。
  2. 在当前界面的事件处理方法中,使用setVisible(false)隐藏当前界面。
  3. 使用setVisible(true)显示新界面。

以下是一个简单的示例代码:

import javax .swing.*;公共 class MainFrame 扩展 JFrame { 私有 JButton 按钮; public MainFrame() { setTitle ( "主界面"); setSize(300, 200 ); setLocationRelativeTo(null); 按钮 =  JButton("跳转"); button.addActionListener(e -> JumpToAnotherFrame()); JPanel 面板 =  JPanel(); panel.add(button); add(panel); } 私有 void jumpToAnotherFrame() { < span class="hljs-type">AnotherFrame  anotherFrame = new AnotherFrame(); setVisible(false); anotherFrame.setVisible (<跨度class="hljs-literal" >true);处置(); //释放当前界面资源。如果不需要返回当前界面,可以调用dispose()方法 } public static  void span> main(字符串[ ] args) { SwingUtilities.invokeLater(() -> { MainFrame mainFrame = new MainFrame(); mainFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); mainFrame.setVisible( true); }); }}
导入 javax.swing。 *;公共  AnotherFrame 扩展 JFrame {  ">public AnotherFrame() { setTitle("另一个接口"); setSize(300, 200); setLocationRelativeTo (< span class="hljs-literal">null); }}

上例中,点击主界面上的按钮会隐藏主界面并显示另一个界面另一个界面的代码与主界面类似,只是界面上的内容可以根据需要进行调整。

1. 本站所有资源来源于用户上传或网络,仅作为参考研究使用,如有侵权请邮件联系站长!
2. 本站积分货币获取途径以及用途的解读,想在本站混的好,请务必认真阅读!
3. 本站强烈打击盗版/破解等有损他人权益和违法作为,请各位会员支持正版!
4. 网络文章 > swing中如何跳转到另一个界面

用户评论