我正在尝试让这个GUI显示并接受用户输入以在单独的类中执行计算,但是当我运行程序时它运行并说构建成功但没有任何反应。我想我需要创建一些类的实例,但我已经尝试将它放在多个位置,似乎没有任何工作。这是我的代码:
class infoGUI extends JFrame {
private JButton btnOK = new JButton("OK");
private JButton btnCancel = new JButton("Cancel");
private JTextField txtCustomerName = new JTextField();
private JTextField txtCustomerNumber = new JTextField();
private JTextField txtCreditLimit = new JTextField();
private JTextField txtPrevBalance = new JTextField();
private JTextField txtCurrentPurchases = new JTextField();
private JTextField txtPayments = new JTextField();
private JTextField txtCreditsReturns = new JTextField();
private JTextField txtLatefeeds = new JTextField();
private JLabel lblCustomerName = new JLabel("Name :");
private JLabel lblCustomerNumber = new JLabel("Number :");
private JLabel lblCreditLimit = new JLabel("Credit Limit :");
private JLabel lblPrevbalance = new JLabel("Previous Balance :");
private JLabel lblCurrentPurchases = new JLabel("Current Purchases :");
private JLabel lblPayments = new JLabel("Payments :");
private JLabel lblCreditsReturns = new JLabel("Credits/Returns :");
private JLabel lblLateFees = new JLabel("Late Fees :");
public infoGUI(){
this.name = null;
setTitle("Credit Wizard");
setSize(300,375);
setLayout(null);
setResizable(false);
initComponent();
initEvent();
}
private void initComponent(){
btnOK.setBounds(20,300, 80,25);
btnCancel.setBounds(110,300, 80,25);
txtCustomerName.setBounds(100,10,100,20);
txtCustomerNumber.setBounds(100,35,100,20);
txtCreditLimit.setBounds(100,65,100,20);
txtPrevBalance.setBounds(130,95,100,20);
txtCurrentPurchases.setBounds(135,125,100,20);
txtPayments.setBounds(100,155,100,20);
txtCreditsReturns.setBounds(120,185,100,20);
txtLatefeeds.setBounds(100,215,100,20);
lblCustomerName.setBounds(20,10,100,20);
lblCustomerNumber.setBounds(20,35,100,20);
lblCreditLimit.setBounds(20,65,100,20);
lblPrevbalance.setBounds(20,95,120,20);
lblCurrentPurchases.setBounds(20,125,125,20);
lblPayments.setBounds(20,155,100,20);
lblCreditsReturns.setBounds(20,185,120,20);
lblLateFees.setBounds(20,215,100,20);
add(btnOK);
add(btnCancel);
add(lblCustomerName);
add(lblCustomerNumber);
add(lblCreditLimit);
add(lblPrevbalance);
add(lblCurrentPurchases);
add(lblPayments);
add(lblCreditsReturns);
add(lblLateFees);
add(txtCustomerName);
add(txtCustomerNumber);
add(txtCreditLimit);
add(txtPrevBalance);
add(txtCurrentPurchases);
add(txtPayments);
add(txtCreditsReturns);
add(txtLatefeeds);
}
private void initEvent(){
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(1);
}
});
btnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnCancelClick(e);
}
});
btnOK.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnOKClick(e);
}
});
}
private void btnCancelClick(ActionEvent evt){
System.exit(0);
}
private String name;
private double custNum;
private double creditLimit;
private double prevBalance;
private double currentPurchases;
private double payments;
private double creditsReturns;
private double lateFees;
private void btnOKClick(ActionEvent evt){
try{
String custName = (txtCustomerName.getText());
setName(custName);
custNum = Double.parseDouble(txtCustomerNumber.getText());
setCustNum(custNum);
creditLimit = Double.parseDouble(txtCreditLimit.getText());
setCreditLimit(creditLimit);
prevBalance = Double.parseDouble(txtPrevBalance.getText());
setPrevBalance(prevBalance);
currentPurchases = Double.parseDouble(txtPrevBalance.getText());
setCurrentPurchases(currentPurchases);
payments = Double.parseDouble(txtPayments.getText());
setPayments(payments);
creditsReturns = Double.parseDouble(txtCreditsReturns.getText());
setCreditsReturns(creditsReturns);
setLateFees(lateFees);
System.exit(0);
}catch(Exception e){
System.out.println(e);
JOptionPane.showMessageDialog(null,
e.toString(),
"Error",
JOptionPane.ERROR_MESSAGE);
}
}
public void setName(String newName){
name = newName;
}
public String getName(){
return name;
}
public void setCustNum(double newCustNum){
custNum = newCustNum;
}
public double getCustNum(){
return custNum;
}
public void setCreditLimit(double newCreditLimit){
creditLimit = newCreditLimit;
}
public double getCreditLimit(){
return creditLimit;
}
public void setPrevBalance(double newPrevBalance){
prevBalance = newPrevBalance;
}
public double getPrevBalance(){
return prevBalance;
}
public void setCurrentPurchases(double newCurrentPurchases){
currentPurchases = newCurrentPurchases;
}
public double getCurrentPurchases(){
return currentPurchases;
}
public void setPayments(double newPayments){
payments = newPayments;
}
public double getPayments(){
return payments;
}
public void setCreditsReturns(double newCreditsReturns){
creditsReturns = newCreditsReturns;
}
public double getCreditsReturns(){
return creditsReturns;
}
public void setLateFees(double newLateFees){
lateFees = newLateFees;
}
public double getLateFees(){
return lateFees;
}
感谢任何帮助
答案 0 :(得分:0)
您必须使用main
方法创建新的GUI。另外,在JFrame的构造函数末尾调用setVisible(true)
:
public static void main(String[] args){
new infoGUI(); // creates a new GUI
}
和
public infoGUI(){
super();
this.name = null;
setTitle("Credit Wizard");
setSize(300,375);
setLayout(null);
setResizable(false);
initComponent();
initEvent();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // good idea to put this in
setVisible(true);
}