简体   繁体   English

将图像添加到JFrame?

[英]add image to JFrame?

I'm having trouble getting the program to paint an image on. 我在获取程序上绘制图像时遇到麻烦。 I'm not sure how it's meant to work, but I've defined paintComponent(Graphics g), but the program doesnt seem to be detecting it. 我不确定它是如何工作的,但是我已经定义了paintComponent(Graphics g),但是程序似乎没有检测到它。 Am i doing something wrong? 难道我做错了什么?

Here's the code I have btw 这是我有的代码

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!");
  }
}

A JFrame is not the best place to draw an image. JFrame不是绘制图像的最佳位置。

According to this question and answer , you might want to consider one of the following approaches: 根据此问题和答案 ,您可能需要考虑以下方法之一:

  • Use a JLabel with an icon. JLabel与图标一起使用。
  • Use a JPanel containing an instance of JCustomComponent , which is a class that extends JComponent and overrides the paintComponent() method to draw the desired image. 使用包含JCustomComponent实例的JPanel ,该实例是扩展JComponent并重写paintComponent()方法的类以绘制所需的图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM