如何使用给定参数动态创建JPanel?

时间:2014-10-01 09:20:47

标签: java swing user-interface

我的项目中有主JFrame。还有一个主JPanel Y-AXIS BoxLayout,用于包含其中的另一个面板。JFrame这是我使用JPanel默认显示此public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { mainPanel = new MainScreenPanel(); MainFrame mainFrame = new MainFrame(); mainFrame.setContentPane(mainPanel); mainFrame.invalidate(); mainFrame.validate(); mainFrame.setVisible(true); } }); } 的方式(我不太相信这是否正确):

mainPanel

接下来,我将两个JPanel添加到public class MainScreenPanel extends javax.swing.JPanel { public MainScreenPanel() { StatusPanel sPanel = new StatusPanel(); LogPanel lPanel = new LogPanel(); add(sPanel); add(lPanel); } } 中,如下所示:

lPanel

mainPanel上有不同的gui元素。其中一个是打开另一个面板的按钮(addConnectionPanel),并替换jFrame中的private void addCnctButtonActionPerformed(java.awt.event.ActionEvent evt) { JFrame topFrame = (JFrame) SwingUtilities.getWindowAncestor(this); topFrame.setContentPane(new AddConnectionPanel()); topFrame.invalidate(); topFrame.validate(); } 以下是我这样做的方式:

AddConectionPanel

ok有一些标签和输入文本框。它有两个按钮cancelcancel。以下是private void cancelCnctBtnActionPerformed(java.awt.event.ActionEvent evt) { JFrame topFrame = (JFrame) SwingUtilities.getWindowAncestor(this); topFrame.setContentPane(new MainScreenPanel()); topFrame.invalidate(); topFrame.validate(); } 按钮的代码:

sPanel

AddConnectionPanel为空。它必须为空,直到JLabels上的输入框未填充且“确定”为止。按下按钮。执行这些操作后,我想动态创建sPanel,从{{1}}的输入中获取参数。标签应该分组,因此当第二次执行的操作必须创建新组时。有人可以就如何做到这一点给我建议吗?并告诉我我的错误?请记住,我使用的是NetBeans。

1 个答案:

答案 0 :(得分:0)

这将是我的方法:

public interface ConnectionPanelListener{
    void onOkButtonClicked(String... options);

    void onCancelButtonClicked();
}


public class AddConnectionPanel extends JPanel {

    private static final long serialVersionUID = 1L;

    private ConnectionPanelListener listener;


    public AddConnectionPanel(){
        final Map<ConnectionOptions, JTextField> components = new HashMap<>(ConnectionOptions.values().length);

        for(ConnectionOptions option:ConnectionOptions.values()){
            this.add(new JLabel(option.labelCaption));

            JTextField textField = new JTextField();
            //setup textField;
            this.add(textField);

            components.put(option, textField);
        }
        JButton button = new JButton("OK");
        button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(final MouseEvent pE) {
                super.mouseClicked(pE);
                //TODO validate TextFields

                Collection<String> inputs = new Stack<>();

                for(Entry<?,JTextField> e : components.entrySet()){
                    String text = e.getValue().getText();

                    if(text==null || text.trim().isEmpty()){
                        //TODO improve input validation
                        System.out.println("Input text is empty for: "+e.getKey());
                    } else {
                        inputs.add(e.getKey() + ": " + text);
                    }
                }
                listener.onOkButtonClicked(inputs.toArray(new String[inputs.size()]));
            }
        });
        this.add(button);
        button = new JButton("cancel");
        button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(final MouseEvent pE) {
                super.mouseClicked(pE);

                listener.onCancelButtonClicked();
            }
        });
        this.add(button);
    }

    public void setConnectionPanelListener(final ConnectionPanelListener l){
        listener = l;
    }

    private enum ConnectionOptions{
        IP_ADDRESS("IP-Address:"), PORT("Port:"), WHATEVER_ATTRIBUTE_YOU_NEED("Extras:"); 

        private String labelCaption;

        private ConnectionOptions(final String caption) {
            labelCaption = caption;
        }
    }
}

正如您所看到的,AddConnectionPanel期望监听器注册该案例,&#34; OK&#34;或&#34;取消&#34;点击。因此,您调整后的实施可能如下:

private void addCnctButtonActionPerformed(java.awt.event.ActionEvent evt) {                                              
    JFrame topFrame = (JFrame) SwingUtilities.getWindowAncestor(this);

    AddConnectionPanel panel = new AddConnectionPanel();
    panel.setConnectionPanelListener(new ConnectionPanelListener(){

        @Override
        void onOkButtonClicked(String... options){ TODO: fill sPanel using the given Strings }

        @Override
        void onCancelButtonClicked(){ TODO }
    });

    topFrame.setContentPane(panel);
    topFrame.invalidate();
    topFrame.validate();
}