本文介绍了基于JRadioButton动态更改JPanel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试根据JRadioButton选择动态地更改内容...我的简化的代码看起来像这样.
I'm trying to change content dynamically based on a JRadioButton selection... My simplified code looks something like this.
//import
public class Thing {
//
JPanel pnlMain, pnl1, pnl2, pnlRt, pnlLt;
JRadioBtn btn1, btn2;
//
Thing () {
//
//initialize panels, add to them, etc.
pnlMain.add(pnlLt);
pnlMain.add(pnl1);
pnlMain.add(pnlRt);
//
//Get it showing and stuff.
//
}
//
//One instance of this class connected to all radio buttons.
class Evt implements ActionListener {
public void actionImplemented (ActionEvent evt) {
//
pnlMain.remove(1);
//
if (evt.getActionCommand().equals("Radio 1"))
pnlMain.add(pnl1);
else pnlMain.add(pnl2);
//
pnlMain.validate();
//
}
}
//
public static void main (String[] args) {
new Thing();
}
//
}
这使我可以更改面板,但是我无法更改回以前选择的面板...我不明白为什么.请帮忙!!!
This lets me change panels, but i cannot change back to a panel i had previously selected... I don't understand why. Please help!!!
推荐答案
您应该改用CardLayout,因为这正是它的用途.在此处教程.
You should be using CardLayout instead, as this is exactly what that is for. Check out the tutorial here.
这篇关于基于JRadioButton动态更改JPanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!