如何将JDialog链接到JButton单击

时间:2016-05-29 20:19:30

标签: java button jbutton jdialog

我在eclipse上使用窗口构建器,我正在尝试使Button打开JDialog。我花了很多时间筛选帖子和谷歌搜索,我拉的任何东西都解决了我的问题。

JButton btnNewButton = new JButton("New button");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
              ApiKeyEntryDialog.setVisible(true);
        }
    });

带按钮的主JFrame。

private JFrame frame;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                FrontPage window = new FrontPage();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public FrontPage() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 1377, 789);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JMenuBar menuBar = new JMenuBar();
    menuBar.setBounds(0, 0, 1361, 21);
    frame.getContentPane().add(menuBar);

    JMenu mnFile = new JMenu("File");
    menuBar.add(mnFile);

    JMenu mnTools = new JMenu("Tools");
    menuBar.add(mnTools);

    JMenu mnView = new JMenu("View");
    menuBar.add(mnView);

    JMenu mnHelp = new JMenu("Help");
    menuBar.add(mnHelp);

    JTabbedPane MainTabbedPane = new JTabbedPane(JTabbedPane.TOP);
    MainTabbedPane.setBounds(0, 21, 1361, 729);
    frame.getContentPane().add(MainTabbedPane);

    JPanel MainTab = new JPanel();
    MainTabbedPane.addTab("Main", null, MainTab, null);
    MainTab.setLayout(null);

    JButton btnNewButton = new JButton("New button");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
              ApiKeyEntryDialog.setVisible(true);
        }
    });
    btnNewButton.setBounds(10, 11, 89, 23);
    MainTab.add(btnNewButton);

    JPanel AssetsTab = new JPanel();
    MainTabbedPane.addTab("Assets", null, AssetsTab, null);

    JPanel BuyOrdersTab = new JPanel();
    MainTabbedPane.addTab("Buy Orders", null, BuyOrdersTab, null);

    JPanel SellOrdersTab = new JPanel();
    MainTabbedPane.addTab("Sell Orders", null, SellOrdersTab, null);

    JPanel JitaTab = new JPanel();
    MainTabbedPane.addTab("Jita", null, JitaTab, null);


}

按下按钮时我想弹出的JDialog帧。

