我正在尝试建立一个网上商店(我的任务)。我,在添加框架时遇到问题。是否有一些不同的方法在textpad中添加框架或我,我做错了。有人可以告诉我一种方法来做到这一点。
谢谢!
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.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import java.awt.Color;
import javax.swing.ImageIcon;
import javax.swing.*;
public class AppStore extends JFrame
{
private static final int FRAME_WIDTH = 1050;
private static final int FRAME_HEIGHT = 550;
private static final int CANDY_GAME_STOCK = 8;
private static final int DEFAULT_QUANTITY = 0;
private static final double DEFAULT_PRICE = 3.5;
private JLabel candyCrush;
private JLabel candyStockLabel;
private JLabel priceLabel;
private JLabel candyImage;
private JLabel error;
private JLabel candyCartLabel;
private ImageIcon candy;
private JLabel adding;
private JButton incCandy;
private JButton decCandy;
private JButton buy;
private JButton addTo;
private JButton proceed;
private JTextField quantity;
private JTextField candyStockField;
private static JPanel cartPanel;
private double price;
private int candyStock;
private int candyGame;
private double sum;
public AppStore()
{
price = DEFAULT_PRICE;
candyStock = CANDY_GAME_STOCK;
priceLabel = new JLabel("Price: ");
candyCrush = new JLabel("Candy-Crush Game: ");
candyStockLabel = new JLabel("In stock: ");
candyCartLabel = new JLabel("Dear Customer! ");
error = new JLabel(" ");
adding = new JLabel("Add this item to shopping cart");
ImageIcon candy = new ImageIcon("candycrush.jpg");
candyImage = new JLabel(new ImageIcon("candycrush.jpg"));
candyImage = new JLabel(candy);
createTextField();
createButton();
createPanel();
setSize(FRAME_WIDTH, FRAME_HEIGHT);
}
private void createTextField()
{
final int FIELD_WIDTH = 4;
final int STOCK_WIDTH = 2;
quantity = new JTextField(FIELD_WIDTH);
quantity.setText("" + DEFAULT_QUANTITY);
candyStockField = new JTextField(STOCK_WIDTH);
candyStockField.setText("" + CANDY_GAME_STOCK);
}
class IncrementCandy implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
int addition = 1;
candyGame = candyGame + addition;
quantity.setText("" + candyGame);
int newQuantity = Integer.parseInt(quantity.getText());
double newPrice = price * newQuantity;
priceLabel.setText("Price: " + newPrice);
}
}
class DecrementCandy implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
int subtraction = 1;
candyGame = candyGame - subtraction;
quantity.setText("" + candyGame);
int newQuantity = Integer.parseInt(quantity.getText());
double newPrice = price * newQuantity;
priceLabel.setText("Price: " + newPrice);
}
}
class AddToCartListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
int newQuantity = Integer.parseInt(quantity.getText());
if((newQuantity > 0)&&(newQuantity <= CANDY_GAME_STOCK))
{
int remainingStock = CANDY_GAME_STOCK - newQuantity;
candyStockField.setText("" + remainingStock);
error.setVisible(false);
}
if((newQuantity > CANDY_GAME_STOCK)||(newQuantity <= 0))
{
error.setText("Invalid, Selection");
error.setVisible(true);
}
}
}
class CartDisplay implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
JFrame shoppingCart = new JFrame();
int newQuantity = Integer.parseInt(quantity.getText());
candyCartLabel.setText("Dear Customer! you added " + newQuantity);
cartPanel.add(candyCartLabel);
add(cartPanel);
shoppingCart.setVisible(true);
}
}
private void createButton()
{
buy = new JButton("Buy");
incCandy = new JButton("+");
ActionListener listener = new IncrementCandy();
incCandy.addActionListener(listener);
decCandy = new JButton("-");
ActionListener listener2 = new DecrementCandy();
decCandy.addActionListener(listener2);
addTo = new JButton("Add to Cart");
ActionListener stockReduction = new AddToCartListener();
addTo.addActionListener(stockReduction);
proceed = new JButton("Proceed");
ActionListener display = new CartDisplay();
proceed.addActionListener(display);
}
private void createPanel()
{
JPanel panel = new JPanel();
panel.setBackground(Color.GRAY);
panel.add(candyImage);
panel.add(candyCrush);
panel.add(quantity);
panel.add(candyStockLabel);
panel.add(candyStockField);
panel.add(incCandy);
panel.add(decCandy);
panel.add(priceLabel);
panel.add(addTo);
panel.add(proceed);
panel.add(error);
panel.add(adding);
adding.setOpaque(true);
adding.setBackground(Color.WHITE);
add(panel);
}
public static void main(String [] args)
{
AppStore frame = new AppStore();
frame.setTitle("Appstore");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
答案 0 :(得分:0)
我认为您在使用它之前错过了初始化cartPanel,在AppStore构造函数中初始化它。 在为内框使用setVisible(true)之前,你应该为它设置Size。 另请参阅JInternalFrame(https://docs.oracle.com/javase/7/docs/api/javax/swing/JInternalFrame.html)