在重新调整窗口大小之前,图像不会出现

时间:2022-02-14 21:12:46

I am very new to Java and Swing, so I am trying to make a little application to practice it. In it the user chooses a pizza type from a group of radio buttons then "orders" the pizza and an image to match the pizza type is displayed. My problem is that the image will not be displayed unless I re-size the window. Where am I going wrong?
Here's my source code:

我是Java和Swing的新手,所以我想尝试一些应用程序来实践它。在其中,用户从一组单选按钮中选择比萨饼类型,然后“订购”比萨饼并显示匹配比萨饼类型的图像。我的问题是除非我重新调整窗口大小,否则不会显示图像。我哪里错了?这是我的源代码:

package gui;
import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
public class GUI extends JFrame{
    //all my variables
    private JRadioButton pepperoni;
    private JRadioButton cheese;
    private JRadioButton jalepeno;
    private JRadioButton sausage;
    private JRadioButton hawiian;
    private JRadioButton spinach;
    private ButtonGroup group;
    private ImageIcon pizza;
    private JLabel displayed;
    private JButton submit;
    public GUI () {
        super("Order a Pizza");
        setLayout(new FlowLayout());
        //give instructions and assign variables
        JLabel instructions = new JLabel("Choose which Pizza you want to order");
        add(instructions);
        pepperoni = new JRadioButton("Pepperoni Pizza", true);
        cheese = new JRadioButton("Cheese Pizza", false);
        jalepeno = new JRadioButton("Hot Jalepeno Pizza", false);
        sausage = new JRadioButton("Sausage Pizza", false);
        hawiian = new JRadioButton("Hawaii Style - PineApple and sausage", false);
        spinach = new JRadioButton("Spinach Pizza", false);
        submit = new JButton("Order Pizza");
        //add to jframe
        add(pepperoni);
        add(cheese);
        add(jalepeno);
        add(sausage);
        add(hawiian);
        add(submit);
        add(spinach);
        //add to button group
        group = new ButtonGroup();
        group.add(pepperoni);
        group.add(cheese);
        group.add(jalepeno);
        group.add(sausage);
        group.add(hawiian);
        group.add(spinach);
        //add event listeners
        pepperoni.addItemListener(new RadioHandler(new ImageIcon(getClass().getResource("pepperoni.jpg"))));
        cheese.addItemListener(new RadioHandler(new ImageIcon(getClass().getResource("download.jpg"))));
        jalepeno.addItemListener(new RadioHandler(new ImageIcon(getClass().getResource("jalepeno.jpg"))));
        sausage.addItemListener(new RadioHandler(new ImageIcon(getClass().getResource("sausage.jpg"))));
        hawiian.addItemListener(new RadioHandler(new ImageIcon(getClass().getResource("hawiian.png"))));
        submit.addActionListener(new SubmitHandler());
    }
    //class that sets file when radio is clicked
    public class RadioHandler implements ItemListener {

        ImageIcon file;
        //constructor for whatever image should be displayed when clicked
        public RadioHandler (ImageIcon img) {
            file = img;
        }
        //when clicked set that image
        public void itemStateChanged(ItemEvent event) {
            pizza = file;
            displayed = new JLabel(pizza);
        }

    }
    //class that displays image when user submits
    public class SubmitHandler implements ActionListener {

        public void actionPerformed (ActionEvent event) {
            if (event.getSource() instanceof JButton) {//if the button is clicked - i dont think i really need this
                add(displayed);//add image
            }

        }

    }
}

Main method:

package main;
 import gui.GUI;
public class Main {

    public static void main(String[] args) {
        GUI go = new GUI();
        go.setSize(800, 600);
        go.setVisible(true);
    }

}

1 个解决方案

#1


1  

As mentioned by @nIcEcOw..

正如@nIcEcOw所提到的..

..just use JLabel.setIcon ( newImage ) instead of recreating the JLabel for no reasons

..只是使用JLabel.setIcon(newImage)而不是无缘无故地重新创建JLabel

Here is a version of the code that implements that idea, but makes few other changes. It still needs to have the GUI creation put on the EDT as mentioned in comments, and other tweaks.

这是实现该想法的代码版本,但几乎没有其他更改。它仍然需要在评论和其他调整中提到的EDT上创建GUI。

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

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;

public class GUI extends JFrame {

    //all my variables
    private JRadioButton pepperoni;
    private JRadioButton cheese;
    private JRadioButton jalepeno;
    private JRadioButton sausage;
    private JRadioButton hawiian;
    private JRadioButton spinach;
    private ButtonGroup group;
    private ImageIcon pizza;
    private JLabel displayed = new JLabel();
    private JButton submit;

