无法弹出下一个JFrame

时间:2013-12-03 00:48:54

标签: java swing loops jframe

我正在制作战舰游戏,当玩家获胜或失败时,我无法获得带有胜利面板的JFrame和带有失败面板的JFrame。有什么想法吗?我认为它永远不会进入主程序中的其他两个循环,但我不确定。我确实检查过以确保全局变量正确和不正确地正确递增,并且它们是。

// Battleship.java

import javax.swing.*;
import java.awt.*;
import java.util.Scanner;
import java.io.*;
import javax.swing.JOptionPane;

 public class Battleship
{
public static String name;

public static void main (String[] args) throws IOException
{

Battleship.name = JOptionPane.showInputDialog("What is your name?");

String answer = JOptionPane.showInputDialog("Welcome to Battleship, "+name+". Would you      like instructions?"); //(GUI1) Dialog Box

if(answer.equals ("yes") || answer.equals ("Yes"))
{
  JOptionPane.showMessageDialog(null,"In this variant of Battleship, you will try to   bomb two randomly-placed ships"
                                  +"\nby clicking on the buttons that represent the field of play. There are two ships:"
                                  +"\nOne 1x2 ship and one 1x3 ship. Green is a hit and  red is a miss. Good luck!");
}




//Creates a JFrame and adds the Buttons panel to it    
JFrame frame = new JFrame("Battleship");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Buttons b = new Buttons(); //(GUI2)  1-D Array of Buttons
Check c = new Check();
NamePanel n = new NamePanel();
JPanel panel = new JPanel(); //Instantiating a panel
panel.add(c);
panel.add(b); //Adds the 1-D Array of buttons to the panel
panel.setBackground(Color.blue);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
frame.getContentPane().add (panel);
frame.pack();
frame.setVisible(true);
frame.getContentPane().validate();
frame.getContentPane().repaint();

 if(Buttons.correct == 5 && (Buttons.incorrect+Buttons.correct)<=15)
 {
//Creats a new frame and adds the winner's panel to the frame 
JFrame frame1 = new JFrame("Winner!");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

WinPanel panel2 = new WinPanel();
frame1.getContentPane().add(panel2);
frame1.pack();
frame1.setVisible(true);
}

else if(Buttons.correct<5 && (Buttons.incorrect+Buttons.correct) >= 15)
{
 //Creates a new frame and adds the loser's panel to the frame
 JFrame frame1 = new JFrame("You lost");
 frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 LosePanel panel2 = new LosePanel();
 frame1.getContentPane().add(panel2);
 frame1.pack();
 frame1.setVisible(true);
  }
  }
 }

// Buttons.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Scanner;
import java.io.*;
import java.util.Random;
import java.util.*;

public class Buttons extends JPanel 
{
JButton[] btn; 
static int[] numbers = new int[5];
public static int correct = 0;
public static int incorrect = 0;


  public Buttons() throws IOException
 {
String[] numberLine = choose(new File("Coords.txt")).split(",");

    for(int i = 0; i < 5; i++)
    {
numbers[i] = Integer.parseInt(numberLine[i]);
        }


btn = new JButton[25];

setLayout(new GridLayout(5,5));

setBackground(Color.blue);
setPreferredSize(new Dimension(300,300));


ButtonListener blisten = new ButtonListener(); //LI

  for(int i=0;i<25;i++)
  {
  btn[i] = new JButton();
  btn[i].addActionListener(blisten);
  add (btn[i]);
  }

} //Ends Constructor

private class ButtonListener implements ActionListener
 {


 public void actionPerformed (ActionEvent event)
  {
  Object source = event.getSource();
  while((Buttons.correct+Buttons.incorrect)<15 && (Buttons.correct<5))
  {
  for(int i=0;i<25;i++)
    if(source==btn[i])
      if(i==numbers[0] || i==numbers[1] || i==numbers[2] || i==numbers[3] ||      i==numbers[4])
  {
    btn[i].setBackground(Color.green);
    btn[i].setEnabled(false);
    Buttons.correct++;
    System.out.println(Buttons.correct);
    }
    else
    {
    btn[i].setBackground(Color.red);
    btn[i].setEnabled(false);
    Buttons.incorrect++;
    System.out.println(Buttons.incorrect);
    }
    break;
  }



  }
}


//Choose method to choose a random line of the imported text file
public static String choose(File f) throws FileNotFoundException 
{
 String result = null; //String is set to null
 Random rand = new Random(); //Instantiating a random number generator
 int n = 0;
 for(Scanner sc = new Scanner(f); sc.hasNext(); ) //for loop-as long as the file has   more to read
 {
    ++n; //Incrementing temp variable
    String line = sc.nextLine(); //Reading from the file
    if(rand.nextInt(n) == 0)
       result = line; //String result is now equal to the text imported from the file         
 }

 return result; //Returning the string      
}

 } //Ends Program
 // LosePanel.java
 import javax.swing.*;
 import java.awt.*;

 public class LosePanel extends JPanel
 {
 public LosePanel()
 {
setPreferredSize(new Dimension(500,500));
setBackground(Color.white);
}
    public void paintComponent(Graphics page) //DR
  {
  super.paintComponent(page);

page.drawOval(165,165,150,150);
 page.fillOval(165+47,165+25,10,10);
page.fillOval(165+92,165+25,10,10);

 page.drawArc(165+45,165+80,60,30,0,180);



 page.drawString("Better luck next time", 182,90);
 }
}

// WinPanel.java

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

    public class WinPanel extends JPanel
    {
     public WinPanel()
     {
    setPreferredSize(new Dimension(500,500));
    setBackground(Color.white);
    }
      public void paintComponent(Graphics page) //(DR)
    { 
      super.paintComponent(page);

    page.drawOval(165,165,150,150);
    page.fillOval(165+47,165+25,10,10);
    page.fillOval(165+92,165+25,10,10);

    page.drawArc(165+46,165+70,60,30,0,-180);

    page.drawString("You've won the game! Congratulations!", 150,90);
    }
   }

1 个答案:

答案 0 :(得分:2)

Guis是事件驱动的框架作品。也就是说,用户做某事并且你回应它

基本上,正在发生的事情是,你在实际完成任务之前检查Buttons的状态。

查看Creating a GUI in SwingHow to use buttonsHow to write an Action Listener,了解有关编写GUI的一些想法,示例和建议