JFrame将按钮点击变量传递给另一个类
假设我有两个类,如下所示。在我的第一个类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());
}
}
您应该在actionPerformed
方法中用Product p = product;
替代Product p = new Product();
行。
如果我删除该行,比我得到“p无法解析” – user3768997
上面的修复答案。 – kgeorgiy
当我试图做一个简单的getProductName打印我仍然没有得到任何东西。 – user3768997
Shrotly只是试图在葡萄酒课上工作,您必须更改您的代码,并在主类中删除'wine.get ....',因为葡萄酒类是空的或现在不工作!你需要打印从动作监听器方法(工作时)的价格或名称如下:
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == psale)
{
Wine w = wine;
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);
System.out.println("Name of wine : "+ wName +", "+"Quanty : "+ wQnty +", "+"Price : "+ wPrice);
}
}
}
我希望这帮助you.Good运气
如果'私有类saleButton'是按钮,你说的about.It的通常返回null,因为Overrided方法是空的! –
否则你需要从主类中得到“wine.name()”而不是从“Wine class” –