我正在尝试从单独的类文件中加载随机图像。
import java.util.*;
import java.io.*;
public class random {
static String back01;
public static void ran(int number) {
switch(number) {
case 1:
setImage("images/01.jpg");
break;
case 2:
setImage("images/02.jpg");
break;
case 3:
setImage("images/03.jpg");
break;
case 4:
setImage("images/04.jpg");
break;
case 5:
setImage("images/05.jpg");
break;
case 6:
setImage("images/06.jpg");
break;
case 7:
setImage("images/07.jpg");
break;
case 8:
setImage("images/08.jpg");
break;
case 9:
setImage("images/09.jpg");
break;
case 10:
setImage("images/10.jpg");
break;
default:
System.out.println("Invalid number");
}
}
public static void setImage(String back01) {
String image = back01;
}
public static int getImage() {
String test;
String set = setImage(test);
return set;
}
}
我遇到了如何将函数调用到我的主类,无论我做什么,我在编译时都会遇到不兼容的类型错误。如果在我回家的时候通过备份我的代码来访问我的usb,那么我将发布一个与此执行相关的代码块。
int number = rand.nextInt(10) + 1;
random.ran(number);
然后我尝试使用
将它放到imageicon.getResource(random.getImage());
然后它会通过一个错误说我在该函数中没有空白 这是我的主要功能,最后可以访问我的代码。
/**
* Write a description of class main here.
*
* @author (your name)
* @version (a version number or a date)
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import sun.audio.*;
import java.util.*;
public class test
{
private static final int WIDTH = 800;
private static final int HEIGHT = 600;
JButton start, exit;
static String back, back01;
Random rand = new Random();
private ExitButtonHandler ebHandler;
//private StartButtonHandler sbHandler;
public test()
{
int test = rand.nextInt(10) + 1;
random.ran(test);
/*switch(rand_background)
{
case 1:
back = "images/01.jpg";
break;
case 2:
back = "images/02.jpg";
break;
case 3:
back = "images/03.jpg";
break;
case 4:
back = "images/04.jpg";
break;
case 5:
back = "images/05.jpg";
break;
case 6:
back = "images/06.jpg";
break;
case 7:
back = "images/07.jpg";
break;
case 8:
back = "images/08.jpg";
break;
case 9:
back = "images/09.jpg";
break;
case 10:
back = "images/10.jpg";
break;
default:
System.out.println("Invalid number");
}
int rand_background01 = rand.nextInt(10) + 1;*/
JFrame frame = new JFrame("Until the End: The Fallen Souls");
//sound.music("music/theme01.wav");
String testing = random.getImage() + "";
ImageIcon image = new ImageIcon(getClass().getResource(random.getImage()));
//ImageIcon image01 = new ImageIcon(getClass().getResource(back01));
start = new JButton("Start game...");
exit = new JButton("Exit game...");
JLabel background = new JLabel();
background.setIcon(image);
background.setLayout(new BorderLayout());
JLabel background01 = new JLabel();
//background01.setIcon(image01);
//background01.setLayout(new BorderLayout());
JPanel panel = new JPanel();
panel.add(background);
//panel.add(background01);
panel.setLayout(new GridBagLayout());
panel.setOpaque(false);
ebHandler = new ExitButtonHandler();
exit.addActionListener(ebHandler);
panel.add(start);
panel.add(Box.createRigidArea(new Dimension(150, 0)));
panel.add(exit);
frame.add(panel);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setUndecorated(true);
frame.setVisible(true);
}
public class ExitButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
public static void main(String args[])
{
test menu = new test();
}
}
使用此代码,我现在通过更改背景来尝试非常基本的程序生成。当我走的时候会更复杂但是现在我们还没有创建任何瓷砖,所以我不能深入了解代码,直到我有一些东西可以试验。
编辑:
我设法让它工作,但我不得不删除我的setImage方法。有没有办法将其纳入其中?我的代码看起来比这个好多了
/**
* Write a description of class random here.
*
* @author (your name)
* @version (a version number or a date)
*/
import java.util.*;
import java.io.*;
public class random
{
public static String back01;
public static void ran(int number)
{
switch(number)
{
case 1:
back01 = "images/01.jpg";
break;
case 2:
back01 = "images/02.jpg";
break;
case 3:
back01 = "images/03.jpg";
break;
case 4:
back01 = "images/04.jpg";
break;
case 5:
back01 = "images/05.jpg";
break;
case 6:
back01 = "images/06.jpg";
break;
case 7:
back01 = "images/07.jpg";
break;
case 8:
back01 = "images/08.jpg";
break;
case 9:
back01 = "images/09.jpg";
break;
case 10:
back01 = "images/10.jpg";
break;
default:
System.out.println("Invalid number");
}
}
public static void setImage(String back01)
{
back01 = random.back01;
}
public static String getImage()
{
return random.back01;
}
}
当我尝试使用System.out.println(random.getImage())时,我会在back01前面带有和不带随机的null
EDIT /解决:
我明白了!谢谢大家的指导。这是我修复setImage函数的方法。
/**
* Write a description of class random here.
*
* @author (your name)
* @version (a version number or a date)
*/
import java.util.*;
import java.io.*;
public class random
{
public static String back01;
public static String test;
public static void ran(int number)
{
switch(number)
{
case 1:
setImage("images/01.jpg");
break;
case 2:
setImage("images/02.jpg");
break;
case 3:
setImage("images/03.jpg");
break;
case 4:
setImage("images/04.jpg");
break;
case 5:
setImage("images/05.jpg");
break;
case 6:
setImage("images/06.jpg");
break;
case 7:
setImage("images/07.jpg");
break;
case 8:
setImage("images/08.jpg");
break;
case 9:
setImage("images/09.jpg");
break;
case 10:
setImage("images/10.jpg");
break;
default:
System.out.println("Invalid number");
}
}
public static void setImage(String back01)
{
test = back01;
}
public static String getImage()
{
return test;
}
}
答案 0 :(得分:0)
为什么当方法签名将返回类型显示为int?
时,您的方法是否尝试返回String public static int getImage() {
String test;
String set = setImage(test);
return set;
}
答案 1 :(得分:0)
你应该有这样的东西:
import java.util.*;
import java.io.*;
public class Random
{
private String back01;
public static void ran(int number) {
switch(number) {
//All options
}
}
//If you only use "setImage()" in the class is not necessary
//declare this one as "public static"
//public static void setImage(String back01) {
private void setImage(String back01) {
this.back01 = back01;
}
public static String getImage() {
return back01;
}
}
然后证明:
int number = rand.nextInt(10) + 1;
random.ran(number);
.getResource(random.getImage());