2016-08-04 12 views
-6

Also, um zu beginnen, baue ich ein Programm für meine Freundin für ihren Geburtstag in 2 Tagen. Das Problem, das ich gerade habe, ist, dass meine Schaltfläche in meiner GUI nicht funktioniert. Wenn jemand auf den Button klickt, soll es das Bild anzeigen, auf das ich es eingestellt habe, dass es ein JPG innerhalb der Programmdateien ist. Stattdessen tut es einfach nichts. Ich habe mich gefragt, ob ich Hilfe bekommen könnte, warum die Taste nicht funktioniert. Es befindet sich im Konstruktor Happy_birthday(). Vielen Dank für Ihre Zeit. Hinweis: Es gibt einige Insider-Witze und Kommentare während des gesamten Programms. Tut mir leid, wenn sie nur lästig scheinen, nur um sie zum Lachen zu bringen.Java Swing Button Aktion funktioniert nicht?

import java.util.Scanner; 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.*; 
import javax.swing.ImageIcon; 

public class Happy_birthday extends JFrame{ 
public Happy_birthday(){ 
    JPanel p1 = new JPanel(); 
    JPanel p2 = new JPanel(); 
    JPanel p3 = new JPanel(); 
    JPanel p4 = new JPanel(); 
    JPanel p5 = new JPanel(); 
    JLabel word1 = new JLabel("You might want to take a photo to remember the dates."); 
    JLabel word2 = new JLabel(" The format goes from year, to month, to day"); 
    JLabel word3 = new JLabel("|2014 1 22|"); 
    JLabel word4 = new JLabel("|2015 7 28|"); 
    JLabel word5 = new JLabel("|2014 1 10|"); 
    JLabel word6 = new JLabel("|2016 1 22|"); 
    JLabel word7 = new JLabel("|2015 9 1|"); 
    p1.add(word1); 
    p1.add(word2); 
    p1.add(word3); 
    p1.add(word4); 
    p1.add(word5); 
    p1.add(word6); 
    p1.add(word7); 

    JButton button1 = new JButton("Option 1 for favorite pic."); 
    button1.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent ae){ 
      ImageIcon icon = new ImageIcon("CassyPic.jpg"); 
      p2.add(new JLabel(icon)); 
     } 
    }); 
    p2.add(button1); 
    add(p1,BorderLayout.CENTER); 
    add(p2,BorderLayout.EAST); 
} 
public static void main(String args[]){ 
    Scanner input = new Scanner(System.in); 
    System.out.println("--- ---  ---  ---------- ---------- -- --"); 
    System.out.println("--- ---  -- -- ---  --- ---  --- -- --"); 
    System.out.println("---------  --- --- --- --- --- --- ----"); 
    System.out.println("--------- --------- --------- ---------  --"); 
    System.out.println("--- --- --  -- ---   ---   --"); 
    System.out.println("--- --- --  -- ---   ---   --"); 
    System.out.print("\n"); 
    System.out.println("---------  ----  ---------- ---------- --- ---"); 
    System.out.println("----------  --  --- ---  ---- --- ---"); 
    System.out.println("--- ---  --  --- ---  ---- ---------"); 
    System.out.println("--------  --  --------  ---- ---------"); 
    System.out.println("--- ---  --  --- ---  ---- --- ---"); 
    System.out.println("----------  --  --- ---  ---- --- ---"); 
    System.out.println("---------  ----  --- ---  ---- --- ---"); 
    System.out.print("\n"); 
    System.out.println("--------  --- -- --"); 
    System.out.println("---------  -- -- -- --"); 
    System.out.println("--- --- --- --- ----"); 
    System.out.println("--- --- --------- --"); 
    System.out.println("--- -- ---  --- --"); 
    System.out.println("------ ---  --- --"); 
    System.out.print("\n"); 
    System.out.println("OK, now since that is out of the way time to begin with the real fun :)"); 
    System.out.println("So first off a joke, what is a fish that has no eyes?"); 
    System.out.println("Find out the answer later. For now type in dates that you think are relevant to our relationship"); 
    String word=""; 
    System.out.println("For some hints, type in the word 'hint' to get some help"); 
    word = input.nextLine(); 
    if(word.equals("hint")){ 
     Happy_birthday TuSa = new Happy_birthday(); 
     TuSa.setTitle("Help window"); 
     TuSa.setSize(200,130); 
     TuSa.setLocationRelativeTo(null); 
     TuSa.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     TuSa.setVisible(true); 
    } 
    int choiceCount=0; 
    int day = 0, year = 0, month = 0; 
    while(choiceCount<5){ 
     boolean dayFlag = true; 
     boolean monthFlag = true; 
     boolean yearFlag = true; 
     while(dayFlag){ 
      System.out.print("Type in a day: "); 
      day = input.nextInt(); 
      if(day>31 || day<0){ 
       System.out.println("That isn't a legitimate day. Try again"); 
       dayFlag = true; 
      } 
      else{ 
       dayFlag = false; 
      } 
     } 
     while(monthFlag){ 
      System.out.print("Type in a number for the month: "); 
      month = input.nextInt(); 
      if(month<0 || month>12){ 
       System.out.println("That isn't a legitimate month. Try again"); 
       monthFlag = true; 
      } 
      else{ 
       monthFlag = false; 
      } 
     } 
     while(yearFlag){ 
      System.out.print("Type in the year(4 digits): "); 
      year = input.nextInt(); 
      if(year<2014 && year>1995){ 
       System.out.println("We were not even dating by this point. Try again."); 
       yearFlag = true; 
      } 
      else if(year<=1995){ 
       System.out.println("You were not even born so chill out lol."); 
       yearFlag = true; 
       //TU SA TU SA IN DUH AIR! 
      } 
      else if(year>2016){ 
       System.out.println("That hasn't even happened yet???"); 
       yearFlag = true; 
      } 
      else{ 
       yearFlag = false; 
      } 
     } 
     if(year == 2014 && month == 1 && day == 22){ 
      System.out.println("The day we started dating. Crazy how it's been almost 3 years :)"); 
      choiceCount++; 
     } 
     if(year == 2015 && month == 7 && day == 28){ 
      System.out.println("The day (or aorund that time) that we went to six flags and had Mt. tom's ice cream :)"); 
      choiceCount++; 
     } 
     if(year == 2014 && month == 1 && day == 10){ 
      System.out.println("Around this time is when you said I love you using Pokemon. I still love that you did that <3"); 
      choiceCount++; 
     } 
     if(year == 2016 && month == 1 && day == 22){ 
      System.out.println("The time we went to Ninja :)"); 
      choiceCount++; 
     } 
     if(year == 2015 && month == 9 && day == 1){ 
      System.out.println("Around this time is when I am hoping Orenda started? And they got the best president possible :)"); 
      choiceCount++; 
     } 
    } 
    System.out.println("Time to move onto somthing a bit different"); 
    System.out.println("I have compiled a couple of things for you."); 
    System.out.println("Type in 1 for a pic that I really like."); 
    System.out.println("Type in 2 for a pic from the place that we had our best date"); 
    System.out.println("Type in 3 for a random corny joke"); 
    System.out.println("Type in 4 to continue on :)"); 
    boolean inner_choice = true; 
    int inner_switch = 0; 
    while(inner_choice){ 
     switch(inner_switch){ 
     case(1): 

      inner_choice = true; 
     case(2): 

      inner_choice = true; 
     case(3): 

      inner_choice = true; 
     case(4): 
      inner_choice = false; 
     } 
    } 
} 
} 
+3

