无法在 gridbaglayout 中添加动作侦听器
Unable to add action listener in gridbaglayout
这是我的代码,如何添加动作侦听器我已经尝试过已知的方法,但由于静态方法我无法添加 it.Netbeans IDE 建议在程序中添加动作侦听器,但我我无法使用它有人可以建议一个更简单的方法来声明动作侦听器。
public class 计算 {
public static void addComponentsToPane(Container pane) {
pane.setLayout(new GridBagLayout());
GridBagConstraints gBC = new GridBagConstraints();
gBC.ipady = 40;
gBC.ipadx = 40;
JTextField JTextField = new JTextField("Hello");
gBC.fill = GridBagConstraints.HORIZONTAL;
gBC.gridx = 0;
gBC.gridy = 0;
gBC.gridwidth = 4;
JTextField.setEditable(false);
pane.add(JTextField, gBC);
//JButton jbnButton;
gBC.gridwidth = 1;
JButton b7 = new JButton("7");
gBC.gridx = 0;
gBC.gridy = 1;
pane.add(b7, gBC);
// more calculator buttons declared here
//这里是提示actionlistner的方式
b0.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
b0ActionPerformed(evt);
}
private void b0ActionPerformed(ActionEvent evt) }
`
});
b0.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b0ActionPerformed(evt);
}
private void b0ActionPerformed(Actionenter Event evt) {}
});}
private static void createAndShowGUI() {}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
我帮你试试这个。您可以在 addComponentsToPane 方法中为您声明的每个组件添加动作侦听器。另外,你忘了传递框架,所以你可以像我一样在 main 方法中传递它。
public class Calc {
public static void addComponentsToPane(Container pane) {
pane.setLayout(new GridBagLayout());
GridBagConstraints gBC = new GridBagConstraints();
gBC.ipady = 40;
gBC.ipadx = 40;
JTextField JTextField = new JTextField("Hello");
gBC.fill = GridBagConstraints.HORIZONTAL;
gBC.gridx = 0;
gBC.gridy = 0;
gBC.gridwidth = 4;
JTextField.setEditable(false);
pane.add(JTextField, gBC);
//JButton jbnButton;
gBC.gridwidth = 1;
JButton b7 = new JButton("7");
gBC.gridx = 0;
gBC.gridy = 1;
pane.add(b7, gBC);
// more calculator buttons declared here
b7.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Write your action here
}
});
}
private static void createAndShowGUI(JFrame pane) {
addComponentsToPane(pane);
pane.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI(new JFrame());
}
});
}
}
这是我的代码,如何添加动作侦听器我已经尝试过已知的方法,但由于静态方法我无法添加 it.Netbeans IDE 建议在程序中添加动作侦听器,但我我无法使用它有人可以建议一个更简单的方法来声明动作侦听器。
public class 计算 {
public static void addComponentsToPane(Container pane) {
pane.setLayout(new GridBagLayout());
GridBagConstraints gBC = new GridBagConstraints();
gBC.ipady = 40;
gBC.ipadx = 40;
JTextField JTextField = new JTextField("Hello");
gBC.fill = GridBagConstraints.HORIZONTAL;
gBC.gridx = 0;
gBC.gridy = 0;
gBC.gridwidth = 4;
JTextField.setEditable(false);
pane.add(JTextField, gBC);
//JButton jbnButton;
gBC.gridwidth = 1;
JButton b7 = new JButton("7");
gBC.gridx = 0;
gBC.gridy = 1;
pane.add(b7, gBC);
// more calculator buttons declared here
//这里是提示actionlistner的方式
b0.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
b0ActionPerformed(evt);
}
private void b0ActionPerformed(ActionEvent evt) }
`
});
b0.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b0ActionPerformed(evt);
}
private void b0ActionPerformed(Actionenter Event evt) {}
});}
private static void createAndShowGUI() {}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
我帮你试试这个。您可以在 addComponentsToPane 方法中为您声明的每个组件添加动作侦听器。另外,你忘了传递框架,所以你可以像我一样在 main 方法中传递它。
public class Calc {
public static void addComponentsToPane(Container pane) {
pane.setLayout(new GridBagLayout());
GridBagConstraints gBC = new GridBagConstraints();
gBC.ipady = 40;
gBC.ipadx = 40;
JTextField JTextField = new JTextField("Hello");
gBC.fill = GridBagConstraints.HORIZONTAL;
gBC.gridx = 0;
gBC.gridy = 0;
gBC.gridwidth = 4;
JTextField.setEditable(false);
pane.add(JTextField, gBC);
//JButton jbnButton;
gBC.gridwidth = 1;
JButton b7 = new JButton("7");
gBC.gridx = 0;
gBC.gridy = 1;
pane.add(b7, gBC);
// more calculator buttons declared here
b7.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Write your action here
}
});
}
private static void createAndShowGUI(JFrame pane) {
addComponentsToPane(pane);
pane.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI(new JFrame());
}
});
}
}