gpt4 book ai didi

java - 尝试使用 Eclipse WindowBuilder,但 ActionLister 没有执行任何操作

转载 作者:行者123 更新时间:2023-12-02 09:16:33 24 4
gpt4 key购买 nike

我已尝试查看大约 5 个不同的 WindowBuilder 教程,虽然在测试代码时可以显示应用程序窗口,但无法使按钮正常工作。这是我根据我看过的教程编写的代码。

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?我在这里假设按钮应该在左键单击时制作标签;这是正确的吗?

最佳答案

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

正如 @MadProgrammer 所建议的,添加新标签后,您可以通过调用 revalidate() 告诉封闭的 Container 进行自身更新,然后 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();
}

关于java - 尝试使用 Eclipse WindowBuilder,但 ActionLister 没有执行任何操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58943159/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com