2016-05-05 5 views
1

ユーザーが開始ボタンをクリックしたときに停止ボタンを押して合計数を表示しようとしています。停止ボタンは、あなたがそれをクリックしたときに与えられる数字を合計したり保持したりすることはありません。停止ボタンは「合計」を実行しません

私はここで間違っていますか?

package com.egroegnosbig.dicerollergui; 

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Font; 

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.AbstractButton; 
import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 

public class Fishing { 
    public static void main(String[] args) { 
     //create Jframe 
     JFrame Frame = new JFrame("Fishing Game"); 

     JPanel panel = new JPanel(new BorderLayout()); 
     panel.setPreferredSize(new Dimension(600,600)); 

     //two panels in main panel 
     JPanel panelup = new JPanel(); 
     JPanel paneldown = new JPanel(); 
     panelup.setBackground(Color.white); 

     //title image with word overlay 
     ImageIcon pole1 = new ImageIcon("fishingpole.png"); 
     JLabel title = new JLabel(pole1); 
     title.setBounds(150, 0,325,175); 
     title.setText("Go Fishing?"); 
     title.setFont(new Font("Ravie", Font.BOLD, 30)); 
     title.setForeground(Color.green); 
     title.setHorizontalTextPosition(JLabel.CENTER); 
     title.setVerticalTextPosition(JLabel.CENTER); 
     title.setOpaque(false); 
     panelup.add(title); 

     panelup.setPreferredSize(new Dimension(600,375)); 
     panelup.setLayout(null); 

     JLabel points = new JLabel("<html>Look what you caught!<br>(click start to keep going)</html> "); 
     points.setForeground(Color.blue); 
     points.setFont(new Font(null, Font.BOLD, 20)); 
     points.setBounds(10,150, 450, 100); 
     panelup.add(points); 

     JTextField caught = new JTextField(20); 
     caught.setSize(10,10); 
     caught.setFont(new Font("Times New Roman", Font.BOLD, 15)); 
     caught.setForeground(Color.RED); 
     caught.setBounds(10,230,350,30); 
     panelup.add(caught); 

     JTextField total = new JTextField(20); 
     total.setSize(10,10); 
     total.setFont(new Font("Times New Roman", Font.BOLD, 15)); 
     total.setForeground(Color.RED); 
     total.setBounds(325,315,150,30); 
     panelup.add(total); 

     ** This is the stop button** 
     JLabel End = new JLabel("<html>All Done?! Press Stop and Let's<br>see how well you did!<html>"); 
     End.setForeground(Color.blue); 
     End.setFont(new Font(null, Font.BOLD, 20)); 
     End.setBounds(10, 310, 325, 50); 
     panelup.add(End); 

     JTextField single = new JTextField(20); 
     single.setSize(10,10); 
     single.setFont(new Font("Times New Roman", Font.BOLD, 15)); 
     single.setForeground(Color.RED); 
     single.setBounds(10,275,100,30); 
     panelup.add(single); 

     //Label for asking user to continue 
     ImageIcon bobber = new ImageIcon("bobber2.jpg"); 
     JButton start = new JButton(bobber); 
     start.setBorderPainted(false); 
     start.setBounds(350, 170,100,100); 
     start.setText("Start!"); 
     start.setFont(new Font("Serif", Font.BOLD, 20)); 
     start.setForeground(Color.black); 
     start.setHorizontalTextPosition(JLabel.CENTER); 
     start.setVerticalTextPosition(JLabel.CENTER); 
     start.setOpaque(false); 
     panelup.add(start); 

     ImageIcon bobber2 = new ImageIcon("bobber2.jpg"); 
     JButton stop= new JButton(bobber2); 
     stop.setBorderPainted(false); 
     stop.setBounds(450, 170,100,100); 
     stop.setText("Stop!"); 
     stop.setFont(new Font("Serif", Font.BOLD, 25)); 
     stop.setForeground(Color.black); 
     stop.setHorizontalTextPosition(JLabel.CENTER); 
     stop.setVerticalTextPosition(JLabel.CENTER); 
     stop.setOpaque(false); 
     panelup.add(stop); 

     start.addActionListener(new ActionListener() 
      { 
       public void actionPerformed(ActionEvent e) 
       { 
        double input = 0; 
        int count = 0 ; 
        int points2 = 0; 
        int die; 
        die = (int)(Math.random()*6) + 1; 

        if (die == 1) 
        { 
         caught.setText("Winner winner fish fry dinner! Catfish "); 
         points2 = 10; 
        } 
        if (die == 2) 
        { 
         caught.setText("Nemo! Go straight to Jail. "); 
         points2 = 15; 
        } 
        if (die == 3) 
        { 
         caught.setText("Stinky shoe boohoo! "); 
         points2 = 5; 
        } 
        if (die == 4) 
        { 
         caught.setText("Whale of a fish, BIG TUNA "); 
         points2 = 30; 
        } 
        if (die == 5) 
        { 
         caught.setText("Yumm Yumm Bass Pro"); 
         points2 = 25; 
        } 
        if (die == 6) 
        { 
         caught.setText("Is that a toilet bowl seat!? "); 
         points2 = 10; 
        } 

        count += points2; 

        single.setText(points2 + ""); 
        { 
        } 
       } 
      } 
     ); 

     stop.addActionListener(new ActionListener() 
      { 
       public void actionPerformed(ActionEvent e) 
       { 
        int total2 = 0; 
        int totalpoints =0; 

        totalpoints += total2; 

        total2 = Integer.parseInt(single.getText()); 
        totalpoints += total2; 
        total.setText(""+ totalpoints); 
       } 
      } 
     ); 

     //paneldown image 
     ImageIcon water = new ImageIcon("water.jpg"); 
     JLabel pic = new JLabel(water);  
     paneldown.add(pic, BorderLayout.SOUTH); 

     //panel positioning 
     panel.add(panelup, BorderLayout.NORTH); 
     panel.add(paneldown, BorderLayout.SOUTH); 

     //set frame with panel 
     Frame.add(panel); 
     Frame.pack(); 
     Frame.setLocationRelativeTo(null); 
     Frame.setSize(600,600); 
     Frame.setVisible(true); 
    } 
} 
+0

