gpt4 book ai didi

Java sale 从 main 方法运行,而不是作为 java beans

转载 作者:太空宇宙 更新时间:2023-11-04 07:15:32 24 4
gpt4 key购买 nike

This is how it should look when I enter the information, and after I click on "Calc" button, I should get the Results of the calculation and show it in msgLbl![][1]如果我想在主方法或作为应用程序而不是作为 java beans 运行销售,我应该在主方法中添加什么???,因为现在作为 java beans 运行它工作正常,但是当我作为应用程序运行它时,什么也没有发生,因为我需要在 main 方法中添加一些代码。

import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.Dimension;
import java.awt.Label;
import java.awt.Rectangle;
import java.awt.Point;
import java.awt.Button;

import java.awt.TextField;

public class Sale extends Frame implements ActionListener, WindowListener {

private static final long serialVersionUID = 1L;
private Label custNameLbl = null;
private Label itemNameLbl = null;
private Label qtyLbl = null;
private Label priceLbl = null;
private Label msgLbl = null;
private Button button = null;
private TextField custNameTF = null;
private TextField itemNameTF = null;
private TextField qtyTF = null;
private TextField priceTF = null;

@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub

}

@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub

}

@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub

}

@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub

}

@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub

}

@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub

}

@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub

}

@Override
public void actionPerformed(ActionEvent e) {
int qty;
double price;
double cost;
qty = Integer.parseInt(qtyTF.getText());
price = Double.parseDouble(priceTF.getText());
cost = price * qty * 1.065;
this.add(msgLbl, null);
msgLbl.setText("The cost of this " + "transaction is: $" + cost);
custNameTF.setText("");
itemNameTF.setText("");
qtyTF.setText("");
priceTF.setText("");

// System.out.println(cost);

// TODO Auto-generated method stub

}

/**
* This method initializes button
*
* @return java.awt.Button
*/
private Button getButton() {
if (button == null) {
button = new Button();
button.setLocation(new Point(126, 198));
button.setLabel("Calc");
button.setSize(new Dimension(40, 23));
button.addActionListener(this);
}
return button;
}

/**
* This method initializes custNameTF
*
* @return java.awt.TextField
*/
private TextField getCustNameTF() {
if (custNameTF == null) {
custNameTF = new TextField();
custNameTF.setBounds(new Rectangle(116, 42, 140, 23));
}
return custNameTF;
}

/**
* This method initializes itemNameTF
*
* @return java.awt.TextField
*/
private TextField getItemNameTF() {
if (itemNameTF == null) {
itemNameTF = new TextField();
itemNameTF.setBounds(new Rectangle(116, 77, 137, 23));
}
return itemNameTF;
}

/**
* This method initializes qtyTF
*
* @return java.awt.TextField
*/
private TextField getQtyTF() {
if (qtyTF == null) {
qtyTF = new TextField();
qtyTF.setBounds(new Rectangle(95, 114, 56, 23));
}
return qtyTF;
}

/**
* This method initializes priceTF
*
* @return java.awt.TextField
*/
private TextField getPriceTF() {
if (priceTF == null) {
priceTF = new TextField();
priceTF.setBounds(new Rectangle(203, 114, 49, 23));
}
return priceTF;
}

/**
* I need to run sale from main method or in other words pass the values to
* msgLbl
*/
public static void main(String[] args) {
Sale saleTest = new Sale();

// TODO Auto-generated method stub

}

/**
* This is the default constructor
*/
public Sale() {
super();
initialize();
}

/**
* This method initializes this
*
* @return void
*/
private void initialize() {
msgLbl = new Label();
msgLbl.setText("Label");
msgLbl.setSize(new Dimension(290, 23));
msgLbl.setAlignment(Label.CENTER);
msgLbl.setLocation(new Point(4, 170));
priceLbl = new Label();
priceLbl.setBounds(new Rectangle(161, 114, 36, 23));
priceLbl.setAlignment(Label.RIGHT);
priceLbl.setText("Price");
qtyLbl = new Label();
qtyLbl.setBounds(new Rectangle(35, 114, 51, 23));
qtyLbl.setAlignment(Label.RIGHT);
qtyLbl.setText("Quantity");
itemNameLbl = new Label();
itemNameLbl.setBounds(new Rectangle(34, 77, 72, 23));
itemNameLbl.setAlignment(Label.RIGHT);
itemNameLbl.setText("Item Name");
custNameLbl = new Label();
custNameLbl.setBounds(new Rectangle(5, 42, 101, 23));
custNameLbl.setAlignment(Label.RIGHT);
custNameLbl.setText("Customer Name");
this.setLayout(null);
this.setSize(300, 229);
this.setTitle("Frame");

this.add(custNameLbl, null);
this.add(itemNameLbl, null);
this.add(qtyLbl, null);
this.add(priceLbl, null);

this.add(getButton(), null);
this.add(getCustNameTF(), null);
this.add(getItemNameTF(), null);
this.add(getQtyTF(), null);
this.add(getPriceTF(), null);
}
}

最佳答案

您需要一个 Sale 构造函数来添加所有组件。类似这样的东西,尽管您可能想使用 LayoutManagers 修复格式。

public Sale {
add(custNameLbl);
add(itemNameLbl);
add(qtyLbl);
...
// add the rest of your components
}

以下是运行它的方法。

public class Sale extends Frame implements ActionListener, WindowListener {

public Sale {
add(custNameLbl);
add(itemNameLbl);
add(qtyLbl);
...
// add the rest of your components
}

...
// rest of code
...

public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
Sale sale = new Sale();
sale.setVisible(true);
}
});
}
}

实际上,在代码按照您想要的方式运行之前,您还需要修复一些其他问题。但要使其启动并运行,请记住您需要在 main 中实例化 Frame 之前添加组件。

另一个提示:您似乎让 Frame 实现了 ActionListener,但我认为您真正想要的是使用您的按钮而不是框架注册 `ActionListener。考虑以下因素

button.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
int qty;
double price;
double cost;
qty = Integer.parseInt(qtyTF.getText());
price = Double.parseDouble(priceTF.getText());
cost = price * qty * 1.065;
this.add(msgLbl, null);
msgLbl.setText("The cost of this " + "transaction is: $" + cost);
custNameTF.setText("");
itemNameTF.setText("");
qtyTF.setText("");
priceTF.setText("");

// System.out.println(cost);

// TODO Auto-generated method stub

}
});

关于Java sale 从 main 方法运行,而不是作为 java beans,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20026955/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com