首先,我要为我的女朋友在2天内为她的生日制作节目。我现在遇到的问题是我的GUI中的按钮不起作用。当有人点击按钮时,它应该显示我设置的图片,它是程序文件中的jpg。相反,它什么也没做。我想知道我是否可以得到一些帮助,为什么按钮可能无法正常工作?它位于Happy_birthday()构造函数中。非常感谢您的宝贵时间。注意:在整个程序中有一些内部笑话和评论,如果它们看起来很麻烦,只是试图让她发笑就是抱歉。
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;
}
}
}
}
答案 0 :(得分:-1)
您可以这样尝试:(将您的操作执行代码更改为以下代码)
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();
}