我尝试查看有关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中的示例代码?我假设在此情况下,单击鼠标左键时该按钮应做标签;正确吗?
答案 0 :(得分: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();
}