-1
我正在嘗試創建一個在線商店(我的任務)。我,米添加框架問題。有沒有一些不同的方式來添加框架內的文本框或我,我做錯了什麼。有人請告訴我一種方法來做到這一點。如何使一個JFrame按鈕在Textpad中打開另一個JFrame類
謝謝!
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);
}
}
*「是否有一些不同的方式來添加幀在一個框架內的文本..」*你是什麼意思的'文本'?這個詞只在標題中提到過一次,而在引用的句子中只提到一次,而類名稱爲「AppStore」。是textpad的應用程序。安裝在你的系統上?您用代碼編寫的IDE或文本編輯器?還有別的嗎?請解釋.. –