📄 product.java
字号:
import java.awt.*;
import javax.swing.*;
//import Product.ProductPanel.Constraint;
/**
* This class models a product sold by the Gourmet Coffee store.
* It contains the following information:
* <ol>
* <li>the code of the product, a <code>String</code></li>
* <li>a short description of the product, a <code>String</code></li>
* <li>the price of the product, a <code>double</code></li>
* </ol>
*
* @author author name
* @version 1.0.0
*/
public class Product {
/* Code of the product. */
private String code;
/* Description of the product. */
private String description;
/* Price of the product. */
private double price;
/**
* Constructs a <code>Product</code> object.
*
* @param initialCode the code of the product.
* @param initialDescription the description of the product.
* @param initialPrice the price of the product.
*/
public Product(String initialCode, String initialDescription,
double initialPrice) {
code = initialCode;
description = initialDescription;
price = initialPrice;
}
/**
* Returns the code of this product.
*
* @return the code of this product.
*/
public String getCode() {
return code;
}
/**
* Returns the description of this product.
*
* @return the descripton of this product.
*/
public String getDescription() {
return description;
}
/**
* Returns the price of this product.
*
* @return price of this product.
*/
public double getPrice() {
return price;
}
/**
* Returns <code>true</code> if code of this product is
* equal to code of the argument.
* </p>
*
* @param object object with which this product is compared.
* @return <code>true</code> if code of this product is
* equal to code of the argument; <code>false</code> otherwise.
*/
public boolean equals(Object object) {
return object instanceof Product
&& getCode().equals(((Product) object).getCode());
}
/**
* Returns the string representation of this product.
*
* @return the string representation of this product.
*/
public String toString() {
return getCode() + "_" + getDescription() + "_" + getPrice();
}
/**
* Obtains a {@link JPanel} object with the information of this product.
*
* @return a <code>JPanel</code> with the information of this product.
*/
public JPanel getPanel() {
/* PLACE YOUR CODE HERE */
return new ProductPanel(); // REMOVE; USED SO THIS FILE COMPILES
}
public class ProductPanel extends JPanel
{
public ProductPanel()
{
GridBagLayout grid = new GridBagLayout();
setLayout(grid);
AddRow("code :",code);
AddRow("description:",description);
AddRow("price :",Double.toString(price));
/* JLabel labelCode = new JLabel("code :");
JTextField textFieldCode = new JTextField(code, 18);
JLabel labeldescription = new JLabel("description :");
JTextField textFielddescription = new JTextField(description, 18);
JLabel labelprice = new JLabel("price :");
JTextField textFieldprice = new JTextField(Double.toString(price) , 18);
// Define the text aligment
textFieldCode.setHorizontalAlignment(JTextField.CENTER);
textFielddescription.setHorizontalAlignment(JTextField.CENTER);
textFieldprice.setHorizontalAlignment(JTextField.CENTER);
// Make the text field un-editable
textFieldCode.setEditable(false);
textFielddescription.setEditable(false);
textFieldprice.setEditable(false);
JPanel panelCode = new JPanel();
panelCode.add(labelCode);
panelCode.add(textFieldCode);
add(panelCode);
JPanel paneldescription = new JPanel();
paneldescription.add(labeldescription);
paneldescription.add(textFielddescription);
add(paneldescription);
JPanel panelprice = new JPanel();
panelprice.add(labelprice);
panelprice.add(textFieldprice);
add(panelprice);*/
}
public void AddRow(String name, String value)
{
JLabel label = new JLabel(name);
JTextField textField = new JTextField(value, 15);
textField.setEditable(false);
add(label,new Constraint(0));
add(textField, new Constraint(1));
row++;
}
class Constraint extends GridBagConstraints
{
public Constraint(int col)
{
this.gridx = col;
this.gridy = row;
weightx = 100;
weighty = 100;
anchor = GridBagConstraints.WEST;
}
}
private int row = 0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -