我似乎无法弄清楚如何从类

时间:2015-12-07 22:36:21

标签: java

我可以使用一些帮助,因为我真的被卡住了。我正在做这个家庭作业,我似乎无法弄清楚它为什么不起作用。这是确切的说明;

  1. 创建名为“Java1Final_LastName”的Eclipse项目。
  2. 创建一个名为“Customer”的抽象类。
  3. 在课程中添加2个私有字段,“name”和“id”。
  4. 添加一个带有2个参数的构造函数,“aName”和“aId”。
  5. 添加以下抽象方法“public abstract int discount();”
  6. 创建一个名为“OnlineCustomer”的Customer类。
  7. 添加一个带有2个参数的构造函数,name,id。
  8. 编写继承的抽象折扣方法的主体。
  9. 在线客户可获得10%的折扣。
  10. 为名为“CustomerJFrame”的用户界面创建一个JFrame类。
  11. 将“Java 1 Final by lastName”分配给帧标题。
  12. 为名称添加JLabel和JTextField。
  13. 为Id添加JLabel和JTextField。
  14. 为邮件输出添加JLabel。
  15. 为“提交”按钮添加JButton。
  16. 为提交按钮添加ActionPerformed Listener。
  17. 选择该按钮后,使用JTextFields中的name和id创建OnlineCustomer对象。在消息JLabel
  18. 中显示OnlineCustomer对象中的名称,ID和折扣
  19. 创建一个名为“CustomerApp”的类,该类具有main方法。
  20. 创建CustomerJFrame类的对象。
  21. 所以我认为我被困在#17上。我不确定它在问什么。这是我到目前为止所做的一切。我觉得我很接近,但我觉得我错过了一些重要的东西!

    - 客户类 -

    import java.time.LocalDate;
    
    public abstract class Customer 
    {
    // private fields
    private String name;
    private String idnumber;
    
    // overloaded constructor with 2 arguments
    public Customer(String aName, String aIdNumber)
    {
        this.name = aName;
        this.idnumber = aIdNumber;
    }
    
    //abstract method
    public abstract int discount();
    }
    

    - 在线客户类 -

    public class OnlineCustomer extends Customer
    {
    private int discount=10;
    
    //OnlineCustomer constructor
    public OnlineCustomer(String aName, String aIdNumber) 
    { 
        //use parent constructor
        super(aName, aIdNumber);
    }
    
    //body of inherited method
    public int discount()
    {
        return discount;
    }
    
    }
    

    -CustomerJFrame类 -

    import java.awt.FlowLayout;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    
    public class CustomerJFrame extends JFrame implements ActionListener
    {
    private static final long serialVersionUID = 1L;
    
    //fields
    private JLabel nameJLabel;
    private JTextField nameJTextField;
    
    private JLabel idJLabel;
    private JTextField idJTextField;
    
    private JLabel outputJLabel;
    
    private JButton submitJButton;
    
    private final int WIDTH = 300;
    private final int HEIGHT = 250;
    
    public CustomerJFrame()
    {
        // call the super class constructor
        super("Java 1 Final by Vokurka");
    
        // set the height and width of the JFrame
        setSize(WIDTH, HEIGHT);
    
        // set the default close operation to end the program.
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);
    
        // set flow layout
        setLayout(new FlowLayout());
    
        // create a label and add it to the frame
        nameJLabel = new JLabel("Name: ");
        add(nameJLabel);
    
        // create a text field and add it to the frame
        nameJTextField = new JTextField("", 15);
        nameJTextField.selectAll();
        add(nameJTextField);
    
        // create a label and add it to the frame
        idJLabel = new JLabel("Id: ");
        add(idJLabel);
    
        // create a text field and add it to the frame
        idJTextField = new JTextField("", 15);
        idJTextField.selectAll();
        add(idJTextField);
    
        // create and add a message JLabel
        outputJLabel = new JLabel("Customer: Name, ID:000, Discount: 0%");
        add(outputJLabel);
    
        // create and add a JButton
        submitJButton = new JButton("Submit");
        submitJButton.addActionListener(this);
        add(submitJButton);
    
        // set the JFrame to visible
        setVisible(true);
    }
    
    @Override
    public void actionPerformed(ActionEvent e) 
    {
        // get the user data from the JTextFields
        String nameresults = nameJTextField.getText();
        String idresults = nameJTextField.getText();
    
        // set the results to the message JLabel
        outputJLabel.setText("Customer:" + nameresults, "ID:" + idresults, "Discount: 10%");
    }
    }
    

    -CustomerApp Class -

    public class CustomerApp 
    {
    
    public static void main(String[] args) 
    {
        // create a JMyFrame object
        CustomerJFrame aFrame = new CustomerJFrame();
    }
    
    }
    

    任何帮助都会非常感激。如果有帮助,我在CustomerJFrame中设置输出文本时出错。谢谢大家!

2 个答案:

答案 0 :(得分:0)

就像消息所说的那样:当你只想要1:

时,你传递的是方法3参数
outputJLabel.setText("Customer:" + nameresults, "ID:" + idresults, "Discount: 10%");

相反,将它们连接成一个单独的字符串(或类似的东西):

outputJLabel.setText("Customer:" + nameresults + " ID:" + idresults + " Discount: 10%");

答案 1 :(得分:0)

你不应该按照步骤中的说法创建对象吗?

OnlineCustomer oc = new OnlineCustomer(nameJTextField.getText(), idJTextField.getText());