如何从另一个面板更改卡片布局面板?
我用Google搜索了很多,没有找到解决方案。我想应该有java高手来帮助我...
这是我的初始化方法:
private void initialize() {
this.setSize(750, 480);
this.setContentPane(getJContentPane());
this.setTitle("Registration");
JPanel topPane = new TopPane();
this.getContentPane().add(topPane,BorderLayout.PAGE_START);
cards=new JPanel(new CardLayout());
cards.add(step0(),"step0");
cards.add(step1(),"step1");
cards.add(step2(),"step2");
this.getContentPane().add(cards,BorderLayout.CENTER);
}
public JPanel step2(){
EnumMap<DPFPFingerIndex,DPFPTemplate> template = new EnumMap<DPFPFingerIndex, DPFPTemplate>(DPFPFingerIndex.class);
JPanel enrol = new Enrollment(template,2);
return enrol;
}
public JPanel step0(){
JPanel userAgree = new UserAgreement();
return userAgree;
}
public JPanel step1(){
JPanel userInfo = new UserInformation();
return userInfo;
}
public JPanel getCards(){
return cards;
}
这是另一个step0 JPanel的方法:
jButtonAgree.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
Registration reg = new Registration();
LayoutManager cards = reg.getCards().getLayout();
((CardLayout) cards).show(reg.getCards(),"step1");
}
});
根本没有反应,我尝试了重新验证,重新绘制和其他人员...不起作用...任何人都可以在这里得到任何解决方案!
I Googled for a lot, and no solutions found. I think there shall be java masters to help me out ...
This is my initialize method:
private void initialize() {
this.setSize(750, 480);
this.setContentPane(getJContentPane());
this.setTitle("Registration");
JPanel topPane = new TopPane();
this.getContentPane().add(topPane,BorderLayout.PAGE_START);
cards=new JPanel(new CardLayout());
cards.add(step0(),"step0");
cards.add(step1(),"step1");
cards.add(step2(),"step2");
this.getContentPane().add(cards,BorderLayout.CENTER);
}
public JPanel step2(){
EnumMap<DPFPFingerIndex,DPFPTemplate> template = new EnumMap<DPFPFingerIndex, DPFPTemplate>(DPFPFingerIndex.class);
JPanel enrol = new Enrollment(template,2);
return enrol;
}
public JPanel step0(){
JPanel userAgree = new UserAgreement();
return userAgree;
}
public JPanel step1(){
JPanel userInfo = new UserInformation();
return userInfo;
}
public JPanel getCards(){
return cards;
}
This, is the method at another step0 JPanel:
jButtonAgree.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
Registration reg = new Registration();
LayoutManager cards = reg.getCards().getLayout();
((CardLayout) cards).show(reg.getCards(),"step1");
}
});
No reation at all, i tried revalidate, repaint and other staff... doesn't work ... any one got any soution here!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这一切都是为了向外界公开正确的方法和常量字符串,以允许类本身交换视图。例如,为您的第一个类提供一个名为“cardlayout”的私有 CardLayout 字段和一个名为“cards”(持卡人 JPanel)的私有 JPanel 字段,以及一些用于将卡片 JPanel 添加到卡片容器的公共字符串常量。还给它一个公共方法,比如名为
public void swapView(String key)
的方法,它允许外部类交换卡片......就像这样:然后外部类只需调用 swapView 即可交换视图此类的可视化实例,传入适当的键字符串(例如本例中的 CardTest.USER_INFO)以显示用户信息 JPanel。
现在你对这段代码有一个问题,我通过注释指出:
在该行上,你正在创建一个新的注册对象,可能与 GUI 上可视化的对象完全无关,因此调用此对象上的方法新对象对当前查看的 GUI 绝对没有影响。相反,您需要获取对所查看的 Registration 对象的引用,也许可以通过为此类提供 getRegistration 方法,然后调用其方法,如下所示:
例如:
以及驱动这一切的 MainClass
It's all about exposing the right methods and constant Strings to the outside world to allow the class to swap views itself. For example, give your first class a private CardLayout field called cardlayout and a private JPanel field called cards (the card holder JPanel), and some public String constants that are used to add your card JPanels to the cards container. Also give it a public method, say called
public void swapView(String key)
that allows outside classes to swap cards... something like so:Then an outside class can swap views simply by calling the swapView on the visualized instance of this class, passing in the appropriate key String such as in this case CardTest.USER_INFO to show the user info JPanel.
Now you have a problem with this bit of code where I indicate by comment:
On that line you're creating a new Registration object, probably one that is completely unrelated to the one that is visualized on the GUI, and so calling methods on this new object will have absolutely no effect on the currently viewed gui. YOu need instead to get a reference to the viewed Registration object, perhaps by giving this class a getRegistration method, and then call its methods, like so:
For example:
and the MainClass that drives this all