问题:
在输入3个文本字段后运行我的程序时,您应该单击Process Item#1然后单击确认 - 但是这不会按预期发生 - 当您单击进程项时它不执行任何操作。
需要量:
点击Process Item#1我需要它来处理订单 - 我试图通过调用:
来实现这一点sProcessItem.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
sStoreAction.ProcessOrder(sCD, Integer.valueOf(sQuantityTF.getText()), Integer.valueOf(sNumItemsTF.getText()));
if(sCurrentOrderNumber < Integer.valueOf(sNumItemsTF.getText())){
sNumItemsTF.setEditable(false);
sNumItemsTF.setEditable(false);
sConfirmItem.setEnabled(true);
sProcessItem.setEnabled(false);
sIDTF.setText("");
sQuantityTF.setText("");
sConfirmItem.setEnabled(false);
//sCurrentOrderNumber++;
RedrawLabels();
}else{
sConfirmItem.setEnabled(false);
sProcessItem.setEnabled(false);
}
完整来源:
package theCDstore;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
public class Store_GUI {
private CDobject sCD;
private int sCurrentOrderNumber;
private JFrame sStoreWindow;
private JPanel sPanel;
private SpringLayout sLayout;
private Store_backend sStoreAction;
private JTextField sNumItemsTF;
private JTextField sIDTF;
private JTextField sQuantityTF;
private JTextField sItemInfoTF;
private JTextField sSubtotalTF;
private JLabel sNumItemsL;
private JLabel sIDL;
private JLabel sQuantityL;
private JLabel sItemInfoL;
private JLabel sSubtotalL;
private JButton sProcessItem;
private JButton sConfirmItem;
private JButton sViewOrder;
private JButton sFinishOrder;
private JButton sNewOrder;
private JButton sExit;
/**
* Starts the initialization of the JFrame, and initializes the StoreAction object.
* @param args
*/
public static void main(String[] args) {
new Store_GUI().StartThread();
sCurrentOrderNumber = 1;
InitWindow();
}
/**
* Sets many of the base attributes of the frame/panel and calls other methods to populate it.
*/
private void InitWindow(){
sStoreWindow = new JFrame("Adrian's Wonderful World of Music");
sPanel = new JPanel();
sPanel.setSize(1000, 300);
sStoreWindow.setSize(1000, 300);
InitButtons();
InitLabels();
InitText();
InitPlacement();
sStoreWindow.add(sPanel);
sStoreWindow.setVisible(true);
}
/**
* Initializes the JLabels and adds them to the panel.
*/
private void InitLabels(){
sNumItemsL = new JLabel("Enter the number of items for this order:");
sIDL = new JLabel("Enter CD ID for item #"+String.valueOf(sCurrentOrderNumber)+":");
sQuantityL = new JLabel("Enter quantity for item #"+String.valueOf(sCurrentOrderNumber)+":");
sItemInfoL = new JLabel("Item #"+String.valueOf(sCurrentOrderNumber)+" info:");
sSubtotalL = new JLabel("Order subtotal for "+String.valueOf(sCurrentOrderNumber-1)+" item(s):");
sPanel.add(sNumItemsL);
sPanel.add(sIDL);
sPanel.add(sQuantityL);
sPanel.add(sItemInfoL);
sPanel.add(sSubtotalL);
}
/**
* Initializes the JTextFields and adds listeners to them.
*/
private void InitText(){
sNumItemsTF = new JTextField(13);
sIDTF = new JTextField(13);
sQuantityTF = new JTextField(13);
sItemInfoTF = new JTextField(40);
sSubtotalTF = new JTextField(13);
sIDTF.addKeyListener(new KeyListener(){
@Override
public void keyPressed(KeyEvent arg0) {
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
if((arg0.getKeyCode() == KeyEvent.VK_ENTER) && !sIDTF.getText().isEmpty()){
sCD = sStoreAction.FindCD(Integer.valueOf(sIDTF.getText()));
if(!sQuantityTF.getText().isEmpty())
sStoreAction.SetCDInfo(sCD, Integer.valueOf(sQuantityTF.getText()));
else
sStoreAction.SetCDInfo(sCD, 1);
AutoComplete(sCD);
}
}});
sIDTF.addFocusListener(new FocusListener(){
@Override
public void focusGained(FocusEvent e) {
// TODO Auto-generated method stub
}
@Override
public void focusLost(FocusEvent e) {
if(!sIDTF.getText().isEmpty()){
sCD = sStoreAction.FindCD(Integer.valueOf(sIDTF.getText()));
if(!sQuantityTF.getText().isEmpty())
sStoreAction.SetCDInfo(sCD, Integer.valueOf(sQuantityTF.getText()));
else
sStoreAction.SetCDInfo(sCD, 1);
AutoComplete(sCD);
}
}
});
sQuantityTF.addFocusListener(new FocusListener(){
@Override
public void focusGained(FocusEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void focusLost(FocusEvent arg0) {
if(!sQuantityTF.getText().isEmpty() && !sIDTF.getText().isEmpty()){
// sCD = sStoreAction.FindCD(Integer.valueOf(sIDTF.getText()),Integer.valueOf(sQuantityTF.getText()));
sCD = sStoreAction.FindCD(Integer.valueOf(sIDTF.getText()));
sStoreAction.SetCDInfo(sCD, Integer.valueOf(sQuantityTF.getText()));
AutoComplete(sCD);
}
}});
sQuantityTF.addKeyListener(new KeyListener(){
@Override
public void keyPressed(KeyEvent arg0) {
}
@Override
public void keyReleased(KeyEvent arg0) {
if(Character.isDigit(arg0.getKeyChar())){
// sCD = sStoreAction.FindCD(Integer.valueOf(sIDTF.getText()),Integer.valueOf(sQuantityTF.getText()));
sCD = sStoreAction.FindCD(Integer.valueOf(sIDTF.getText()));
sStoreAction.SetCDInfo(sCD, Integer.valueOf(sQuantityTF.getText()));
AutoComplete(sCD);
}
}
@Override
public void keyTyped(KeyEvent arg0) {
}});
sItemInfoTF.setEnabled(false);
sSubtotalTF.setEditable(false);
sPanel.add(sNumItemsTF);
sPanel.add(sIDTF);
sPanel.add(sQuantityTF);
sPanel.add(sItemInfoTF);
sPanel.add(sSubtotalTF);
}
/**
* Initializes the JButtons and adds listeners to them.
*/
private void InitButtons(){
sProcessItem = new JButton("Process Item #"+String.valueOf(sCurrentOrderNumber)+"");
sConfirmItem = new JButton("Confirm Item #"+String.valueOf(sCurrentOrderNumber)+"");
sViewOrder = new JButton("View Order");
sFinishOrder = new JButton("Finish Order");
sNewOrder = new JButton("New Order");
sExit = new JButton("Exit");
sConfirmItem.setEnabled(false);
sViewOrder.setEnabled(false);
sFinishOrder.setEnabled(false);
sProcessItem.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
sStoreAction.ProcessOrder(sCD, Integer.valueOf(sQuantityTF.getText()), Integer.valueOf(sNumItemsTF.getText()));
if(sCurrentOrderNumber < Integer.valueOf(sNumItemsTF.getText())){
sNumItemsTF.setEditable(false);
sNumItemsTF.setEditable(false);
sConfirmItem.setEnabled(true);
sProcessItem.setEnabled(false);
sIDTF.setText("");
sQuantityTF.setText("");
sConfirmItem.setEnabled(false);
//sCurrentOrderNumber++;
RedrawLabels();
}else{
sConfirmItem.setEnabled(false);
sProcessItem.setEnabled(false);
}
}});
sConfirmItem.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(sPanel,"Item #"+String.valueOf(sCurrentOrderNumber)+" accepted");
sNumItemsTF.setEditable(false);
sProcessItem.setEnabled(true);
sConfirmItem.setEnabled(false);
}});
sViewOrder.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(sPanel, sStoreAction.getDisplayViewOrder());
}});
sFinishOrder.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(sPanel, sStoreAction.DisplayInvoice());
sStoreAction.WriteInvoice();
NewOrderClick();
}});
sNewOrder.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
NewOrderClick();
}});
sExit.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
sStoreWindow.dispose();
}});
sPanel.add(sProcessItem);
sPanel.add(sConfirmItem);
sPanel.add(sViewOrder);
sPanel.add(sFinishOrder);
sPanel.add(sNewOrder);
sPanel.add(sExit);
}
/**
* Positions all of the objects in the JPanel using SpringLayout.
*/
private void InitPlacement(){
sLayout = new SpringLayout();
sLayout.putConstraint(SpringLayout.NORTH,sNumItemsL, 5, SpringLayout.NORTH,sPanel);
sLayout.putConstraint(SpringLayout.NORTH,sNumItemsTF, 5, SpringLayout.NORTH,sPanel);
sLayout.putConstraint(SpringLayout.WEST, sNumItemsL, 5, SpringLayout.WEST, sPanel);
sLayout.putConstraint(SpringLayout.WEST, sNumItemsTF, 5, SpringLayout.EAST, sNumItemsL);
sLayout.putConstraint(SpringLayout.NORTH,sIDL, 7, SpringLayout.SOUTH,sNumItemsL);
sLayout.putConstraint(SpringLayout.WEST, sIDL, 5, SpringLayout.WEST, sPanel);
sLayout.putConstraint(SpringLayout.NORTH, sIDTF, 5, SpringLayout.SOUTH, sNumItemsTF);
sLayout.putConstraint(SpringLayout.WEST, sIDTF, 5, SpringLayout.EAST, sIDL);
sLayout.putConstraint(SpringLayout.NORTH,sQuantityL, 10, SpringLayout.SOUTH,sIDL);
sLayout.putConstraint(SpringLayout.WEST, sQuantityL, 5, SpringLayout.WEST, sPanel);
sLayout.putConstraint(SpringLayout.NORTH, sQuantityTF, 5, SpringLayout.SOUTH, sIDTF);
sLayout.putConstraint(SpringLayout.WEST, sQuantityTF, 5, SpringLayout.EAST, sQuantityL);
sLayout.putConstraint(SpringLayout.NORTH,sItemInfoL, 10, SpringLayout.SOUTH,sQuantityL);
sLayout.putConstraint(SpringLayout.WEST, sItemInfoL, 5, SpringLayout.WEST, sPanel);
sLayout.putConstraint(SpringLayout.NORTH, sItemInfoTF, 5, SpringLayout.SOUTH, sQuantityTF);
sLayout.putConstraint(SpringLayout.WEST, sItemInfoTF, 5, SpringLayout.EAST, sItemInfoL);
sLayout.putConstraint(SpringLayout.NORTH,sSubtotalL, 10, SpringLayout.SOUTH,sItemInfoL);
sLayout.putConstraint(SpringLayout.WEST, sSubtotalL, 5, SpringLayout.WEST, sPanel);
sLayout.putConstraint(SpringLayout.NORTH, sSubtotalTF, 5, SpringLayout.SOUTH, sItemInfoTF);
sLayout.putConstraint(SpringLayout.WEST, sSubtotalTF, 5, SpringLayout.EAST, sSubtotalL);
sLayout.putConstraint(SpringLayout.NORTH, sProcessItem, 10, SpringLayout.SOUTH, sSubtotalL);
sLayout.putConstraint(SpringLayout.NORTH, sConfirmItem, 10, SpringLayout.SOUTH, sSubtotalL);
sLayout.putConstraint(SpringLayout.NORTH, sViewOrder, 10, SpringLayout.SOUTH, sSubtotalL);
sLayout.putConstraint(SpringLayout.NORTH, sFinishOrder, 10, SpringLayout.SOUTH, sSubtotalL);
sLayout.putConstraint(SpringLayout.NORTH, sNewOrder, 10, SpringLayout.SOUTH, sSubtotalL);
sLayout.putConstraint(SpringLayout.NORTH, sExit, 10, SpringLayout.SOUTH, sSubtotalL);
sLayout.putConstraint(SpringLayout.WEST, sProcessItem, 10, SpringLayout.WEST, sPanel);
sLayout.putConstraint(SpringLayout.WEST, sConfirmItem, 10, SpringLayout.EAST, sProcessItem);
sLayout.putConstraint(SpringLayout.WEST, sViewOrder, 10, SpringLayout.EAST, sConfirmItem);
sLayout.putConstraint(SpringLayout.WEST, sFinishOrder, 10, SpringLayout.EAST, sViewOrder);
sLayout.putConstraint(SpringLayout.WEST, sNewOrder, 10, SpringLayout.EAST, sFinishOrder);
sLayout.putConstraint(SpringLayout.WEST, sExit, 10, SpringLayout.EAST, sNewOrder);
sPanel.setLayout(sLayout);
}
/**
* Auto fills the Info, Subtotal, and if necessary the Quantity text boxes.
* @param mCD The current CD object.
*/
private void AutoComplete(CDobject mCD){
sItemInfoTF.setText(mCD.getInfo());
if(!sQuantityTF.getText().isEmpty())
sSubtotalTF.setText(String.valueOf(sStoreAction.getDisplaySubtotal(mCD, Integer.valueOf(sQuantityTF.getText()))));
else{
sQuantityTF.setText("1");
sSubtotalTF.setText(String.valueOf(sStoreAction.getDisplaySubtotal(mCD,1)));
}
}
/**
* Updates the items number of many of the labels based on the sCurrentOrderNumber.
*/
private void RedrawLabels(){
sProcessItem.setText("Process Item #"+String.valueOf(sCurrentOrderNumber)+"");
sConfirmItem.setText("Confirm Item #"+String.valueOf(sCurrentOrderNumber)+"");
sIDL.setText("Enter CD ID for item #"+String.valueOf(sCurrentOrderNumber)+":");
sQuantityL.setText("Enter quantity for item #"+String.valueOf(sCurrentOrderNumber)+":");
sItemInfoL.setText("Item #"+String.valueOf(sCurrentOrderNumber)+" info:");
sSubtotalL.setText("Order subtotal for "+String.valueOf(sCurrentOrderNumber-1)+" item(s):");
}
/**
* Clears the text fields, reinitializes the StoreAction object, and redraws the labels.
*/
private void NewOrderClick(){
new Store_GUI().StartThread();
sCurrentOrderNumber = 1;
sIDTF.setText("");
sQuantityTF.setText("");
sItemInfoTF.setText("");
sSubtotalTF.setText("");
sNumItemsTF.setText("");
RedrawLabels();
sNumItemsTF.setEditable(true);
sConfirmItem.setEnabled(true);
sProcessItem.setEnabled(false);
}
/**
* Defines a new class to execute the initialization of Store Action on its own thread.
* @author david
*/
class NewThread extends Thread{
@Override
public void run() {
sStoreAction = new Store_backend();
}
}
/**
* Starts the thread.
*/
private void StartThread(){
new NewThread().start();
}
}
RESULTS (after removing static):
Error:
Main method is not static in class theCDstore.Store_GUI, please define the main method as:
public static void main(String[] args)
答案 0 :(得分:0)
根据你的意见:
仅供参考 - 当我将main方法更改为静态并将所有其他方法保持为非静态时,我会收到一条错误说明:无法对非静态字段进行静态引用sCurrentOrderNumber&amp;无法从类型Store_GUI Store_GUI.java * 强文本 *
中对非静态方法InitWindow()进行静态引用
我已经在你上面的评论中提到过:
您的问题是,在您的main方法中,您尝试以静态方式访问类的实例字段和方法,这是错误的。不要那样做。在main方法中创建并初始化Store_GUI 变量 ,在此实例上调用startThread()
,然后在实例上调用initWindow()
。不要在没有实例的情况下调用它,因为那时您尝试以静态方式访问该方法。永远不要直接从另一个类操纵你的类的字段。
再次,通过使一切都静止,你正在修复错误的东西。解决方案不是使变量和方法保持静态,而是避免尝试以静态方式访问它们。
即,
public static void main(String[] args) {
Store_GUI store = new Store_GUI();
store.StartThread();
store.InitWindow();
// !! new Store_GUI().StartThread();
// sCurrentOrderNumber = 1;
// InitWindow();
}
这与Swing或GUI无关,而与基本编程技术有关。