我正在尝试使showBtn或Input按钮传递用户将在TextFields(itemNameTF,itemNumTF,itemUPCTF,itemQtyTF和itemUMTF)中输入的信息以显示在两个标签(itemQtyLbl,itemUMLbl)中,但我得到的只是一个错误,当我按下输入按钮时,我正在进入下一个窗口,但在下一个窗口中,当我按下“显示”按钮时,我不会得到用户在TextFields中输入的信息,我会感激任何帮助,提前谢谢。
代码:
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Dimension;
import java.awt.Label;
import java.awt.BorderLayout;
import java.awt.Point;
import java.awt.Color;
import java.awt.Font;
import java.awt.Button;
import java.awt.Rectangle;
import java.awt.TextField;
public class EnterItemInfo extends UsefulFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private Label compNameLbl = null;
private Button showBtn = null;
private Label itemQtyLbl = null;
private Label itemUMLbl = null;
public Item item;
private Label itemNameLbl = null;
private Label itemNumLbl = null;
private Label itemUPCLbl = null;
private TextField itemNameTF = null;
private TextField itemNumTF = null;
private TextField itemUPCTF = null;
private TextField itemQtyTF = null;
private TextField itemUMTF = null;
@Override
public void actionPerformed(ActionEvent e) {
// itemQtyLbl.setText(item.getItemQty() + " " + item.getItemUM() + " " +
// item.getItemName() + " " + "units");
// itemUMLbl.setText("were received with a UPC of" + " " +
// item.getItemUPC());
itemQtyLbl.setText(getItemQtyTF() + " " + getItemUMTF() + " "
+ getItemNameTF() + " " + "units");
itemUMLbl.setText("were received with a UPC of" + " " + getItemUPCTF());
itemNameTF.setText("");
itemNumTF.setText("");
itemUPCTF.setText("");
itemQtyTF.setText("");
itemUMTF.setText("");
ItemFrame iFrame = new ItemFrame(item);
// TODO Auto-generated method stub
}
/**
* This is the default constructor
*
* @param i
*/
public EnterItemInfo() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
itemUPCLbl = new Label();
itemUPCLbl.setForeground(Color.green);
itemUPCLbl.setLocation(new Point(74, 185));
itemUPCLbl.setSize(new Dimension(120, 23));
itemUPCLbl.setAlignment(Label.RIGHT);
itemUPCLbl.setText("UPC Code");
itemNumLbl = new Label();
itemNumLbl.setForeground(Color.green);
itemNumLbl.setLocation(new Point(74, 132));
itemNumLbl.setSize(new Dimension(120, 23));
itemNumLbl.setAlignment(Label.RIGHT);
itemNumLbl.setText("Item Number");
itemNameLbl = new Label();
itemNameLbl.setForeground(Color.green);
itemNameLbl.setLocation(new Point(74, 79));
itemNameLbl.setSize(new Dimension(120, 23));
itemNameLbl.setAlignment(Label.RIGHT);
itemNameLbl.setText("Item Name");
itemUMLbl = new Label();
itemUMLbl.setText("Unit of Measure");
itemUMLbl.setSize(new Dimension(120, 20));
itemUMLbl.setAlignment(Label.RIGHT);
itemUMLbl.setForeground(Color.green);
itemUMLbl.setLocation(new Point(74, 288));
itemQtyLbl = new Label();
itemQtyLbl.setText("Quantity");
itemQtyLbl.setSize(new Dimension(120, 20));
itemQtyLbl.setAlignment(Label.RIGHT);
itemQtyLbl.setForeground(Color.green);
itemQtyLbl.setLocation(new Point(74, 238));
compNameLbl = new Label();
compNameLbl.setText("TNT Salvage");
compNameLbl.setSize(new Dimension(384, 36));
compNameLbl.setBackground(Color.orange);
compNameLbl.setFont(new Font("Verdana", Font.PLAIN, 24));
compNameLbl.setAlignment(Label.CENTER);
compNameLbl.setLocation(new Point(8, 30));
this.setSize(400, 350);
this.setTitle("Item Information");
Item item = new Item("", "", "", "", "");
this.add(compNameLbl, BorderLayout.NORTH);
this.add(itemQtyLbl, null);
this.add(itemUMLbl, null);
this.add(itemNameLbl, null);
this.add(itemNumLbl, null);
this.add(itemUPCLbl, null);
this.add(getItemNameTF(), null);
this.add(getItemNumTF(), null);
this.add(getItemUPCTF(), null);
this.add(getItemQtyTF(), null);
this.add(getItemUMTF(), null);
this.setLayout(null);
this.add(getShowBtn(), null);
this.setVisible(true);
this.setExitButtonLocation();
}
/**
* This method initializes showBtn
*
* @return java.awt.Button
*/
private Button getShowBtn() {
if (showBtn == null) {
showBtn = new Button();
showBtn.setLocation(new Point(164, 323));
showBtn.setLabel("Input");
showBtn.setName("inputBTN");
showBtn.setSize(new Dimension(60, 20));
showBtn.addActionListener(this);
}
return showBtn;
}
/**
* This method initializes itemNameTF
*
* @return java.awt.TextField
*/
private TextField getItemNameTF() {
if (itemNameTF == null) {
itemNameTF = new TextField();
itemNameTF.setLocation(new Point(204, 79));
itemNameTF.setSize(new Dimension(150, 23));
}
return itemNameTF;
}
/**
* This method initializes itemNumTF
*
* @return java.awt.TextField
*/
private TextField getItemNumTF() {
if (itemNumTF == null) {
itemNumTF = new TextField();
itemNumTF.setLocation(new Point(204, 132));
itemNumTF.setSize(new Dimension(50, 23));
}
return itemNumTF;
}
/**
* This method initializes itemUPCTF
*
* @return java.awt.TextField
*/
private TextField getItemUPCTF() {
if (itemUPCTF == null) {
itemUPCTF = new TextField();
itemUPCTF.setLocation(new Point(204, 185));
itemUPCTF.setSize(new Dimension(75, 23));
}
return itemUPCTF;
}
/**
* This method initializes itemQtyTF
*
* @return java.awt.TextField
*/
private TextField getItemQtyTF() {
if (itemQtyTF == null) {
itemQtyTF = new TextField();
itemQtyTF.setLocation(new Point(204, 238));
itemQtyTF.setSize(new Dimension(50, 23));
}
return itemQtyTF;
}
/**
* This method initializes itemUMTF
*
* @return java.awt.TextField
*/
private TextField getItemUMTF() {
if (itemUMTF == null) {
itemUMTF = new TextField();
itemUMTF.setLocation(new Point(204, 288));
itemUMTF.setSize(new Dimension(75, 23));
}
return itemUMTF;
}
} // @jve:decl-index=0:visual-constraint="10,8"
答案 0 :(得分:1)
在您的actionPerformed中,只需获取所有文字someTextField.getText()
,然后在标签someLabel.setText("someText");
public void actionPerformed(ActionEvent e){
// example
String someTextFieldInput = someTextField.getText();
// if they are numbers from the field, you must parse
// example
int someInt = Integer.parseInt(someTextField.getText();
// after you get all your input from the text fields, add them to labels
someLabel.setText(someTextFieldInput);
someOtherLabel.setText(String.valueOf(someInt));
}
您还应该将ItemFrame
作为EnterItemInfo
的内部类,这样它就可以使用所有文本字段。将操作执行到您在EnterItemInfo
中获取文本的位置,并将其注册到该类中的按钮。
public class EnterItemInfo extends JFrame {
private ItemFrame iFrame = new iFrame();
... all text fields and labels
private JButton showBut = new JButton("show");
public ItemFrame(){
... add components
iFrame.setVisible(false);
add(iFrame);
add(showBut);
showBut.addActionListener(new ActionListener(){
iFrame.setVisible(true);
});
}
private class ItemFrame extends JFrame {
private JButton button = new JButton("show info");
public ItemFrame(){
add(new JLabel(), BorderLayout.CENTER);
add(button, borderLayout.NORTH);
button.addActionListener(new ActionListener(){
... do stuff
});
}
}
}
或者你可以让ItemFrame扩展EnterItemFrame。我不确定你从一帧到下一帧输入是否有问题。如果没有,请保持原样。