    public GUI() {
        super("Order a Pizza");
        setLayout(new FlowLayout());
        //give instructions and assign variables
        JLabel instructions = new JLabel("Choose which Pizza you want to order");
        add(instructions);
        pepperoni = new JRadioButton("Pepperoni Pizza", true);
        cheese = new JRadioButton("Cheese Pizza", false);
        jalepeno = new JRadioButton("Hot Jalepeno Pizza", false);
        sausage = new JRadioButton("Sausage Pizza", false);
        hawiian = new JRadioButton("Hawaii Style - PineApple and sausage", false);
        spinach = new JRadioButton("Spinach Pizza", false);
        submit = new JButton("Order Pizza");
        //add to jframe
        add(pepperoni);
        add(cheese);
        add(jalepeno);
        add(sausage);
        add(hawiian);
        add(submit);
        add(spinach);
        //add to button group
        group = new ButtonGroup();
        group.add(pepperoni);
        group.add(cheese);
        group.add(jalepeno);
        group.add(sausage);
        group.add(hawiian);
        group.add(spinach);
        //add event listeners
        pepperoni.addItemListener(new RadioHandler(new ImageIcon(getImage(20, 80))));
        cheese.addItemListener(new RadioHandler(new ImageIcon(getImage(20, 40))));
        jalepeno.addItemListener(new RadioHandler(new ImageIcon(getImage(80, 20))));
        sausage.addItemListener(new RadioHandler(new ImageIcon(getImage(80, 60))));
        hawiian.addItemListener(new RadioHandler(new ImageIcon(getImage(80, 40))));
        submit.addActionListener(new SubmitHandler());

        add(displayed);//add image
    }

    private static Image getImage(int w, int h) {
        return new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    }

    //class that sets file when radio is clicked
    public class RadioHandler implements ItemListener {

        ImageIcon file;

        //constructor for whatever image should be displayed when clicked
        public RadioHandler(ImageIcon img) {
            file = img;
        }

        //when clicked set that image
        public void itemStateChanged(ItemEvent event) {
            pizza = file;

        }
    }

    //class that displays image when user submits
    public class SubmitHandler implements ActionListener {

        public void actionPerformed(ActionEvent event) {
            if (event.getSource() instanceof JButton) {//if the button is clicked - i dont think i really need this
                displayed.setIcon(pizza);
            }
        }
    }

    public static void main(String[] args) {
        GUI go = new GUI();
        go.setSize(800, 600);
        go.setVisible(true);
    }
}

#1


1  

As mentioned by @nIcEcOw..

正如@nIcEcOw所提到的..

..just use JLabel.setIcon ( newImage ) instead of recreating the JLabel for no reasons

..只是使用JLabel.setIcon(newImage)而不是无缘无故地重新创建JLabel

Here is a version of the code that implements that idea, but makes few other changes. It still needs to have the GUI creation put on the EDT as mentioned in comments, and other tweaks.

这是实现该想法的代码版本,但几乎没有其他更改。它仍然需要在评论和其他调整中提到的EDT上创建GUI。

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

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;

public class GUI extends JFrame {

    //all my variables
    private JRadioButton pepperoni;
    private JRadioButton cheese;
    private JRadioButton jalepeno;
    private JRadioButton sausage;
    private JRadioButton hawiian;
    private JRadioButton spinach;
    private ButtonGroup group;
    private ImageIcon pizza;
    private JLabel displayed = new JLabel();
    private JButton submit;

    public GUI() {
        super("Order a Pizza");
        setLayout(new FlowLayout());
        //give instructions and assign variables
        JLabel instructions = new JLabel("Choose which Pizza you want to order");
        add(instructions);
        pepperoni = new JRadioButton("Pepperoni Pizza", true);
        cheese = new JRadioButton("Cheese Pizza", false);
        jalepeno = new JRadioButton("Hot Jalepeno Pizza", false);
        sausage = new JRadioButton("Sausage Pizza", false);
        hawiian = new JRadioButton("Hawaii Style - PineApple and sausage", false);
        spinach = new JRadioButton("Spinach Pizza", false);
        submit = new JButton("Order Pizza");
        //add to jframe
        add(pepperoni);
        add(cheese);
        add(jalepeno);
        add(sausage);
        add(hawiian);
        add(submit);
        add(spinach);
        //add to button group
        group = new ButtonGroup();
        group.add(pepperoni);
        group.add(cheese);
        group.add(jalepeno);
        group.add(sausage);
        group.add(hawiian);
        group.add(spinach);
        //add event listeners
        pepperoni.addItemListener(new RadioHandler(new ImageIcon(getImage(20, 80))));
        cheese.addItemListener(new RadioHandler(new ImageIcon(getImage(20, 40))));
        jalepeno.addItemListener(new RadioHandler(new ImageIcon(getImage(80, 20))));
        sausage.addItemListener(new RadioHandler(new ImageIcon(getImage(80, 60))));
        hawiian.addItemListener(new RadioHandler(new ImageIcon(getImage(80, 40))));
        submit.addActionListener(new SubmitHandler());

        add(displayed);//add image
    }

    private static Image getImage(int w, int h) {
        return new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    }

    //class that sets file when radio is clicked
    public class RadioHandler implements ItemListener {

        ImageIcon file;

        //constructor for whatever image should be displayed when clicked
        public RadioHandler(ImageIcon img) {
            file = img;
        }

        //when clicked set that image
        public void itemStateChanged(ItemEvent event) {
            pizza = file;

        }
    }

    //class that displays image when user submits
    public class SubmitHandler implements ActionListener {

        public void actionPerformed(ActionEvent event) {
            if (event.getSource() instanceof JButton) {//if the button is clicked - i dont think i really need this
                displayed.setIcon(pizza);
            }
        }
    }

    public static void main(String[] args) {
        GUI go = new GUI();
        go.setSize(800, 600);
        go.setVisible(true);
    }
}