我可以使用一些帮助,因为我真的被卡住了。我正在做这个家庭作业,我似乎无法弄清楚它为什么不起作用。这是确切的说明;
所以我认为我被困在#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中设置输出文本时出错。谢谢大家!
答案 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());