Java销售从main方法运行,而不是作为Java bean运行
如果我想在main方法中运行sale,或者作为一个应用程序而不是作为javabean运行sale,那么我应该在main方法中添加什么呢???,因为现在作为javabean运行sale效果很好,但是当我作为一个应用程序运行sale时,什么都没有发生,因为我需要在main方法中添加一些代码Java销售从main方法运行,而不是作为Java bean运行,java,Java,如果我想在main方法中运行sale,或者作为一个应用程序而不是作为javabean运行sale,那么我应该在main方法中添加什么呢???,因为现在作为javabean运行sale效果很好,但是当我作为一个应用程序运行sale时,什么都没有发生,因为我需要在main方法中添加一些代码 import java.awt.Frame; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import jav
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
构造函数,添加所有组件。类似于此,不过您可能需要使用LayoutManager修复格式设置
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
实现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
}
});
您需要一个Sale
构造函数,添加所有组件。类似于此,不过您可能需要使用LayoutManager修复格式设置
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
实现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
}
});