假設我有兩個類,如下所示。在我的第一個類Product中,我有產品實例變量,而在第二個類中,我有我的JFrame組件。現在我試圖從JTextField titemName獲取值,並在點擊按鈕時將其賦值給setProductName?JFrame將按鈕點擊變量傳遞給另一個類
有人請給我一個關於如何做的提示嗎?到現在爲止,我得到getWineName的null calue,因爲setWineName超出了範圍,我猜?
第一類:
public class Wine {
private String wineName;
private double winePrice;
private int wineQnty;
public Wine(String wineName, double winePrice, int wineQnty)
{
this.wineName = wineName;
this.winePrice = winePrice;
this.wineQnty = wineQnty;
}
public Wine()
{
this ("", 0, 0);
}
// Getters (accessor methods) and setters(modifier methods)
public String getWineName() {
return wineName;
}
public void setWineName(String wineName) {
this.wineName = wineName;
}
public double getWinePrice() {
return winePrice;
}
public void setWinePrice(double winePrice) {
this.winePrice = winePrice;
}
public int getWineQnty() {
return wineQnty;
}
public void setWineQnty(int wineQnty) {
this.wineQnty = wineQnty;
}
}
二等:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LWMGUI extends JFrame
implements ActionListener
{
private Wine wine;
private CustomerAccount custAcct;
private JButton psale, preturn;
private JLabel litemName, litemQuantity, litemPrice, ltransAmount, lcurrBalance;
private JTextField titemName, titemQuantity, titemPrice, ttransAmount, tcurrBalance;
public LWMGUI(Wine w, CustomerAccount c)
{
wine = w;
custAcct = c;
// Creating Dialog box to get customer's name
String custName = JOptionPane.showInputDialog("Please enter the customer name: ");
if (custName.isEmpty() || custName == null){
System.exit(0);
}
//Creating dialog box to get the current balance for the given customer
String currBalanceTxt;
while (true){
currBalanceTxt = JOptionPane.showInputDialog("Please enter the current balance for this customer: ");
try {
Double currBalance = Double.parseDouble(currBalanceTxt);
break;
} catch (NumberFormatException ex)
{
JOptionPane.showMessageDialog(null, "Please enter a numeric value!", "Non-numeric value error", JOptionPane.ERROR_MESSAGE);
}
}
Double currBalance = Double.parseDouble(currBalanceTxt);
//Setting customer account values
c.setAccountName(custName);
c.setCurrBalance(currBalance);
setTitle("Lilybank Wine Merchants: " + custAcct.getAccountName());
setSize(560, 150);
setLocation(200, 200);
createTop();
createButtons();
createBottom();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
private void createTop() {
litemName = new JLabel("Name:");
litemQuantity = new JLabel("Quantity:");
litemPrice = new JLabel("Price: £");
titemName = new JTextField(15);
titemName.addActionListener(this);
titemQuantity = new JTextField(6);
titemQuantity.addActionListener(this);
titemPrice = new JTextField(7);
titemPrice.addActionListener(this);
JPanel topPan = new JPanel();
topPan.add(litemName);
topPan.add(titemName);
topPan.add(litemQuantity);
topPan.add(titemQuantity);
topPan.add(litemPrice);
topPan.add(titemPrice);
add(topPan, "North");
}
private void createBottom() {
ltransAmount = new JLabel("Amount of Transaction:");
lcurrBalance = new JLabel("Current balance:");
ttransAmount = new JTextField(6);
tcurrBalance = new JTextField(6);
ttransAmount.setEnabled(false);
tcurrBalance.setEnabled(false);
JPanel lowerPan = new JPanel();
lowerPan.add(ltransAmount);
lowerPan.add(ttransAmount);
lowerPan.add(lcurrBalance);
lowerPan.add(tcurrBalance);
add(lowerPan, "South");
}
private void createButtons() {
// Setting up buttons
psale = new JButton("Process Sale");
preturn = new JButton("Process Return");
psale.addActionListener(this);
preturn.addActionListener(this);
JPanel buttonPan = new JPanel();
buttonPan.add(psale);
buttonPan.add(preturn);
add(buttonPan, "Center");
}
private class saleButton implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == psale)
{
Wine w = wine;
CustomerAccount ct = custAcct;
System.out.println("Sale button pressed!");
String wName = titemName.getText();
int wQnty = Integer.parseInt(titemQuantity.getText());
Double wPrice = Double.parseDouble(titemPrice.getText());
w.setWineName(wName);
w.setWineQnty(wQnty);
w.setWinePrice(wPrice);
String saleAmount = Double.toString(ct.sale());
System.out.println(saleAmount);
ttransAmount.setText(saleAmount);
}
}
}
第三類:
import javax.swing.JFrame;
公共類MainCl {
public static void main(String[] arg)
{
Wine wine = new Wine();
CustomerAccount cuAcct = new CustomerAccount();
LWMGUI g = new LWMGUI(wine, cuAcct);
System.out.println(wine.getWineName());
System.out.println(wine.getWinePrice());
}
}
如果'私有類saleButton'是按鈕,你說的about.It的通常返回null,因爲Overrided方法是空的! –
否則你需要從主類中得到「wine.name()」而不是從「Wine class」 –