当我尝试运行我的java应用程序时,我遇到以下两个错误 - 我认为我只是将值更改为静态,但这会导致更多问题。
Cannot make a static reference to the non-static field sCurrentOrderNumber Store_GUI.java /Program 1/src/theCDstore line 54 Java Problem
Cannot make a static reference to the non-static method InitWindow() from the type Store_GUI Store_GUI.java /Program 1/src/theCDstore line 55 Java Problem
有人可以解释一下这些可以如何重新装修吗?当我尝试将方法更改为静态时,除了这两个之外我还会遇到更多错误:
Description Resource Path Location Type
Cannot make a static reference to the non-static field sStoreWindow Store_GUI.java /Program 1/src/theCDstore line 78 Java Problem
Cannot make a static reference to the non-static field sStoreWindow Store_GUI.java /Program 1/src/theCDstore line 77 Java Problem
Cannot make a static reference to the non-static field sPanel Store_GUI.java /Program 1/src/theCDstore line 77 Java Problem
Cannot make a static reference to the non-static method InitText() from the type Store_GUI Store_GUI.java /Program 1/src/theCDstore line 73 Java Problem
Cannot make a static reference to the non-static method InitPlacement() from the type Store_GUI Store_GUI.java /Program 1/src/theCDstore line 75 Java Problem
Cannot make a static reference to the non-static method InitButtons() from the type Store_GUI Store_GUI.java /Program 1/src/theCDstore line 69 Java Problem
Cannot make a static reference to the non-static method InitLabels() from the type Store_GUI Store_GUI.java /Program 1/src/theCDstore line 71 Java Problem
Cannot make a static reference to the non-static field sPanel Store_GUI.java /Program 1/src/theCDstore line 66 Java Problem
Cannot make a static reference to the non-static field sStoreWindow Store_GUI.java /Program 1/src/theCDstore line 67 Java Problem
Cannot make a static reference to the non-static field sStoreWindow Store_GUI.java /Program 1/src/theCDstore line 63 Java Problem
Cannot make a static reference to the non-static field sPanel Store_GUI.java /Program 1/src/theCDstore line 64 Java Problem
SOURCE SNIPPET:
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);
}
完整来源:
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 static CDobject sCD;
private static int sCurrentOrderNumber;
private static JFrame sStoreWindow;
private static JPanel sPanel;
private static SpringLayout sLayout;
private static Store_backend sStoreAction;
private static JTextField sNumItemsTF;
private static JTextField sIDTF;
private static JTextField sQuantityTF;
private static JTextField sItemInfoTF;
private static JTextField sSubtotalTF;
private static JLabel sNumItemsL;
private static JLabel sIDL;
private static JLabel sQuantityL;
private static JLabel sItemInfoL;
private static JLabel sSubtotalL;
private static JButton sProcessItem;
private static JButton sConfirmItem;
private static JButton sViewOrder;
private static JButton sFinishOrder;
private static JButton sNewOrder;
private static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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();
}
}
答案 0 :(得分:0)
你不能这样做:
sCurrentOrderNumber = 1;
因为sCurrentOrderNumber
是属性字段,所以您需要创建Store_GUI
类的实例,然后在该对象上设置sCurrentOrderNumber
值。
你需要打破这些陈述
new Store_GUI().StartThread();
sCurrentOrderNumber = 1;
到
Store_GUI storeGUIObj = new Store_GUI();
storeGUIObj.setSCurrentOrderNumber(1);
storeGUIObj.StartThread();
由于您缺少私人提交的sCurrentOrderNumber的setter,因此您需要创建一个如上所述setSCurrentOrderNumber:
public void setSCurrentOrderNumber(int sCurrentOrderNumber )
{
this.sCurrentOrderNumber = sCurrentOrderNumber ;
}