Swing中的切换面板 [英] Switching Panels in Swing

查看:242
本文介绍了Swing中的切换面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Card Layout的Swing应用程序,该应用程序基本上根据用户从下拉菜单中选择的内容来更改显示的面板.

I have a Swing application using Card Layout which basically changes the displayed panel depending on what the user selects from a drop-down menu.

我的一个面板有一个表格.我需要在按下提交按钮时收集所有输入并将面板切换到另一输入. (第二个面板是在单独的类中定义的),我还需要将所有输入以某种方式传递给新面板中的方法.

One of my panels has a form. I would need for when the submit buton is pressed for all the inputs to be collected and the Panel to be switched to another one. (This second panel is defined in a separate class) I would also need for all the input to be somehow passed to a method in the new panel.

有什么建议吗? 达里奥(Dario)

Any suggestions? Dario

推荐答案

如果您看下面的代码中的<--,则每个人都应该解决您在帖子中遇到的每个不同的问题.我认为您应该知道如何制作提交"按钮,因此没有包括在内. (注意:这不是运行代码,只是建议);

If you look at the <--s in the following code, each should solve each different question you have in your post. I figured you should know how to make a submit button, so I didn't include that. (Note: this is not running code, just suggestions);

public class MainPanel entends JPanel {
    CardLayout layout = new CardLayout(); <-- card layout
    JPanel panel = new JPanel(layout);    <-- set layout to main panel
    NewPanel newPanel = new NewPanel();   <-- you new panel
    JPanel p1 = new JPanel();             <-- random panel
    JTextField text = new JTextField()    <-- text field in form
    JButton button = new JButton();

    JComboBox cbox = new JComboBox(new String[] {"newPanel", "p1"});  <-- hold panel names

    public MainPanel(){
        panel.add(newPanel, "newPanel");       <-- name associated with panel
        panel.add(p1, "p1");

        ...

        cbox.addAItemListener(new ItemListener(){
            public void itemStateChnaged(ItemEvent e){

                layout.show(panel, (string).getItem());     <-- show Panel from combobox
            }
        });

        button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                String txt = text.getText();
                newPanel.printText(txt);             <-- Using method from other class    
            }
        });
    }
}  

public class NewPanel extends JPanel {

    public void printText(String text){             <-- method from other class
        System.out.println(text);
    }
}

这篇关于Swing中的切换面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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