Für eine bessere Hilfe früher, Posten [MCVE] oder [Kurz, Self Contained, Korrekt Beispiel] (http://www.sscce.org/). Dies sollte nur eine Schaltfläche und ein Fenster benötigen, um es anzuzeigen. –

+1

Lösen Sie zunächst durch Vereinfachung: Versuchen Sie, das einfachste Programm zu erstellen, sagen Sie, dass eine einfache Hauptmethode eine ImageIcon in einem JOptionPane anzeigt (ja, Sie können tun dies - übergeben Sie es als zweiten Parameter), und versuchen Sie, Ihr Bild auf diese Weise zu zeigen, Debugging, wie Sie gehen. Als nächstes versuchen Sie, das Bild als Klassenressource und nicht als Datei zu erhalten ... –

+1

Erst nachdem Sie überprüft haben, dass Sie das Bild erhalten und anzeigen können, sollten Sie versuchen, es in die Haupt-GUI einzubinden. –

Antwort

-1

Sie können es so versuchen: (ändern Sie auf Aktion durchgeführt Code zu diesem Code unten)

try { 
    // read the image from the cource 
    BufferedImage read = ImageIO.read(Happy_birthday.class.getResourceAsStream("/CassyPic.jpg")); 
    // create icon 
    ImageIcon icon = new ImageIcon(read); 
    // add icon to the panel (p2) 
    p2.add(new JLabel(icon)); 
    // the most important thing that you must do is this 
    p2.revalidate(); // 2. re validate the panel (p2) to take effect. 
} catch (Exception e) { 
    e.printStackTrace(); 
} 
+0

Es gibt bessere Ansätze als dies. Einer erwähnt in einem Kommentar oben, der andere ist ein 'CardLayout' .. –

+0

Der Auftrag ist revalidate() und dann repaint(). Sie möchten den Layout-Manager aufrufen, bevor Sie alle Komponenten neu zeichnen. – camickr

+0

@AndrewThompson Ja du hast recht, ich löse gerade sein Problem an dem Punkt, den er will, ich habe die Antwort nicht komplex gemacht, vielleicht weil er es nicht verstehen konnte. –