我是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
}
}
}
}
主要方法:
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);
}
}
答案 0 :(得分:1)
正如@nIcEcOw所说..
..只使用
JLabel.setIcon ( newImage )
而不是无缘无故地重新创建JLabel
这是实现该想法的代码版本,但几乎没有其他更改。它仍然需要在评论和其他调整中提到的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);
}
}