将图像添加到 JFrame?
add image to JFrame?
我无法让程序在其上绘制图像。我不确定它是如何工作的,但我已经定义了 paintComponent(Graphics g),但程序似乎没有检测到它。我做错了什么吗?
顺便说一句,这是我的代码
import java.util.Scanner;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
import java.awt.Image;
//add actionlistener event later
public class ZodiacKiller extends JPanel{
private JFrame mainframe;
private Choice month;
private Choice day;
private Label desc;
private Label dayDesc;
private int monthSelect;
private int daySelect;
private Button calc;
private Image[] images;
private String[] zodiacNames = {"aquarius","pisces","aries","taurus","gemini","cancer","leo","virgo","libra","scorpio","saggitarus","capricorn"};
private Image imageSelect;
public ZodiacKiller(){
guiProgram();
}
public static void main(String[] args){
ZodiacKiller ted = new ZodiacKiller();
}
public void guiProgram(){
mainframe = new JFrame("Zodiac Days 2016");
mainframe.setSize(485,300);
mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainframe.setBackground(Color.WHITE);
//figure out how layouts work in general
mainframe.setLayout(new FlowLayout());
Container c = mainframe.getContentPane();
desc = new Label("Enter your month");
mainframe.add(desc);
month = new Choice();
month.add("1");
month.add("2");
month.add("3");
month.add("4");
month.add("5");
month.add("6");
month.add("7");
month.add("8");
month.add("9");
month.add("10");
month.add("11");
month.add("12");
mainframe.add(month);
month.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent ie){
monthSelect = Integer.parseInt(month.getSelectedItem());
dayGenerator(monthSelect);
buttonGenerator();
}
});
dayDesc = new Label("Enter your day");
mainframe.add(dayDesc);
mainframe.setVisible(true);
}
public void buttonGenerator(){
mainframe.setVisible(false);
if(calc != null){
mainframe.remove(calc);
calc = null;
oldChoiceDeleter();
}
calc = new Button("Calculate!");
mainframe.add(calc);
calc.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
daySelect = Integer.parseInt(day.getSelectedItem());
Zodiac teddie = new Zodiac(daySelect, monthSelect);
imageMount(teddie.zodiacCalc());
repaint();
System.out.println("everything works!");
}
});
mainframe.setVisible(true);
}
public void dayGenerator(int monthSelect){
mainframe.setVisible(false);
if(day != null){
mainframe.remove(day);
day = null;
oldChoiceDeleter();
}
day = new Choice();
day.add("1");
day.add("2");
day.add("3");
day.add("4");
day.add("5");
day.add("6");
day.add("7");
day.add("8");
day.add("9");
day.add("10");
day.add("11");
day.add("12");
day.add("13");
day.add("14");
day.add("15");
day.add("16");
day.add("17");
day.add("18");
day.add("19");
day.add("20");
day.add("21");
day.add("22");
day.add("23");
day.add("24");
day.add("25");
day.add("26");
day.add("27");
day.add("28");
day.add("29");
switch(monthSelect){
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
day.add("30");
day.add("31");
case 4: case 6: case 9:
day.add("30");
default:
}
mainframe.add(day);
mainframe.setVisible(true);
}
//start here
public void imageMount(int output){
images = new Image[12];
for (int i = 1; i<=12;i++){
Image temp = (new ImageIcon("img/no" + i + ".jpeg")).getImage();
images[i-1] = temp;
}
imageSelect = images[output - 1];
System.out.println(zodiacNames[output-1] + "\n" + output);
//make sure the image selection process works.
}
public static void oldChoiceDeleter(){
return;
}
public static void cmdPrompt(){
Scanner kb = new Scanner(System.in);
System.out.println("Day?");
int day = kb.nextInt();
System.out.println("Month? (1 ~ 12)");
int month = kb.nextInt();
Zodiac tedCruz = new Zodiac(day, month);
System.out.println(tedCruz.zodiacCalc());
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(imageSelect, 50, 50, null);
System.out.println("I went here!");
}
}
一个JFrame
不是画图的最佳位置。
根据 this question and answer,您可能需要考虑以下方法之一:
- 使用带有图标的
JLabel
。
- 使用包含
JCustomComponent
实例的 JPanel
,它是 class 扩展 JComponent
并覆盖 paintComponent()
方法来绘制所需的图片。
我无法让程序在其上绘制图像。我不确定它是如何工作的,但我已经定义了 paintComponent(Graphics g),但程序似乎没有检测到它。我做错了什么吗?
顺便说一句,这是我的代码
import java.util.Scanner;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
import java.awt.Image;
//add actionlistener event later
public class ZodiacKiller extends JPanel{
private JFrame mainframe;
private Choice month;
private Choice day;
private Label desc;
private Label dayDesc;
private int monthSelect;
private int daySelect;
private Button calc;
private Image[] images;
private String[] zodiacNames = {"aquarius","pisces","aries","taurus","gemini","cancer","leo","virgo","libra","scorpio","saggitarus","capricorn"};
private Image imageSelect;
public ZodiacKiller(){
guiProgram();
}
public static void main(String[] args){
ZodiacKiller ted = new ZodiacKiller();
}
public void guiProgram(){
mainframe = new JFrame("Zodiac Days 2016");
mainframe.setSize(485,300);
mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainframe.setBackground(Color.WHITE);
//figure out how layouts work in general
mainframe.setLayout(new FlowLayout());
Container c = mainframe.getContentPane();
desc = new Label("Enter your month");
mainframe.add(desc);
month = new Choice();
month.add("1");
month.add("2");
month.add("3");
month.add("4");
month.add("5");
month.add("6");
month.add("7");
month.add("8");
month.add("9");
month.add("10");
month.add("11");
month.add("12");
mainframe.add(month);
month.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent ie){
monthSelect = Integer.parseInt(month.getSelectedItem());
dayGenerator(monthSelect);
buttonGenerator();
}
});
dayDesc = new Label("Enter your day");
mainframe.add(dayDesc);
mainframe.setVisible(true);
}
public void buttonGenerator(){
mainframe.setVisible(false);
if(calc != null){
mainframe.remove(calc);
calc = null;
oldChoiceDeleter();
}
calc = new Button("Calculate!");
mainframe.add(calc);
calc.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
daySelect = Integer.parseInt(day.getSelectedItem());
Zodiac teddie = new Zodiac(daySelect, monthSelect);
imageMount(teddie.zodiacCalc());
repaint();
System.out.println("everything works!");
}
});
mainframe.setVisible(true);
}
public void dayGenerator(int monthSelect){
mainframe.setVisible(false);
if(day != null){
mainframe.remove(day);
day = null;
oldChoiceDeleter();
}
day = new Choice();
day.add("1");
day.add("2");
day.add("3");
day.add("4");
day.add("5");
day.add("6");
day.add("7");
day.add("8");
day.add("9");
day.add("10");
day.add("11");
day.add("12");
day.add("13");
day.add("14");
day.add("15");
day.add("16");
day.add("17");
day.add("18");
day.add("19");
day.add("20");
day.add("21");
day.add("22");
day.add("23");
day.add("24");
day.add("25");
day.add("26");
day.add("27");
day.add("28");
day.add("29");
switch(monthSelect){
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
day.add("30");
day.add("31");
case 4: case 6: case 9:
day.add("30");
default:
}
mainframe.add(day);
mainframe.setVisible(true);
}
//start here
public void imageMount(int output){
images = new Image[12];
for (int i = 1; i<=12;i++){
Image temp = (new ImageIcon("img/no" + i + ".jpeg")).getImage();
images[i-1] = temp;
}
imageSelect = images[output - 1];
System.out.println(zodiacNames[output-1] + "\n" + output);
//make sure the image selection process works.
}
public static void oldChoiceDeleter(){
return;
}
public static void cmdPrompt(){
Scanner kb = new Scanner(System.in);
System.out.println("Day?");
int day = kb.nextInt();
System.out.println("Month? (1 ~ 12)");
int month = kb.nextInt();
Zodiac tedCruz = new Zodiac(day, month);
System.out.println(tedCruz.zodiacCalc());
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(imageSelect, 50, 50, null);
System.out.println("I went here!");
}
}
一个JFrame
不是画图的最佳位置。
根据 this question and answer,您可能需要考虑以下方法之一:
- 使用带有图标的
JLabel
。 - 使用包含
JCustomComponent
实例的JPanel
,它是 class 扩展JComponent
并覆盖paintComponent()
方法来绘制所需的图片。