将数据从gui类获取到另一个类
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将数据从gui类获取到另一个类相关的知识,希望对你有一定的参考价值。
我有这个项目应该从GUI获取信息并执行计算,但似乎数据只返回null
值,我很难理解如何定制setter和getter使用GUI。这是我的GUI类:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
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 name = (txtCustomerName.getText());
custNum = Double.parseDouble(txtCustomerNumber.getText());
creditLimit = Double.parseDouble(txtCreditLimit.getText());
prevBalance = Double.parseDouble(txtPrevBalance.getText());
currentPurchases = Double.parseDouble(txtPrevBalance.getText());
payments = Double.parseDouble(txtPayments.getText());
creditsReturns = Double.parseDouble(txtCreditsReturns.getText());
lateFees = Double.parseDouble(txtLatefeeds.getText());
creditInfoSystem calc = new creditInfoSystem();
calc.buttonClick();
System.exit(0);
} catch (Exception e) {
System.out.println(e);
JOptionPane.showMessageDialog(null,
e.toString(),
"Error",
JOptionPane.ERROR_MESSAGE);
}
}
public void creditFields(String newName, double newCustNum, double newCreditLimit,
double newPrevBalance, double newCurrentPurchases, double newPayments,
double newCreditsReturns, double newLateFees) {
setName(newName);
setCustNum(newCustNum);
setCreditLimit(newCreditLimit);
setPrevBalance(newPrevBalance);
setCurrentPurchases(newCurrentPurchases);
setPayments(newPayments);
setCreditsReturns(newCreditsReturns);
setLateFees(newLateFees);
}
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;
}
}
而我的主要课程:
public class CreditInfoSystem {
public static void main(String[] args) {
InfoGUI f = new InfoGUI();
f.setVisible(true);
}
public static void buttonClick() {
InfoGUI get = new InfoGUI();
String custName = get.getName();
double custNum = get.getCustNum();
double creditLimit = get.getCreditLimit();
double prevBalance = get.getPrevBalance();
double currentPurchases = get.getCurrentPurchases();
double payments = get.getPayments();
double creditsReturns = get.getCreditsReturns();
double lateFees = get.getLateFees();
System.out.println(custName);
}
}
任何帮助表示赞赏!
答案
正如评论中所写,理解类和实例是什么似乎存在问题。我尝试修复你的代码:import javax.swing。*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent;
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 name = (txtCustomerName.getText());
custNum = Double.parseDouble(txtCustomerNumber.getText());
creditLimit = Double.parseDouble(txtCreditLimit.getText());
prevBalance = Double.parseDouble(txtPrevBalance.getText());
currentPurchases = Double.parseDouble(txtPrevBalance.getText());
payments = Double.parseDouble(txtPayments.getText());
creditsReturns = Double.parseDouble(txtCreditsReturns.getText());
lateFees = Double.parseDouble(txtLatefeeds.getText());
// better: Do not pass this but only the values which are needed
creditInfoSystem.doFancyCalulations(this);
// Why
System.exit(0);
} catch (Exception e) {
System.out.println(e);
JOptionPane.showMessageDialog(null,
e.toString(),
"Error",
JOptionPane.ERROR_MESSAGE);
}
}
public void creditFields(String newName, double newCustNum, double
newCreditLimit,
double newPrevBalance, double newCurrentPurchases, double newPayments,
double newCreditsReturns, double newLateFees) {
setName(newName);
setCustNum(newCustNum);
setCreditLimit(newCreditLimit);
setPrevBalance(newPrevBalance);
setCurrentPurchases(newCurrentPurchases);
setPayments(newPayments);
setCreditsReturns(newCreditsReturns);
setLateFees(newLateFees);
}
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;
}
}
而不是以后
public class CreditInfoSystem {
public static void main(String[] args) {
InfoGUI f = new InfoGUI();
f.setVisible(true);
}
public static void doFancyCalculations(InfoGUI get){
String custName = get.getName();
double custNum = get.getCustNum();
double creditLimit = get.getCreditLimit();
double prevBalance = get.getPrevBalance();
double currentPurchases = get.getCurrentPurchases();
double payments = get.getPayments();
double creditsReturns = get.getCreditsReturns();
double lateFees = get.getLateFees();
System.out.println(custName);
}
}
我在这里做的基本修复是将GUI的实例传递给执行业务逻辑的方法。这实际上不是最好的风格。也许你应该看一下MVC模式。
以上是关于将数据从gui类获取到另一个类的主要内容,如果未能解决你的问题,请参考以下文章