2016-04-28 31 views
-3

私はGuess Your Numberゲームを作っていますが、実行されません。GuessMyNumber guiは実行されません。

はまた、あなたは、ウィンドウが表示されるように忘れてしまったかのようにそれが表示され、全体的なコード

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.util.Random; 
public class GuessMyNumber extends JFrame { 


    private JTextField textField; 
    public JPanel Mainpanel; 
    public JLabel lblEnterNumberHere; 
    public JLabel lblNewLabel; 
    public JButton btnCheckGuess; 
    public JLabel lblNewLabel_1; 
    Random rand = new Random(); 
    int random = rand.nextInt(100); 
    int cap = 3; 
    int tries = 0; 

    public GuessMyNumber(){ 
     getContentPane().setLayout(null); 

     Mainpanel = new JPanel(); 
     Mainpanel.setBounds(0, 0, 424, 250); 
     getContentPane().add(Mainpanel); 
     Mainpanel.setLayout(null); 

     lblNewLabel = new JLabel("Enter a number between 1-100 "); 
     lblNewLabel.setBounds(126, 11, 153, 14); 
     Mainpanel.add(lblNewLabel); 

     lblEnterNumberHere = new JLabel("Enter number here"); 
     lblEnterNumberHere.setBounds(164, 51, 90, 14); 
     Mainpanel.add(lblEnterNumberHere); 

     textField = new JTextField(); 
     textField.setBounds(174, 76, 86, 20); 
     Mainpanel.add(textField); 
     textField.setColumns(10); 

     btnCheckGuess = new JButton("Check guess"); 
     btnCheckGuess.setBounds(161, 107, 93, 23); 
     Mainpanel.add(btnCheckGuess); 

     lblNewLabel_1 = new JLabel(""); 
     lblNewLabel_1.setBounds(150, 159, 129, 47); 
     Mainpanel.add(lblNewLabel_1); 

     initialize(); 

    } 

    private class ButtonListener implements ActionListener 
    { 
     @Override 
     public void actionPerformed(ActionEvent arg0) 
     { 
      int guess = Integer.parseInt(textField.getText()); 
      if(tries==cap){ 
       lblNewLabel_1.setText("You have guessed too many times."); 
      } 
      else{ 
       tries++; 
       if(guess>random) 
        lblNewLabel_1.setText("Your guess was too high."); 
       else if(guess<random) 
        lblNewLabel_1.setText("Your guess is too low."); 
       else 
        lblNewLabel_1.setText("You are correct."); 
      } 
     } 
    } 

    public static void main(String[] args) 
    { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       new GuessMyNumber(); 
      } 
     }); 
    } 

    private void initialize() 
    { 
     Mainpanel.setPreferredSize(new Dimension(300,300)); 
     // Create labels, buttons, etc. and place them on the window here... 
    } 
} 
+0

質問は何ですか? – randominstanceOfLivingThing

+0

ランダムな結末は '}'ですか? – Laurel

+0

Java GUIは、異なるロケールの異なるPLAFを使用して、異なるOS、画面サイズ、画面解像度などで動作する必要があります。したがって、ピクセルの完全なレイアウトには役立ちません。代わりに、レイアウトマネージャや[それらの組み合わせ](http://stackoverflow.com/a/5630271/418556)と[空白](http://stackoverflow.com/a/17874718/)のレイアウトパディングとボーダーを使用してください。 418556)。 –

答えて

2

上のより多くの助けを必要としています。 setVisible(true);

ウィンドウが表示されない場合、イベントリスナーは起動せず、実行するコードはもうないので、プログラムは終了します。