在 Jbutton 单击上创建对象
Create object on Jbutton click
我试图在用户按下 button.So 时创建一个对象,我已经想出了下面的实现,但似乎 work.I 没有处理过完全没有 Swing 和 Java UI,所以我猜这可能是一个业余错误。
我要创建的对象来自另一种类型,称为 DebitCard。
private JFrame frame;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GenerateCard window = new GenerateCard();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public GenerateCard() {
}
{
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton btnNewButton = new JButton("Generate card");
btnNewButton.setBounds(112, 213, 216, 41);
frame.getContentPane().add(btnNewButton);
}
private class buttonEvent implements ActionListener {
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals("Generate card")) {
DebitCard a = new DebitCard();
}
}
}
根据您的可用代码,您似乎忘记了使用 btnNewButton
注册 buttonEvent
btnNewButton.addActionListener(new buttonEvent());
你可能想仔细看看:
我试图在用户按下 button.So 时创建一个对象,我已经想出了下面的实现,但似乎 work.I 没有处理过完全没有 Swing 和 Java UI,所以我猜这可能是一个业余错误。
我要创建的对象来自另一种类型,称为 DebitCard。
private JFrame frame;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GenerateCard window = new GenerateCard();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public GenerateCard() {
}
{
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton btnNewButton = new JButton("Generate card");
btnNewButton.setBounds(112, 213, 216, 41);
frame.getContentPane().add(btnNewButton);
}
private class buttonEvent implements ActionListener {
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals("Generate card")) {
DebitCard a = new DebitCard();
}
}
}
根据您的可用代码,您似乎忘记了使用 btnNewButton
buttonEvent
btnNewButton.addActionListener(new buttonEvent());
你可能想仔细看看: