2017-04-17 6 views
-1

私はオンラインショップ(私の割り当て)を作ろうとしています。私はフレームを追加する際に問題があります。テキストパッドやフレームのフレーム内にフレームを追加する方法がいくつかありますか?何か間違ったことをしています。誰かが私にこれを行う方法を教えてください。JFrameボタンをテキストパッドの別の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); 

    } 



} 
+0

*「フレーム内のフレームをテキストパッドに追加する方法はありますか?」「テキストパッド」とはどういう意味ですか?この単語は、タイトルの中で1回、引用文の中で1回だけ言います。クラス名は 'AppStore'です。テキストパッドはアプリですか?あなたのシステムにインストールされていますか? IDEやテキストエディタでコードを書く?他に何か?説明してください.. –

答えて

0

私はそれを使用する前にcartPanelを初期化していないと思いますが、それをAppStoreコンストラクタで初期化してください。 あなたの内側のフレームにsetVisible(true)を使用する前に、そのサイズを設定する必要があります。 JInternalFrame(https://docs.oracle.com/javase/7/docs/api/javax/swing/JInternalFrame.html

関連する問題