private final JPanel contentPanel = new JPanel();
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
private JTextField textField_5;
private JTextField textField_6;
private JTextField textField_7;
private JTextField textField_8;
private JTextField textField_9;
private JTextField textField_10;
private JTextField textField_11;
private JTextField textField_12;
private JTextField textField_13;
private JTextField textField_14;
private JTextField textField_15;
private JTextField textField_16;
private JTextField textField_17;
private JTextField textField_18;
private JTextField textField_19;
private JTextField textField_20;
private JTextField textField_21;
private JTextField textField_22;
private JTextField textField_23;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    try {
        ApiKeyEntryDialog dialog = new ApiKeyEntryDialog();
        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dialog.setVisible(true);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

/**
 * Create the dialog.
 */
public ApiKeyEntryDialog() {
    setBounds(100, 100, 610, 803);
    getContentPane().setLayout(new BorderLayout());
    contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
    getContentPane().add(contentPanel, BorderLayout.CENTER);
    contentPanel.setLayout(null);
    {
        JLabel label = new JLabel("Api Key Entry");
        label.setFont(new Font("Tahoma", Font.PLAIN, 15));
        label.setBounds(10, 11, 107, 19);
        contentPanel.add(label);
    }
    {
        textField = new JTextField();
        textField.setColumns(10);
        textField.setBounds(97, 65, 482, 20);
        contentPanel.add(textField);
    }
    {
        JLabel label = new JLabel("Verification Code:");
        label.setBounds(10, 68, 85, 17);
        contentPanel.add(label);
    }
    {
        JLabel label = new JLabel(" Key ID:");
        label.setBounds(55, 43, 46, 14);
        contentPanel.add(label);
    }
    {
        textField_1 = new JTextField();
        textField_1.setColumns(10);
        textField_1.setBounds(97, 40, 482, 20);
        contentPanel.add(textField_1);
    }
    {
        textField_2 = new JTextField();
        textField_2.setColumns(10);
        textField_2.setBounds(97, 96, 482, 20);
        contentPanel.add(textField_2);
    }
    {
        textField_3 = new JTextField();
        textField_3.setColumns(10);
        textField_3.setBounds(97, 121, 482, 20);
        contentPanel.add(textField_3);
    }
    {
        JLabel label = new JLabel("Verification Code:");
        label.setBounds(10, 124, 85, 17);
        contentPanel.add(label);
    }
    {
        JLabel label = new JLabel(" Key ID:");
        label.setBounds(55, 99, 46, 14);
        contentPanel.add(label);
    }
    {
        textField_4 = new JTextField();
        textField_4.setColumns(10);
        textField_4.setBounds(97, 152, 482, 20);
        contentPanel.add(textField_4);
    }
    {
        textField_5 = new JTextField();
        textField_5.setColumns(10);
        textField_5.setBounds(97, 177, 482, 20);
        contentPanel.add(textField_5);
    }
    {
        JLabel label = new JLabel("Verification Code:");
        label.setBounds(10, 180, 85, 17);
        contentPanel.add(label);
    }
    {
        JLabel label = new JLabel(" Key ID:");
        label.setBounds(55, 155, 46, 14);
        contentPanel.add(label);
    }
    {
        textField_6 = new JTextField();
        textField_6.setColumns(10);
        textField_6.setBounds(97, 208, 482, 20);
        contentPanel.add(textField_6);
    }
    {
        textField_7 = new JTextField();
        textField_7.setColumns(10);
        textField_7.setBounds(97, 233, 482, 20);
        contentPanel.add(textField_7);
    }
    {
        JLabel label = new JLabel("Verification Code:");
        label.setBounds(10, 236, 85, 17);
        contentPanel.add(label);
    }
    {
        JLabel label = new JLabel(" Key ID:");
        label.setBounds(55, 211, 46, 14);
        contentPanel.add(label);
    }
    {
        textField_8 = new JTextField();
        textField_8.setColumns(10);
        textField_8.setBounds(97, 264, 482, 20);
        contentPanel.add(textField_8);
    }
    {
        textField_9 = new JTextField();
        textField_9.setColumns(10);
        textField_9.setBounds(97, 289, 482, 20);
        contentPanel.add(textField_9);
    }
    {
        JLabel label = new JLabel("Verification Code:");
        label.setBounds(10, 292, 85, 17);
        contentPanel.add(label);
    }
    {
        JLabel label = new JLabel(" Key ID:");
        label.setBounds(55, 267, 46, 14);
        contentPanel.add(label);
    }
    {
        textField_10 = new JTextField();
        textField_10.setColumns(10);
        textField_10.setBounds(97, 320, 482, 20);
        contentPanel.add(textField_10);
    }
    {
        textField_11 = new JTextField();
        textField_11.setColumns(10);
        textField_11.setBounds(97, 345, 482, 20);
        contentPanel.add(textField_11);
    }
    {
        JLabel label = new JLabel("Verification Code:");
        label.setBounds(10, 348, 85, 17);
        contentPanel.add(label);
    }
    {
        JLabel label = new JLabel(" Key ID:");
        label.setBounds(55, 323, 46, 14);
        contentPanel.add(label);
    }
    {
        textField_12 = new JTextField();
        textField_12.setColumns(10);
        textField_12.setBounds(97, 376, 482, 20);
        contentPanel.add(textField_12);
    }
    {
        textField_13 = new JTextField();
        textField_13.setColumns(10);
        textField_13.setBounds(97, 401, 482, 20);
        contentPanel.add(textField_13);
    }
    {
        JLabel label = new JLabel("Verification Code:");
        label.setBounds(10, 404, 85, 17);
        contentPanel.add(label);
    }
    {
        JLabel label = new JLabel(" Key ID:");
        label.setBounds(55, 379, 46, 14);
        contentPanel.add(label);
    }
    {
        textField_14 = new JTextField();
        textField_14.setColumns(10);
        textField_14.setBounds(97, 432, 482, 20);
        contentPanel.add(textField_14);
    }
    {
        textField_15 = new JTextField();
        textField_15.setColumns(10);
        textField_15.setBounds(97, 457, 482, 20);
        contentPanel.add(textField_15);
    }
    {
        JLabel label = new JLabel("Verification Code:");
        label.setBounds(10, 460, 85, 17);
        contentPanel.add(label);
    }
    {
        JLabel label = new JLabel(" Key ID:");
        label.setBounds(55, 435, 46, 14);
        contentPanel.add(label);
    }
    {
        textField_16 = new JTextField();
        textField_16.setColumns(10);
        textField_16.setBounds(97, 488, 482, 20);
        contentPanel.add(textField_16);
    }
    {
        textField_17 = new JTextField();
        textField_17.setColumns(10);
        textField_17.setBounds(97, 513, 482, 20);
        contentPanel.add(textField_17);
    }
    {
        JLabel label = new JLabel("Verification Code:");
        label.setBounds(10, 516, 85, 17);
        contentPanel.add(label);
    }
    {
        JLabel label = new JLabel(" Key ID:");
        label.setBounds(55, 491, 46, 14);
        contentPanel.add(label);
    }
    {
        textField_18 = new JTextField();
        textField_18.setColumns(10);
        textField_18.setBounds(97, 544, 482, 20);
        contentPanel.add(textField_18);
    }
    {
        textField_19 = new JTextField();
        textField_19.setColumns(10);
        textField_19.setBounds(97, 569, 482, 20);
        contentPanel.add(textField_19);
    }
    {
        JLabel label = new JLabel("Verification Code:");
        label.setBounds(10, 572, 85, 17);
        contentPanel.add(label);
    }
    {
        JLabel label = new JLabel(" Key ID:");
        label.setBounds(55, 547, 46, 14);
        contentPanel.add(label);
    }
    {
        textField_20 = new JTextField();
        textField_20.setColumns(10);
        textField_20.setBounds(97, 600, 482, 20);
        contentPanel.add(textField_20);
    }
    {
        textField_21 = new JTextField();
        textField_21.setColumns(10);
        textField_21.setBounds(97, 625, 482, 20);
        contentPanel.add(textField_21);
    }
    {
        JLabel label = new JLabel("Verification Code:");
        label.setBounds(10, 628, 85, 17);
        contentPanel.add(label);
    }
    {
        JLabel label = new JLabel(" Key ID:");
        label.setBounds(55, 603, 46, 14);
        contentPanel.add(label);
    }
    {
        textField_22 = new JTextField();
        textField_22.setColumns(10);
        textField_22.setBounds(97, 656, 482, 20);
        contentPanel.add(textField_22);
    }
    {
        textField_23 = new JTextField();
        textField_23.setColumns(10);
        textField_23.setBounds(97, 681, 482, 20);
        contentPanel.add(textField_23);
    }
    {
        JLabel label = new JLabel("Verification Code:");
        label.setBounds(10, 684, 85, 17);
        contentPanel.add(label);
    }
    {
        JLabel label = new JLabel(" Key ID:");
        label.setBounds(55, 659, 46, 14);
        contentPanel.add(label);
    }
    {
        JButton btnVerify = new JButton("Verify");
        btnVerify.setBounds(337, 712, 89, 23);
        contentPanel.add(btnVerify);
    }
    {
        JButton btnSave = new JButton("Save");
        btnSave.setBounds(451, 712, 89, 23);
        contentPanel.add(btnSave);
    }
    {
        JPanel buttonPane = new JPanel();
        buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
        getContentPane().add(buttonPane, BorderLayout.SOUTH);
    }
}

public static void showConfirmationDialog(boolean b) {
    // TODO Auto-generated method stub

}

我习惯于剪切和粘贴宏的VB代码,但Java invirontment的行为不一样。尝试修改和插入示例中的不同代码并没有奏效。

1 个答案:

答案 0 :(得分:0)

请您替换FrontPage课程中的以下代码段:

btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
              ApiKeyEntryDialog.setVisible(true);
        }
    });

通过以下代码片段:

btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ApiKeyEntryDialog dialog = new ApiKeyEntryDialog();
                dialog.setVisible(true);
            }
        });

并告诉我结果?