我試圖製作一個程序,可以讓您選擇想要的圖像數量,然後它會詢問您要使用的文件的名稱。然後它將以適當的方式將它們打印在屏幕上。將一些圖像繪製成一個窗格
現在我知道這個代碼的很多部分,如寬度和高度,行和列,以及其他的東西都是錯誤的,我打算在我弄清楚之後修復它們。不管我如何改變它,它都不讓我使用paintComponent。我想保留在我的主課堂中,這仍然有可能嗎?很多時候我把它放在一個單獨的圖形類中,但是把我的輸入引入到這個類中是令人惱怒的。
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Core extends JPanel{
public static void main(String[] args){
Scanner r = new Scanner(System.in);
System.out.println("How many selections will you have? 1, 2 or 4? ");
if(r.next().contains("1") | r.next().contains("2") | r.next().contains("4")){
String selections = r.next();
int number = Integer.parseInt(selections);
ArrayList<String> images = new ArrayList();
for(int j = 1; j <= number; j++){
System.out.println("Your options are boo, bae, skinny, bro...");
System.out.println("Name of image " + j + "? ");
if(r.next().contains("boo") | r.next().contains("bae") | r.next().contains("skinny") | r.next().contains("bro"))
images.add(r.next());
else{
System.out.println("Im sorry that was an improper input...");
System.out.println("Next time, remember, you can only input boo, bae, skinny, or bro.");
r.close();
}
}
JFrame theGUI = new JFrame();
theGUI.setTitle("Random Images");
theGUI.setSize(number * 100, number * 100);//ratio of 1:100
theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = theGUI.getContentPane();
pane.setLayout(new GridLayout(number, number));
for(int j = 1; j <= number; j++){
Color backColor = Color.white;
Image image = new ImageIcon((images.get(j)) + ".jpg").getImage();
public paintComponent(Graphics g){//error here(paintComponent cannot be resolved to a variable)(Illegal modifier for 'g')
super.paintComponent(Graphics g);//and error here (cannot use super)(Graphics cannot be resokved to a variable)
g.drawImage(image, 0, 0, 100, 100, null);}
ColorPanel panelz = new ColorPanel(backColor);
pane.add(panelz);
}
theGUI.setVisible(true);
}//end of the checker for the 1, 2 and 4.
else {
System.out.println("I'm sorry that was an improper input.");
System.out.println("Keep in mind your inputs may only be 1, 2 or 4.");
}
}
}
在評論中張貼代碼的道歉,我是新來的。但是當我喜歡你說的時候,我爲變量聲明瞭什麼ID?
public class Core extends JPanel{
public static Image image;
public void paintComponent(g){//insert VariableDeclaratorID error
super.paintComponent(g);
g.drawImage(image, 0, 0, 100, 100, null);
}
public static void main(String[] args){
您正在聲明主方法內部的方法,它不是合適的語法。聲明它在你的主要方法之外,但仍然在你的班級 – 2014-11-06 01:09:07
@VinceEmigh好吧lemme通過真正的快速運行,並嘗試... – Kotite 2014-11-06 01:10:52
事實上,你已經得到**所有內部的主要方法,並沒有任何類適當。我建議1)閱讀Swing圖形教程(或重新閱讀它們),閱讀OOP和Java,然後從頭開始重新嘗試這個東西。理解主要的方法應該是**非常短,並且應該讓程序運行,就是這樣。 – 2014-11-06 01:12:38