我的JFrame无法以Java显示我的JPanel

时间:2018-12-09 19:16:18

标签: java jframe jpanel drjava

所以我最近是编程(类)的新手,并且我一直在尝试让我的JPanel在运行代码时显示,但是什么也没发生(只是空白的灰色屏幕)。我不知道怎么了,DrJava不会显示任何错误,因此它一定是运行时错误,但我不知道它是什么。这是代码:

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

public class Findme extends JPanel implements ActionListener {

  CardLayout cdLayout = new CardLayout (); //Global Variable

  public static void main (String[] args){

    Findme content = new Findme();

    JFrame window = new JFrame("Finding me");

    window.setContentPane(content);
    window.setSize(345,453);
    window.setLocation(100, 100);
    window.setVisible(true);
  }//end main

  public void Findme(){
    setLayout (cdLayout);
    screen1();
    screen2();
    screen3();
    screen4();
    screen5();
    screen6();
    screen7();
  }//end constructor

  public void screen1 () {
    JPanel card1 = new JPanel();
    card1.setBackground (Color.white);

    JLabel title = new JLabel ("You are in the hall.");
    title.setForeground(new Color(154,41,154));
    title.setFont(new Font("Arial", Font.PLAIN, 25));

    JLabel info = new JLabel ("You did not find me!");
    info.setForeground(Color.red);
    info.setFont(new Font("Arial", Font.PLAIN, 17));

    JLabel picture = new JLabel (createImageIcon ("hall.jpg"));

    JLabel goTo = new JLabel ("Go to:");
    goTo.setForeground(Color.black);
    goTo.setFont(new Font("Arial", Font.PLAIN, 20));


    JButton firstChoice = new JButton ("Study");
    firstChoice.setActionCommand("2");
    firstChoice.addActionListener(this);

    JButton secondChoice = new JButton ("Lounge");
    secondChoice.setActionCommand("3");
    secondChoice.addActionListener(this);

    JButton thirdChoice = new JButton ("Ballroom");
    thirdChoice.setActionCommand("7");
    thirdChoice.addActionListener(this);

    card1.add(title);
    card1.add(picture);
    card1.add(info);
    card1.add(goTo);
    card1.add(firstChoice);
    card1.add(secondChoice);
    card1.add(thirdChoice);
    add ("1", card1);
  }//end screen1

  public void screen2 () {
    JPanel card2 = new JPanel();
    card2.setBackground(Color.white);

    JLabel title = new JLabel ("You are in the study room.");
    title.setForeground(new Color(154,41,154));
    title.setFont(new Font("Arial", Font.PLAIN, 25));

    JLabel info = new JLabel ("You did not find me!");
    info.setForeground(Color.red);
    info.setFont(new Font("Arial", Font.PLAIN, 17));

    JLabel picture = new JLabel (createImageIcon ("Study.jpg"));

    JLabel goTo = new JLabel ("Go to:");
    goTo.setForeground(Color.black);
    goTo.setFont(new Font("Arial", Font.PLAIN, 20));


    JButton firstChoice = new JButton ("Hall");
    firstChoice.setActionCommand("1");
    firstChoice.addActionListener(this);

    JButton secondChoice = new JButton ("Library");
    secondChoice.setActionCommand("4");
    secondChoice.addActionListener(this);

    card2.add(title);
    card2.add(picture);
    card2.add(info);
    card2.add(goTo);
    card2.add(firstChoice);
    card2.add(secondChoice);
    add ("2", card2);
  }//end screen2

  public void screen3 () {
    JPanel card3 = new JPanel();
    card3.setBackground (Color.white);

    JLabel title = new JLabel ("You are in the lounge.");
    title.setForeground(new Color(154,41,154));
    title.setFont(new Font("Arial", Font.PLAIN, 25));

    JLabel info = new JLabel ("You did not find me!");
    info.setForeground(Color.red);
    info.setFont(new Font("Arial", Font.PLAIN, 17));

    JLabel picture = new JLabel (createImageIcon ("Lounge.jpg"));

    JLabel goTo = new JLabel ("Go to:");
    goTo.setForeground(Color.black);
    goTo.setFont(new Font("Arial", Font.PLAIN, 20));


    JButton firstChoice = new JButton ("Hall");
    firstChoice.setActionCommand("1");
    firstChoice.addActionListener(this);

    JButton secondChoice = new JButton ("Conservatory");
    secondChoice.setActionCommand("5");
    secondChoice.addActionListener(this);

    card3.add(title);
    card3.add(picture);
    card3.add(info);
    card3.add(goTo);
    card3.add(firstChoice);
    card3.add(secondChoice);
    add ("3", card3);
  }//end screen3

  public void screen4 () {
    JPanel card4 = new JPanel();
    card4.setBackground (Color.white);

    JLabel title = new JLabel ("You are in the library.");
    title.setForeground(new Color(154,41,154));
    title.setFont(new Font("Arial", Font.PLAIN, 25));

    JLabel info = new JLabel ("You did find me!");
    info.setForeground(Color.green);
    info.setFont(new Font("Arial", Font.PLAIN, 17));

    JLabel picture = new JLabel (createImageIcon ("library.jpg"));

    JLabel goTo = new JLabel ("Go to:");
    goTo.setForeground(Color.black);
    goTo.setFont(new Font("Arial", Font.PLAIN, 20));


    JButton firstChoice = new JButton ("Study");
    firstChoice.setActionCommand("2");
    firstChoice.addActionListener(this);

    JButton secondChoice = new JButton ("Billard Room");
    secondChoice.setActionCommand("6");
    secondChoice.addActionListener(this);

    card4.add(title);
    card4.add(picture);
    card4.add(info);
    card4.add(goTo);
    card4.add(firstChoice);
    card4.add(secondChoice);
    add ("4", card4);
  }//end screen4

