Ich habe Probleme, das Programm ein Bild zu malen. Ich bin mir nicht sicher, wie es funktionieren soll, aber ich habe paintComponent (Graphics g) definiert, aber das Programm scheint es nicht zu erkennen. Mache ich etwas falsch?Bild zu JFrame hinzufügen?
Hier ist der Code, den ich habe 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!");
}
}
Wo fügen Sie der Benutzeroberfläche (z. B. Mainframe) das 'JPanel' hinzu? – copeg
Beginnen Sie mit dem Lesen des [Swing-Tutorials] (http://docs.oracle.com/javase/tutorial/uiswing/TOC.html) für Swing-Grundlagen. Ihr Code ist eine Kombination aus AWT- und Swing-Komponenten. Verwenden Sie keine AWT-Komponenten in einer Swing-Anwendung. Swing-Komponenten beginnen mit "J". Es gibt keine Notwendigkeit für benutzerdefinierte Malerei. Benutze einfach ein JLabel mit einem Icon. Lesen Sie den Abschnitt über die Verwendung von Symbolen aus dem Tutorial. – camickr
1) Um eine bessere Hilfe zu erhalten, geben Sie ein [MCVE] oder [Short, Self Contained, Correct Example] (http://www.sscce.org/) ein. 2) Eine Möglichkeit, Bilder für ein Beispiel zu erhalten, besteht darin, eine direkte Verbindung zu Bildern herzustellen, die in [diesem Q & A] (http://stackoverflow.com/q/19209650/418556) zu sehen sind. –