結果を保存していないためです。合計を保存するには、一部のメンバー変数が必要になります。 – MikeCAT

+0

私の第3言語は英語ですので、これが意味をなさないのであれば申し訳ありませんが、私はstop actionListenerを合計して、合計をゼロにするようにしています。私はそれらを追加して、停止ボタンをクリックするとそれを表示したい。 –

+0

インデントと空白行を含むコードの書式設定は、コードを難しくしています。 Javaコードの書式設定のルールに目を通してください。これらのルールに従うことで、他の人があなたのコードを読んで理解しやすくなり、あなたを助けることができるようになります。ほとんどのIDEを使用している場合は、コードを正しくフォーマットするのに役立ちます。 –

答えて

0

これは可変スコープの問題です。単により多くの目に見える範囲での場所に変数を移動させることにより、それらは複数の方法によってアクセスすることができる(簡単に言えば)

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class Fishing { 

public static void main(String[] args) { 
    // This is where the variables got moved to 
    final int[] total2 = { 
      0 
    }; 
    final int[] totalpoints = { 
      0 
    }; 

    //create Jframe 
    JFrame Frame = new JFrame("Fishing Game"); 
    Frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 

    JPanel panel = new JPanel(new BorderLayout()); 
    panel.setPreferredSize(new Dimension(600, 600)); 

    //two panels in main panel 
    JPanel panelup = new JPanel(); 
    JPanel paneldown = new JPanel(); 
    panelup.setBackground(Color.white); 

    //title image with word overlay 
    ImageIcon pole1 = new ImageIcon("fishingpole.png"); 
    JLabel title = new JLabel(pole1); 
    title.setBounds(150, 0, 325, 175); 
    title.setText("Go Fishing?"); 
    title.setFont(new Font("Ravie", Font.BOLD, 30)); 
    title.setForeground(Color.green); 
    title.setHorizontalTextPosition(JLabel.CENTER); 
    title.setVerticalTextPosition(JLabel.CENTER); 
    title.setOpaque(false); 
    panelup.add(title); 

    panelup.setPreferredSize(new Dimension(600, 375)); 
    panelup.setLayout(null); 


    JLabel points = new JLabel("<html>Look what you caught!<br>(click start to keep going)</html> "); 
    points.setForeground(Color.blue); 
    points.setFont(new Font(null, Font.BOLD, 20)); 
    points.setBounds(10, 150, 450, 100); 
    panelup.add(points); 

    JTextField caught = new JTextField(20); 
    caught.setSize(10, 10); 
    caught.setFont(new Font("Times New Roman", Font.BOLD, 15)); 
    caught.setForeground(Color.RED); 
    caught.setBounds(10, 230, 350, 30); 
    panelup.add(caught); 

    JTextField total = new JTextField(20); 
    total.setSize(10, 10); 
    total.setFont(new Font("Times New Roman", Font.BOLD, 15)); 
    total.setForeground(Color.RED); 
    total.setBounds(325, 315, 150, 30); 
    panelup.add(total); 

    // STOP BUTTON 
    JLabel End = new JLabel("<html>All Done?! Press Stop and Let's<br>see how well you did!<html>"); 
    End.setForeground(Color.blue); 
    End.setFont(new Font(null, Font.BOLD, 20)); 
    End.setBounds(10, 310, 325, 50); 
    panelup.add(End); 

    JTextField single = new JTextField(20); 
    single.setSize(10, 10); 
    single.setFont(new Font("Times New Roman", Font.BOLD, 15)); 
    single.setForeground(Color.RED); 
    single.setBounds(10, 275, 100, 30); 
    panelup.add(single); 


    //Label for asking user to continue 
    ImageIcon bobber = new ImageIcon("bobber2.jpg"); 
    JButton start = new JButton(bobber); 
    start.setBorderPainted(false); 
    start.setBounds(350, 170, 100, 100); 
    start.setText("Start!"); 
    start.setFont(new Font("Serif", Font.BOLD, 20)); 
    start.setForeground(Color.black); 
    start.setHorizontalTextPosition(JLabel.CENTER); 
    start.setVerticalTextPosition(JLabel.CENTER); 
    start.setOpaque(false); 
    panelup.add(start); 

    ImageIcon bobber2 = new ImageIcon("bobber2.jpg"); 
    JButton stop = new JButton(bobber2); 
    stop.setBorderPainted(false); 
    stop.setBounds(450, 170, 100, 100); 
    stop.setText("Stop!"); 
    stop.setFont(new Font("Serif", Font.BOLD, 25)); 
    stop.setForeground(Color.black); 
    stop.setHorizontalTextPosition(JLabel.CENTER); 
    stop.setVerticalTextPosition(JLabel.CENTER); 
    stop.setOpaque(false); 
    panelup.add(stop); 


    start.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      double input = 0; 
      int count = 0; 
      int points2 = 0; 
      int die; 
      die = (int) (Math.random() * 6) + 1; 

      if (die == 1) { 
       caught.setText("Winner winner fish fry dinner! Catfish "); 
       points2 = 10; 

      } 
      if (die == 2) { 
       caught.setText("Nemo! Go straight to Jail. "); 
       points2 = 15; 
      } 

      if (die == 3) { 
       caught.setText("Stinky shoe boohoo! "); 
       points2 = 5; 
      } 
      if (die == 4) { 
       caught.setText("Whale of a fish, BIG TUNA "); 
       points2 = 30; 

      } 
      if (die == 5) { 
       caught.setText("Yumm Yumm Bass Pro"); 
       points2 = 25; 
      } 
      if (die == 6) { 
       caught.setText("Is that a toilet bowl seat!? "); 
       points2 = 10; 
      } 

      count += points2; 

      single.setText(points2 + ""); 
      totalpoints[0] += points2; 
     } 
    }); 

    stop.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      try { 
       total2[0] = Integer.parseInt(single.getText()); 
      } catch (NumberFormatException ee) { 
       // If there is a blank input from single.getText();, then make total2 zero to avoid larger errors 
       total2[0] = 0; // Another way to handle the exception 
      } 
      total.setText("" + totalpoints[0]); 
     } 

    }); 


    //paneldown image 
    ImageIcon water = new ImageIcon("water.jpg"); 
    JLabel pic = new JLabel(water); 
    paneldown.add(pic, BorderLayout.SOUTH); 

    //panel positioning 
    panel.add(panelup, BorderLayout.NORTH); 
    panel.add(paneldown, BorderLayout.SOUTH); 


    //set frame with panel 
    Frame.add(panel); 
    Frame.pack(); 
    Frame.setLocationRelativeTo(null); 
    Frame.setSize(600, 600); 
    Frame.setVisible(true); 
} 
} 
変数からアクセスされているので、私は、配列内の変数になった理由である

