我有一个JFrame,它有一些选项.当按下OK按钮时,我想要相同的JFrame清除内容并添加新内容.我试过了,但问题是新的JFrame被弹出.我究竟做错了什么?

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.IOException;

import java.net.InetAddress;

import java.net.UnknownHostException;

import java.util.logging.Level;

import java.util.logging.Logger;

import javax.swing.*;

public class GuiFrame extends JFrame {

final JFrame f = new JFrame("Test");

public void Starter(){

ImageIcon img = new ImageIcon("C:\\Users\\neal\\Desktop\\no.png");

f.setIconImage(img.getImage());

ButtonGroup group = new ButtonGroup();

final JRadioButton Acess = new JRadioButton("Acess");

final JRadioButton Chat = new JRadioButton("Chat");

group.add(Acess);

group.add(Chat);

f.setSize(400,100);

f.setLocationRelativeTo(null);

JButton OptionOk = new JButton("OK");

Label option = new Label("Choose a Option");

final Container content = f.getContentPane();

content.setBackground(Color.white);

content.setLayout(new FlowLayout());

content.add(option);

content.add(Acess);

content.add(Chat);

content.add(OptionOk);

f.setVisible(true);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

OptionOk.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

try {

new GuiFrame().Initiate();

} catch (UnknownHostException ex) {

Logger.getLogger(GuiFrame.class.getName()).log(Level.SEVERE, null, ex);

}

}

});

}

public void Initiate() throws UnknownHostException {

f.removeAll();

ButtonGroup group = new ButtonGroup();

final JRadioButton ButtonServer = new JRadioButton("Server");

final JRadioButton ButtonClient = new JRadioButton("Client");

group.add(ButtonServer);

group.add(ButtonClient);

f.setSize(400, 100);

f.setLocationRelativeTo(null);

InetAddress thisIp = InetAddress.getLocalHost();

ImageIcon img = new ImageIcon("C:\\Users\\neal\\Desktop\\no.png");

f.setIconImage(img.getImage());

Label lip = new Label("Your IP is : " thisIp.getHostAddress());

Label setup = new Label("Setup as ");

JButton ButtonOk = new JButton("OK");

final Container content = f.getContentPane();

content.setBackground(Color.white);

content.setLayout(new FlowLayout());

content.add(lip);

content.add(setup);

content.add(ButtonServer);

content.add(ButtonClient);

content.add(ButtonOk);

f.setVisible(true);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public static void main(String[] args) throws UnknownHostException {

GuiFrame gf = new GuiFrame();

gf.Starter();

}

}

解决方法:

解决方案很简单:使用CardLayout,让这个布局管理器为您完成所有繁重的工作.有关如何执行此操作的更多详细信息,请参阅教程:How to use CardLayout

至于你的代码,请注意你实际上在启动时创建了2个JFrame,如果推送了JButton则还有两个:

GuiFrame类本身扩展了JFrame,但它似乎是一个你永远不会使用的JFrame,因此被浪费了,但它在程序启动时以及每当创建GuiFrame实例时创建,例如按下按钮时.然后在这个类的内部创建另一个JFrame f,一个在程序启动时再次按下按钮,我不认为这是你想要做的.

因此,更改代码以使类不扩展JFrame,并且不要在按钮的ActionListener中创建类的新实例.而是使用CardLayout交换视图.

例如:

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.*;

public class GuiFrame {

private static final String FIRST_PANEL = "First Panel";

private static final String SECOND_PANEL = "Second Panel";

final JFrame f = new JFrame("Test");

private CardLayout cardLayout = new CardLayout();

private JPanel content;

public void Starter() {

f.setSize(400, 100);

f.setLocationRelativeTo(null);

JButton OptionOk = new JButton("OK");

Label option = new Label("Choose a Option");

content = (JPanel) f.getContentPane();

content.setLayout(cardLayout);

JPanel firstPanel = new JPanel();

firstPanel.setBackground(Color.white);

firstPanel.setLayout(new FlowLayout());

firstPanel.add(option);

firstPanel.add(OptionOk);

content.add(firstPanel, FIRST_PANEL);

content.add(createSecondPanel(), SECOND_PANEL);

f.setVisible(true);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

OptionOk.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

cardLayout.show(content, SECOND_PANEL);

}

});

}

private JPanel createSecondPanel() {

JPanel secondPanel = new JPanel();

secondPanel.add(new JButton(new AbstractAction("Go Back") {

public void actionPerformed(ActionEvent e) {

cardLayout.show(content, FIRST_PANEL);

}

}));

return secondPanel;

}

public static void main(String[] args) {

GuiFrame gf = new GuiFrame();

gf.Starter();

}

}

来源:https://www.icode9.com/content-1-463801.html

