JFrame,当我们单击不同的按钮时,其内容会更改 [英] JFrame whose content changes as we click on the different buttons

查看:86
本文介绍了JFrame,当我们单击不同的按钮时,其内容会更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里使用Java's Swing制作UI应用程序.我创建了一个带有一些按钮的JFrame.当我单击此按钮时,我想要一个新的JFrame,该地方具有一些不同的内容.但是,我不想在这里加载新的JFrame.

I am using Java's Swing here to make a UI application. I have a created a JFrame, with some buttons. When I click on this button, I want a new JFrame with some different content at this place. However, I do not want a new JFrame to load here.

一种方法是在第一个JFrame中的按钮的actionPerformed(ActionEvent obj)方法中将第二个JFrame的可见性设置为True.但是它再次加载了一个新的JFrame,我不想要那个.

One approach, I know is of setting the visbility of the second JFrame to be True in the actionPerformed(ActionEvent obj) method of the button in the first JFrame. But it again loads a new JFrame and I don't want that.

public class FirstUI extends JFrame {
    JButton but1;

    public FirstUI(){
        but1= new JButton("Click here");
        add(but1);

    XYZ obj= new XYZ():
    but1.addActionListener(obj);
    }

    public class XYZ implements ActionListener{
        public void actionPerformed(ActionEvent obj1){

             // WHAT TO DO HERE  
        } 
    }
}

我只想要一个JFrame,当我们单击不同的按钮时,其内容会更改.我该如何实现?

I only want a single JFrame whose content changes as we click on different buttons. How can I achieve that ?

推荐答案

看看

Have a look at CardLayout, this would allow to switch the content of your frame:

CardLayout对象是容器的布局管理器.它将容器中的每个组件视为卡片.一次只能看到一张卡片,而且容器就像一堆卡片一样.首次显示容器时,添加到CardLayout对象的第一个组件是可见组件.

A CardLayout object is a layout manager for a container. It treats each component in the container as a card. Only one card is visible at a time, and the container acts as a stack of cards. The first component added to a CardLayout object is the visible component when the container is first displayed.

有关示例,请参见如何使用CardLayout .

这篇关于JFrame,当我们单击不同的按钮时,其内容会更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