-4
我遇到了編譯我的代碼的問題。嘗試生成對象時出錯 - 構造函數類
問題似乎是當我嘗試從我的主類中的客戶類生成對象。 CareHire主類中的代碼行如下:
newCust[i]=new Customer();
我輸入一個字符串和整數,併爲數組中的每個條目編號。這是我打算用於每個實例的路線,但它顯然不正確,我不確定如何繼續。
當我編輯這條線,我要建立。點擊「Enter」按鈕,只顯示字符串而不顯示int值。我應該在這裏做什麼?
主類:CareHire
package CareHire;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Scanner;
public class CareHire extends JFrame implements ActionListener
{
private JLabel nameLabel=new JLabel("Name");
private JLabel daysLabel=new JLabel("Days of Hire");
private JTextField nameField=new JTextField(16);
private JTextField daysField=new JTextField(11);
private JButton enterButton=new JButton("Enter");
private JButton displayButton=new JButton("Display");
private JButton searchButton= new JButton("Search");
private JButton displayStatButton=new JButton("Statistics");
private JButton exitButton=new JButton("Exit");
private JTextArea textArea=new JTextArea(16,35);
private Customer newCust[]=new Customer[20];
int numProcessed=0;
private static final int FRAME_WIDTH = 480;
private static final int FRAME_HEIGHT = 430;
//This is the problematic section
public CareHire() {
super(" Care Hire System ");
setLayout(new FlowLayout());
//Issues appears to be with this For Loop
for(int i=0;i<20;i++) {
newCust[i]=new Customer();
}
//End there. Remainder of code is provided for context.
add(nameLabel);
add(nameField);
add(daysLabel);
add(daysField);
add(enterButton);
add(displayButton);
add(searchButton);
add(displayStatButton);
add(exitButton);
add(textArea);
enterButton.addActionListener(this);
displayButton.addActionListener(this);
searchButton.addActionListener(this);
displayStatButton.addActionListener(this);
exitButton.addActionListener(this);
}
}
public void processInput()
{
textArea.setText(nameField.getText());
newCust[numProcessed].setDoH(Integer.parseInt(daysField.getText()));
String title="Customer Days of Hire ($)\n\n";
textArea.setText(title+newCust[numProcessed].getName()+ "\t $"+newCust[numProcessed].getDoH()+"\t "+" ");
nameField.setText("");
daysField.setText("");
numProcessed++;
}
public void display() {
if(numProcessed==0) { //no data entered
JOptionPane.showMessageDialog(null,"Need Customers","Display All",JOptionPane.ERROR_MESSAGE);
return;
}
String allItems="";
for(int i=0;i<numProcessed;i++) {
allItems=allItems+(i+1)+".\t"+newCust[i].getName()+ "\t\t $"+newCust[i].getDoH()+"\t "+" "+"\n";}
textArea.setText("No.\t Customer \t Days of Hire \n\n"+allItems);
}
public void search()
{
}
public void statistics() {
if(numProcessed==0){ //no data entered
JOptionPane.showMessageDialog(null,"Need Customers", "Find Max",JOptionPane.ERROR_MESSAGE);
return;
}
String maxName=null;
int maxAmount=-10000;
for(int i=0;i<numProcessed;i++) {
if(newCust[i].getDoH()>maxAmount){
maxAmount=newCust[i].getDoH();
maxName=newCust[i].getName();
}
}
}
public static void main(String[] args)
{
JFrame frame = new CareHire();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setVisible(true);
frame.setResizable(false);
}
}
容器類:客戶
package CareHire;
public class Customer {
private String name;
private int doh;
public Customer(String custName, int totalDays) {
name = custName;
doh = totalDays;
}
public void setName(String custName) {
name = custName;
}
public String getName() {
return name;
}
public void setDoH(int totalDays) {
doh = totalDays;
}
public int getDoH() {
return doh;
}
}
's [i] = int new custName();'也不會編譯。 –
請改善你的標題**和**添加一個**問題**,因爲它不清楚你在問什麼。 –
對不起,我一直在試圖使這個清晰,而我自己調查這個問題。我認爲問題可能是我不確定我應該問什麼問題。這讓我很難過。我已經更新了主題,並附有一段引人入勝的問題。 –