问题描述
我在这里使用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 ?
推荐答案
看看 CardLayout
,这将允许您切换框架的内容:
Have a look at CardLayout
, this would allow to switch the content of your frame:
有关示例,请参见如何使用CardLayout .
这篇关于JFrame,当我们单击不同的按钮时,其内容会更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!