彼らは最終的にまたは効果的に最終的に宣言する必要があります。この記事の続きを読む(here)

ところで、(IMHO)あなたの最初のコードは恐ろしい形になっていました。コードの大部分を投稿する場合は、私のIDEの自動フォーマッタでもこれを修正できなかったので、あなた以外の誰かがそれを読んでいることに注意してください。

EDIT:機能を追加するための余分な文を含むようにコードが更新されました。 EDIT 2:より良い方法で例外を処理するコードを更新しました

+0

OKこれは停止ボタンの機能を停止させましたが、停止ボタンをクリックすると開始時の内容が追加されます。ユーザーが開始ボタンを何度もクリックして停止ボタンをクリックすると、このメソッドは合計を与えますが、与えられた前の数値を保持しません –

+0

私はコードを修正しました。あなたが何を求めているのかはっきりしていませんが、これはあなたが言っていることの線に沿っていると思います。 – Netwinder

+0

はい、このコードはほぼそこにあります。私はあなたが何をしたかを見て、あなたの忍耐と理解に感謝します!しかし、私はあなたが停止を押すまで、ユーザーがどれくらい多くの完了したボックスにどれくらい蓄積したかを表示するために開始を押すと、合計を望んでいません。プログラムが正しく追加されるようになりましたので、ありがとうございますが、停止ボタンを押すとポイントが再び合計に加算されます。これは私が修正できなかった問題です –

0

停止ボタンをクリックするたびに、totalpoints変数が再作成され、ゼロに設定されます。この変数をアクションリスナーの外に移動すると、この問題は発生しません。

関連する問題