试图使用Eclipse WindowBuilder,但是ActionLister却没有做任何事情

问题描述 投票:0回答:1

我尝试查看有关WindowBuilder的5种不同的教程,虽然我可以在测试代码时看到一个应用程序窗口,但是我无法使这些按钮起作用。这是我根据所看过的教程编写的代码。

package guiTest;

import java.awt.EventQueue;

import javax.swing.JFrame;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import java.awt.GridBagConstraints;
import javax.swing.JLabel;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class GuiTest {

    private JFrame frame;

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

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

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        GridBagLayout gridBagLayout = new GridBagLayout();
        gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0, 0};
        gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0};
        gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
        gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
        frame.getContentPane().setLayout(gridBagLayout);

        JButton btnLabelAdd = new JButton("Label Add");
        GridBagConstraints gbc_btnLabelAdd = new GridBagConstraints();
        gbc_btnLabelAdd.insets = new Insets(0, 0, 5, 0);
        gbc_btnLabelAdd.gridx = 6;
        gbc_btnLabelAdd.gridy = 1;
        frame.getContentPane().add(btnLabelAdd, gbc_btnLabelAdd);
        btnLabelAdd.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                makeLabel(evt);
            }
        });
    }

    private void makeLabel(ActionEvent evt) {
        JLabel lblHereLabel = new JLabel("Here Label");
        GridBagConstraints gbc_lblHereLabel = new GridBagConstraints();
        gbc_lblHereLabel.gridx = 6;
        gbc_lblHereLabel.gridy = 4;
        frame.getContentPane().add(lblHereLabel, gbc_lblHereLabel);
    }
}

有人有任何技巧可以使它正常工作吗?还是一些我可以用来确保问题不在Eclipse中的示例代码?我假设在此情况下,单击鼠标左键时该按钮应做标签;正确吗?

java eclipse user-interface jbutton windowbuilder
1个回答
1
投票

您的按钮可以正常工作。您可以在调试器中观看代码跳转到makeLabel()的情况。您只是没有使新标签可见。

如@MadProgrammer所建议,一旦添加了新标签,您就可以通过调用Container告诉封装的revalidate()对其进行自我更新,然后通过repaint()对其进行重绘:

private void makeLabel(ActionEvent evt) {
    JLabel lblHereLabel = new JLabel("Here Label");
    GridBagConstraints gbc_lblHereLabel = new GridBagConstraints();
    gbc_lblHereLabel.gridx = 6;
    gbc_lblHereLabel.gridy = 4;
    frame.getContentPane().add(lblHereLabel, gbc_lblHereLabel);
    frame.getContentPane().revalidate();
    frame.getContentPane().repaint();
}

这样做意味着您已经摆脱了直接访问frame的麻烦。这意味着您可以通过查询触发事件所属的Container的按钮并将其添加为新标签,从而将处理程序重写为“与帧无关”,就像这样:

private void makeLabel(ActionEvent evt) {
    JLabel lblHereLabel = new JLabel("Here Label");
    GridBagConstraints gbc_lblHereLabel = new GridBagConstraints();
    gbc_lblHereLabel.gridx = 6;
    gbc_lblHereLabel.gridy = 4;

    Container displayArea = ((JButton) evt.getSource()).getParent();
    displayArea.add(lblHereLabel, gbc_lblHereLabel);
    displayArea.revalidate();
    displayArea.repaint();
}
© www.soinside.com 2019 - 2024. All rights reserved.