  public void screen5 () {
    JPanel card5 = new JPanel();
    card5.setBackground (Color.cyan);


    JLabel title = new JLabel ("You are in the conservatory.");
    title.setForeground(new Color(154,41,154));
    title.setFont(new Font("Arial", Font.PLAIN, 25));

    JLabel info = new JLabel ("You did not find me!");
    info.setForeground(Color.red);
    info.setFont(new Font("Arial", Font.PLAIN, 17));

    JLabel picture = new JLabel (createImageIcon ("conservatory.jpg"));

    JLabel goTo = new JLabel ("Go to:");
    goTo.setForeground(Color.black);
    goTo.setFont(new Font("Arial", Font.PLAIN, 20));


    JButton firstChoice = new JButton ("Lounge");
    firstChoice.setActionCommand("3");
    firstChoice.addActionListener(this);

    JButton secondChoice = new JButton ("BallRoom");
    secondChoice.setActionCommand("7");
    secondChoice.addActionListener(this);

    card5.add(title);
    card5.add(picture);
    card5.add(info);
    card5.add(goTo);
    card5.add(firstChoice);
    card5.add(secondChoice);
    add ("5", card5);
  }//end screen5

  public void screen6 () {
    JPanel card6 = new JPanel ();
    card6.setBackground(Color.white);

    JLabel title = new JLabel ("You are in the billard room.");
    title.setForeground(new Color(154,41,154));
    title.setFont(new Font("Arial", Font.PLAIN, 25));

    JLabel info = new JLabel ("You did not find me!");
    info.setForeground(Color.red);
    info.setFont(new Font("Arial", Font.PLAIN, 17));

    JLabel picture = new JLabel (createImageIcon ("BRoom.jpg"));

    JLabel goTo = new JLabel ("Go to:");
    goTo.setForeground(Color.black);
    goTo.setFont(new Font("Arial", Font.PLAIN, 20));


    JButton firstChoice = new JButton ("Library");
    firstChoice.setActionCommand("4");
    firstChoice.addActionListener(this);

    JButton secondChoice = new JButton ("BallRoom");
    secondChoice.setActionCommand("7");
    secondChoice.addActionListener(this);

    card6.add(title);
    card6.add(picture);
    card6.add(info);
    card6.add(goTo);
    card6.add(firstChoice);
    card6.add(secondChoice);
    add ("6", card6);
  }

  public void screen7 () {
    JPanel card7 = new JPanel ();
    card7.setBackground(Color.white);

    JLabel title = new JLabel ("You are in the ballroom.");
    title.setForeground(new Color(154,41,154));
    title.setFont(new Font("Arial", Font.PLAIN, 25));

    JLabel info = new JLabel ("You did not find me!");
    info.setForeground(Color.red);
    info.setFont(new Font("Arial", Font.PLAIN, 17));

    JLabel picture = new JLabel (createImageIcon ("BallRoom.jpg"));

    JLabel goTo = new JLabel ("Go to:");
    goTo.setForeground(Color.black);
    goTo.setFont(new Font("Arial", Font.PLAIN, 20));

    JButton firstChoice = new JButton ("Hall");
    firstChoice.setActionCommand("1");
    firstChoice.addActionListener(this);

    JButton secondChoice = new JButton ("conservatory");
    secondChoice.setActionCommand("5");
    secondChoice.addActionListener(this);

    JButton thirdChoice = new JButton ("Billard Room");
    thirdChoice.setActionCommand("6");
    thirdChoice.addActionListener(this);


    card7.add(title);
    card7.add(picture);
    card7.add(info);
    card7.add(goTo);
    card7.add(firstChoice);
    card7.add(secondChoice);
    card7.add(thirdChoice);
    add ("7", card7);
  }

  public void actionPerformed (ActionEvent e){
//moves between the screens
    if (e.getActionCommand().equals ("1")) {
      cdLayout.show (this, "1");
    } else if (e.getActionCommand().equals ("2")) {
      cdLayout.show (this, "2");
    } else if (e.getActionCommand().equals ("3")) {
      cdLayout.show (this, "3");
    } else if (e.getActionCommand().equals ("4")) {
      cdLayout.show (this, "4");
    } else if (e.getActionCommand().equals ("5")) {
      cdLayout.show (this, "5");
    }else if (e.getActionCommand().equals ("6")) {
      cdLayout.show(this, "6");
    }else if (e.getActionCommand().equals ("7")) {
      cdLayout.show(this, "7");
    }
  }//end actionPerformed

  protected static ImageIcon createImageIcon (String path)
  {
    java.net.URL imgURL = Findme.class.getResource (path);
    if (imgURL != null)
    {
      return new ImageIcon (imgURL);
    }else
    {
      System.err.println("Couldn't find file: " + path);
      return null;
    }
  } 
}//end class

0 个答案:

没有答案