java frame清除控件_java – 清除JFrame的组件并添加新组件相关推荐

  1. java swing 表格控件_java swing 开发 -JTable

    最近利用空闲时间自己琢磨了一下java swing 编程,其实在从事javaweb之前我一直向往的就是java swing 开发,不知道为什么可能当时觉得Windows上的exe程序很是神奇,关于wi ...

  2. java调用日期控件_JAVA基础应用:日期时间选择控件(代码)

    private JPanel createWeekAndDayPanal() { String colname[] = {"日","一","二&quo ...

  3. java 的日期选择控件_Java日期选择控件

    一起学习 一次项目研发中需要日期时间选择控件, 网上提供的不多, 且质量一般, 所以只好自己做,参考了 网上某位同学的 作品 Jave 日期选择控件 DateChooser . 目前的代码将日期时间选 ...

  4. MFC 控件PictureControl 清除显示

    MFC 控件PictureControl 清除显示 方法一: GetDlgItem(IDC_STATIC_CAPTURE_PIC)->ShowWindow(FALSE); GetDlgItem( ...

  5. MFC实战篇——图片旋转、控件PictureControl 清除显示、伪彩、直方图显示、为按钮添加背景图标、设置程序图标

    文章目录 一.图片旋转 二.MFC 控件PictureControl 清除显示 三.伪彩 四.直方图 五.为按钮添加背景图标 六.设置图标 七.改变组框外观 一.图片旋转 建立图片控件 改变图片控件I ...

  6. java调用ocx控件获取数据_Java调用ocx控件以及dll

    通过Java调用OCX控件有几种方法,JNI.JACOB.Jawin等 1.JNI 最直接的方式,也是最麻烦的方式,需要自己完成所有的工作,不推荐. 2.Jawin 尝试了一下,效果不错,但相对来说, ...

  7. java程序获取外部java程序的控件,将 Java 小程序迁移到 Microsoft J# 浏览器控件-JSP教程,Java技巧及代码...

    visual j# .net 小组 microsoft corporation 摘要:通过 microsoft j# 浏览器控件,开发人员可以将所编写的在 java 虚拟机上运行的 java 小程序迁 ...

  8. Java调用ocx控件以及dll

    2019独角兽企业重金招聘Python工程师标准>>> http://lvqingboy-163-com.iteye.com/blog/769358 通过Java调用OCX控件有几种 ...

  9. 龙博方案网Big Faceless Java图形展现控件详细介绍及下载

    2019独角兽企业重金招聘Python工程师标准>>> Graph Library 是用于以Java创建图形和图表的Java类库.它使用全三维模式,可在 PNG.Flash.PDF或 ...

最新文章

  1. 云计算之Docker介绍
  2. POJ 3630 Phone List
  3. mysql 建表时建立索引_mysql 分享建表和索引的几点规范
  4. C++Builder STL 泛型
  5. C语言关系运算符介绍和示例
  6. ASP.NET常用代码汇总
  7. 操作数据库pymysql
  8. ym——android源码大放送(实战开发必备)
  9. 英语面试自我介绍范文
  10. Python项目实战:使用selenium爬取拉勾网数据
  11. 【高等数学】微分方程
  12. 实际案例说明计算机网络安全,计算机网络安全案例教程
  13. Winsock属性、方法介绍
  14. 利用计算机建模的优点,论计算机技术在数学建模领域的应用
  15. 屠光绍:公司债启动时机成熟 市场意义重大
  16. 回流焊接温度曲线用户手册 (HLW)
  17. java语言程序设计第三版答案郎波著,太完整了!
  18. 有哪些方法处理废水中的重金属
  19. Python 程序性能测试方法
  20. eclipse里建立servlet文件时,package下面有红色下划线

热门文章

  1. flash 火狐总是崩溃_win10系统火狐flash插件总是崩溃的解决方法
  2. 重建索引一般需要多久_游泳小白学游泳,一般需要多久才能学会?猜猜看
  3. ubuntu mysql支持中文_ubuntu (16.04) server 英文原版 添加中文语言支持 消除java 程序、mysql 数据库不能处理中文的错误...
  4. 互联网物流是计算机类吗,那些常常被误解的大学专业,亲戚眼中的修电脑送快递,网友:想哭...
  5. 嵌入式基于Linux电机控制,基于嵌入式arm+linux平台的直流电机调速控制系统.pdf
  6. 仪表盘怎么调 铃木uy125摩托车_平时市区骑行,摩托车链条多久保养一次?
  7. if or函数套用_IF函数和OR函数的套用我想利用IF函数和 – 手机爱问
  8. R中‘ts‘ object must have one or more observations
  9. c mysql 免安装版_MySQL5.6免安装版环境配置图文教程
  10. cad渐变线怎么画_怎么画压力线和支撑线