[英]JFrame pass variable from button click to another class
假設我有兩個類,如下所示。 在名為Product的第一個類中,我具有產品實例變量,而在第二個類中,我具有JFrame組件。 現在,我想從JTextField titemName中獲取值,然后在單擊按鈕時將其分配給setProductName嗎?
有人可以給我提示如何做嗎? 到現在為止,我對getWineName的使用是無效的,因為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());
}
}
您應該替換行Product p = new Product();
按Product p = product;
在actionPerformed
方法中。
Shrotly只是嘗試在wine類上工作,您必須更改代碼,並在主類中刪除“ wine.get ....”,因為wine類為空或現在不起作用! 您需要像這樣在操作偵聽器方法中打印價格或名稱:
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);
}
}
}
希望對您有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.