【发布时间】:2014-05-05 02:09:49
【问题描述】:
我用 Java 制作了一个游戏,您应该在其中猜测一个随机生成的 int,介于 1-100 之间。如果你猜的太低,一个文本框会填满一个“太低”的文本,但是是瑞典语。如果你猜得太高,也一样。
当答案正确时就会出现问题,然后我调用一个方法使 Invisible JPanel 可见。这工作得很好,但 Jpanel 本身不会移动到它应该在的顶部。它需要完美契合,因为它只是一张经过照片处理的背景图片。 背景的属性是:1920 x 1080。photoshopped GG WP的属性是1920 x 297。Jpane被称为“免费”
我希望我没有错过任何重要的事情,我很感激我能得到的所有帮助,因为我现在卡住了。
附言。抱歉英文和格式不好。
package slumpatal;
import java.util.Random;
//This class runs the program, the main method is here.
public class SlumpaTal extends callback1 {
private int randomTal;
Random random = new Random();
@Override
public int SetValue(int value) {
if (value < randomTal)
return -1;
else if (value == randomTal)
return 0;
else
return 1;
}
//private JFrame1 frame;
SlumpaTal() {
GenerateRandomNumber();
JFrame1.createWindow(this);
}
public void GenerateRandomNumber() {
randomTal = random.nextInt(100) + 1;
}
public static void main(String[] args) {
new SlumpaTal();
}
}
.
package slumpatal;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
public class JFrame1 {
//public static int dittSvar;
public static void createWindow(callback1 callbackArg) {
//Font font = new Font("Verdana", Font.BOLD,28);
JFrame frame = new JFrame("Gissa ett Tal mellan 1-100");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setOpaque(true);
contentPane.setBorder(BorderFactory.createMatteBorder(5, 5, 5, 5, Color.WHITE));
contentPane.setBackground(Color.WHITE);
contentPane.setLayout(new BorderLayout(10, 10));
ImagePanel imagePanel = new ImagePanel(callbackArg);
//frame properties
contentPane.add(imagePanel, BorderLayout.CENTER);
frame.setContentPane(contentPane);
frame.setSize(1000, 600);
frame.setVisible(true);
}
}
class ImagePanel extends JPanel
{
callback1 callback;
public int dittSvar;
private BufferedImage image;
JButton restartApp;
JTextField dittSvarText1;
JTextField dittRes1;
BufferedImage myPicture;
public JPanel grattis;
public ImagePanel(callback1 callbackArg) {
try {
myPicture = ImageIO.read(new URL("https://imagizer.imageshack.us/v2/1375x213q90/843/r7f8.jpg"));
} catch (Exception ex) {
System.err.print("No reward image");
}
callback = callbackArg;
setOpaque(true);
setBorder(BorderFactory.createLineBorder(Color.BLACK, 5));
try {
image = ImageIO.read(new URL("http://imageshack.com/a/img835/193/v8k3.jpg"));
}
//If it doesn't work, write an error message, printStackTrace.
catch (IOException e) {
System.err.printf("%s", e.toString());
e.printStackTrace();
}
createGUI();
createGUI2();
dittSvarText1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dittSvar = Integer.parseInt(dittSvarText1.getText());
int res = callback.SetValue(dittSvar);
if (res < 0)
dittRes1.setText("För lågt");
else if (res == 0) {
dittRes1.setText("Rätt Svar!!!!");
makeItVisible();
} else
dittRes1.setText("För Högt");
dittSvarText1.setText("");
}
});
restartApp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
callback.GenerateRandomNumber();
dittSvarText1.setText(null);
dittRes1.setText(null);
grattis.setVisible(false);
}
});
}
public void makeItVisible() {
System.out.print("Showing reward image");
grattis = new JPanel();
grattis.setOpaque(false);
grattis.setLayout(new FlowLayout(FlowLayout.LEFT));
if (myPicture != null) {
JLabel picLabel = new JLabel(new ImageIcon(myPicture));
//picLabel.setLocation(new Point(0,0));
picLabel.setLocation(new Point(0, 0));
// picLabel.setSize(1000,297);
// grattis.setLocation(0, 0);
picLabel.setVerticalAlignment(WIDTH);
grattis.setVisible(true);
grattis.setSize(1000, 600);
grattis.add(picLabel);
add(grattis);
updateUI();
} else {
System.err.print("No picture created!");
}
}
public void createGUI() {
setLayout(new GridBagLayout());
JPanel panel1 = new JPanel();
panel1.setOpaque(false);
panel1.setLayout(new GridLayout(2, 2, 2, 2));
JLabel skrivdingissning = new JLabel("Skriv din gissning här : ");
skrivdingissning.setForeground(Color.WHITE);
dittSvarText1 = new JTextField(10);
JLabel VadBlevDet = new JLabel("Vad blev det : ");
VadBlevDet.setForeground(Color.WHITE);
dittRes1 = new JTextField(10);
panel1.add(skrivdingissning);
panel1.add(dittSvarText1);
panel1.add(VadBlevDet);
panel1.add(dittRes1);
add(panel1);
}
public void createGUI2() {
JPanel panel2 = new JPanel();
panel2.setLocation(100, 500);
panel2.setOpaque(false);
restartApp = new JButton("Starta om");
restartApp.setLocation(100, 500);
restartApp.setBackground(Color.WHITE);
panel2.add(restartApp);
// add(panel2);
}
@Override
public Dimension getPreferredSize() {
return (new Dimension(300, 300));
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
//g.drawImage(image, 0,0,this);
}
}
abstract class callback1 {
abstract int SetValue(int value);
abstract void GenerateRandomNumber();
}
【问题讨论】:
-
对于有关面板定位的问题,所有这些代码真的必要吗?见How to create a Minimal, Complete, and Verifiable example。请做。关键字是Minimum。并且请尝试正确缩进您的代码以使其更具可读性。在目前的状态下,它也可能是俄语
-
由于格式化而投反对票。伙计,如果有人应该帮助你,请认真地给他详细信息(只有必要的)并格式化它们,以便他可以轻松阅读。
-
来自@HenryKitchener 的评论:能否请您提供一个示例图片,我们不知道“不会移动到它应该在的顶部” i> 表示,顶部在哪里?
-
查看
CardLayout,而不是将面板设置为可见。对于该职位,可能就像在相关面板中添加EmtpyBorder一样简单。但要获得更多帮助,请按照@peeskillet 的建议发布 MCVE。 -
我将您的代码提取到 Eclipse 中并让它运行。图片是在线的。伙计,你的图像对于 1 - 100 的猜谜游戏来说是巨大的。将它们降低到 640 x 480,以便它们适合大多数人的屏幕。您的代码的排列方式对我这个 Swing 开发人员来说非常陌生,以至于我不得不从头开始重写您的代码。哦,还有灰色标签上的白色?你听说过对比吗?
标签: java image swing jpanel jlabel