我在这个代码中遇到了各种各样的错误.我是把try/catch放在正确的位置,还是我的代码完全搞砸了



我正试图创建一个弹出窗口,用于跟踪银行账户的存款/提款。我已经正确地创建了窗口,但当我创建代码来计算这两个按钮时,我遇到了很多错误。我很确定我只是在错误的地方输入了代码,但我不确定如何修复它。这是我目前的代码。有人能向我解释一下我做错了什么吗?


import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class acmeBank {
private JFrame frame;
private JTextField creditInput;
private JTextField debitInput;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
acmeBank window = new acmeBank();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public acmeBank() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {

double balance = 0;

frame = new JFrame();
frame.setBounds(100, 100, 470, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JLabel lblNewLabel = new JLabel("DEPOSIT");
lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 14));

JLabel lblNewLabel_1 = new JLabel("WITHDRAWAL");
lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 14));

creditInput = new JTextField();
creditInput.setColumns(10);

debitInput = new JTextField();
debitInput.setColumns(10);

JButton creditButton = new JButton("CREDIT");
creditButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});

JButton debitButton = new JButton("DEBIT");
debitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});

JButton clearButton = new JButton("CLEAR");
clearButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});

JLabel lblNewLabel_2 = new JLabel("BALANCE    $");
lblNewLabel_2.setFont(new Font("Tahoma", Font.PLAIN, 14));

JLabel totalBalance = new JLabel("");
GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING, false)
.addGroup(groupLayout.createSequentialGroup()
.addGap(47)
.addComponent(lblNewLabel_2)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(totalBalance, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
.addGap(18)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(lblNewLabel, Alignment.TRAILING)
.addComponent(lblNewLabel_1, Alignment.TRAILING))
.addGap(0)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
.addComponent(debitInput, Alignment.TRAILING)
.addComponent(creditInput, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE))))
.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(debitButton, GroupLayout.PREFERRED_SIZE, 76, GroupLayout.PREFERRED_SIZE)
.addComponent(creditButton, GroupLayout.PREFERRED_SIZE, 80, GroupLayout.PREFERRED_SIZE))
.addGap(18)
.addComponent(clearButton, GroupLayout.PREFERRED_SIZE, 78, GroupLayout.PREFERRED_SIZE)
.addContainerGap(39, Short.MAX_VALUE))
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(23)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(lblNewLabel)
.addComponent(creditInput, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(creditButton))
.addGap(11)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(lblNewLabel_1)
.addComponent(debitInput, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(debitButton)))
.addGroup(groupLayout.createSequentialGroup()
.addGap(40)
.addComponent(clearButton)))
.addGap(33)
.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
.addComponent(lblNewLabel_2)
.addComponent(totalBalance, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE))
.addContainerGap(122, Short.MAX_VALUE))
);
frame.getContentPane().setLayout(groupLayout);

public void actionPerformed(ActionEvent e) {
if (e.getSource() == creditButton) {
try {

double amount = Double.parseDouble(creditInput.getText());
balance += amount;
totalBalance.setText("$" + balance);

} catch (NumberFormatException ee) {

JOptionPane.showMessageDialog(f, "Please enter only numbers");
}
} else if (e.getSource() == debitButton) {
try {

double amount = Double.parseDouble(debitInput.getText());
balance -= amount;
totalBalance.setText("$" + balance);

} catch (NumberFormatException ee) {

JOptionPane.showMessageDialog(f, "Please enter only numbers");
}
} else if (e.getSource() == clearButton) {

debitInput.setText("");
creditInput.setText("");
}
}

public static void main(String[] args) {
new ACMEBank();
}
}
}


}

小心表单编辑器,即使是经验丰富的开发人员也可以毫不费力地将原本简单的界面/代码搞砸。他们不会教你脱钩和单一责任之类的东西,很快,你就可以有一个";上帝;完全失控的类。

一般来说,表单编辑器生成的代码也很难更新、维护和修复,最好不要这样做。

初级";发行";事实上,您正试图在方法中声明方法,Java不允许您这样做。

";关联的";问题归结为您试图访问的变量是在本地声明的(使用initialize方法(,如果您想访问它们,您应该将它们声明为实例变量。

话虽如此,如果你利用ActionEvent#getActionCommand,你就不需要了(就我个人而言,我使用匿名类,但我认为这现在有点超出了你的能力,而且来到这样的QA网站也是一种危险,我们不会考虑你目前的"学习"水平,只会向你扔各种"酷"的东西,这实际上不会帮助你成为一个更好的开发人员,只是说(

以下基本上是您的代码的手写版本。。。

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class ACMEBank {
public static void main(String[] args) {
new ACMEBank();
}
public ACMEBank() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel implements ActionListener {
private JTextField depositField;
private JTextField withdrawField;
private JLabel balanceLabel;
private double balance;
public TestPane() {
setBorder(new EmptyBorder(16, 16, 16, 16));
setLayout(new GridBagLayout());
depositField = new JTextField(10);
withdrawField = new JTextField(10);
balanceLabel = new JLabel("0.00");
JButton creditBtn = new JButton("Credit");
JButton debitBtn = new JButton("Debit");
JButton clearBtn = new JButton("Clear");
JPanel inputPane = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.LINE_END;
gbc.insets = new Insets(4, 4, 4, 4);
inputPane.add(new JLabel("Deposit"), gbc);
gbc.gridy++;
inputPane.add(new JLabel("Widthdraw"), gbc);
gbc.gridx++;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.LINE_START;
inputPane.add(depositField, gbc);
gbc.gridy++;
inputPane.add(withdrawField, gbc);
gbc.gridx++;
gbc.gridy = 0;
inputPane.add(creditBtn, gbc);
gbc.gridy++;
inputPane.add(debitBtn, gbc);
JPanel balancePane = new JPanel(new GridBagLayout());
gbc = new GridBagConstraints();
gbc.insets = new Insets(4, 4, 4, 4);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.LINE_START;
balancePane.add(new JLabel("Balance $"), gbc);
gbc.gridx++;
gbc.weightx = 1;
balancePane.add(balanceLabel, gbc);
gbc = new GridBagConstraints();
gbc.insets = new Insets(4, 4, 4, 4);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.HORIZONTAL;
add(inputPane, gbc);
gbc.gridy++;
gbc.anchor = GridBagConstraints.LINE_START;
add(balancePane, gbc);
gbc.fill = GridBagConstraints.NONE;
gbc.gridy = 0;
gbc.gridx++;
add(clearBtn, gbc);
// I'd prefer to use anoymouse class here to isoldate the functionality
// and make it easier to deal with, but that's not how the originla code
// works
debitBtn.addActionListener(this);
creditBtn.addActionListener(this);
clearBtn.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if ("credit".equalsIgnoreCase(e.getActionCommand())) {
try {
double amount = Double.parseDouble(depositField.getText());
balance += amount;
balanceLabel.setText("$" + balance);
} catch (NumberFormatException ee) {
JOptionPane.showMessageDialog(this, "Please enter only numbers");
}
} else if ("debit".equalsIgnoreCase(e.getActionCommand())) {
try {
double amount = Double.parseDouble(withdrawField.getText());
balance -= amount;
balanceLabel.setText("$" + balance);
} catch (NumberFormatException ee) {
JOptionPane.showMessageDialog(this, "Please enter only numbers");
}
} else if ("clear".equalsIgnoreCase(e.getActionCommand())) {
withdrawField.setText("");
depositField.setText("");
}
}
}
}