繁体   English   中英

如何将模板对话框面板用于多个按钮?

[英]how to use a template dialog panel for multiple buttons?

我正在用Java编写GUI。 我有一个JXTable,目前我只在其中拥有静态数据,但是我希望允许用户使用我创建的模板面板输入数据。

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Window;
import java.awt.Dialog.ModalityType;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;

public class Template_StackOverflowExample extends JPanel{

    private JPanel diagPanel = new dialogTemplate();
    Object[] columnIdentifiers = {
        "id",
        "imei",
    };

    Object[][] data = {
        {"1", "123"},
        {"2", "123"},
        {"3", "123"}
    };

    private JDialog dialog;
    private static DefaultTableModel model;

    public Template_StackOverflowExample(){ 
        setLayout(new BorderLayout());
        JPanel pane = new JPanel(new BorderLayout());

        JButton addRow = new JButton("Add Row");
        addRow.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                openRowPane("Add Row");
            }
        });
        JButton editRow = new JButton("Edit Row");
        editRow.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                openRowPane("Edit Row");
            }
        });

        JPanel buttonPane = new JPanel(new GridLayout(0, 1));
        TitledBorder buttonBorder = new TitledBorder("Buttons");
        buttonPane.setBorder(buttonBorder);

        buttonPane.add(addRow);
        buttonPane.add(editRow);

        model = new DefaultTableModel();
        model.setColumnIdentifiers(columnIdentifiers);
        JTable table = new JTable(model);

        for(int i = 0; i < data.length; i++){
            model.insertRow(i, data[i]);
        }

        JScrollPane scrollPane = new JScrollPane(table);

        pane.add(buttonPane, BorderLayout.LINE_END);
        pane.add(scrollPane, BorderLayout.CENTER);

        add(pane, BorderLayout.CENTER);
    }

    public void openRowPane(String name){
        if(dialog == null){
            Window win = SwingUtilities.getWindowAncestor(this);
            if(win != null){
                dialog = new JDialog(win, name,    ModalityType.APPLICATION_MODAL);
                dialog.getContentPane().add(diagPanel);
                dialog.pack();
                dialog.setLocationRelativeTo(null);
            }
        }
        dialog.setVisible(true);
    }

    public static void createAndShowGUI(){
        JFrame frame = new JFrame("MCVE");      
        Template_StackOverflowExample mainPanel = new     Template_StackOverflowExample();
        frame.add(mainPanel);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

class dialogTemplate extends JPanel{

    private JComponent[] content;
    private String[] labelHeaders = {
        "ID:",
        "IMEI:",
    };

    public dialogTemplate(){
        JPanel diagTemplate = new JPanel();
        diagTemplate.setLayout(new BorderLayout());
        JPanel rowContent = new JPanel(new GridLayout(0, 2));

        JLabel idLabel = null;
        JLabel imeiLabel = null;

        JLabel[] labels = {
            idLabel,
            imeiLabel,
        };

        JTextField idTextField = new JTextField(20);
        JTextField imeiTextField = new JTextField(20);

        content = new JComponent[] {
            idTextField,
            imeiTextField,
        };

        for(int i = 0; i < labels.length; i++){
            labels[i] = new JLabel(labelHeaders[i]);
            rowContent.add(labels[i]);
            rowContent.add(content[i]);
            labels[i].setLabelFor(content[i]);
        }

        JButton save = new JButton("Save"); 
        save.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                closeWindow();
            }
        });
        JButton cancel = new JButton("Cancel");
        cancel.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                closeWindow();
            }
        });

        JPanel buttonPane = new JPanel(new GridLayout(0, 1, 5, 5));
        buttonPane.add(save);
        buttonPane.add(cancel);

        buttonPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

        diagTemplate.add(buttonPane, BorderLayout.PAGE_END);
        diagTemplate.add(rowContent, BorderLayout.CENTER);

        add(diagTemplate);
    }

    public void closeWindow(){
        Window win = SwingUtilities.getWindowAncestor(this);
        if(win != null) {
            win.dispose();
        }
    }
}

您可能会看到窗格中的两个按钮都创建了首先创建的对话框(“添加行”或“编辑行”)。 我希望他们创建不同的对话框,以便数据的编辑与添加数据不冲突。

提前致谢。

为每个按钮分配不同的对话框创建方法,以便您可以打开两个对话框:

    public void openRowPane(String name){
        if(dialog == null){
            Window win = SwingUtilities.getWindowAncestor(this);
            //change modality
            dialog = new JDialog(win, name,    ModalityType.MODELESS);
            dialog.getContentPane().add(diagPanel);
            dialog.pack();
            dialog.setLocationRelativeTo(null);
        }
        dialog.setVisible(true);
    }

    public void openRowPane2(String name){
        if(dialog2 == null){
            Window win = SwingUtilities.getWindowAncestor(this);
            //change modality    
            dialog2 = new JDialog(win, name,    ModalityType.MODELESS);
            dialog2.getContentPane().add(diagPanel2);
            dialog2.pack();
            dialog2.setLocationRelativeTo(null);

        }
        dialog2.setVisible(true);
    }  

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM