JLabel ImageIcon 数组
Array of JLabel ImageIcon
我正在尝试创建一个数独游戏。我想使用 JSwing API。所以,我使用 JLabel 数组来显示网格。我有一张 3x3 网格的图片,我想在 3x3 网格中显示它。我的问题是它不会显示图像。有人可以帮我解决问题吗?
我当前的代码看起来像这样,分为两个 class。
Main.class
package com.brendenbunker;
import javax.swing.*;
import java.awt.*;
public class Main{
FileMaker fileMaker;
void init() {
fileMaker = new FileMaker();
}
public static void main(String args[]){
ScreenGenerator gui = new ScreenGenerator();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
double width = gui.gridPic.getIconWidth();
double height = gui.gridPic.getIconHeight();
int h = (int) height*4;
int w = (int) width*3;
gui.setSize(w,h);
gui.setTitle("Suduko");
gui.setVisible(true);
}
}
ScreenGenerator.class
package com.brendenbunker;
import javax.swing.*;
import java.awt.*;
public class ScreenGenerator extends JFrame{
//Intro Components
//JLabel temp;
JLabel[] gridLabel;
ImageIcon gridPic;
//intro Vars
public ScreenGenerator() {
setLayout(new FlowLayout());
gridPic = new ImageIcon(getClass().getResource("/Grid_Unified.png"));
gridLabel = new JLabel[8];
for (int i=0; i>=9; i++) {
gridLabel[i] = new JLabel("Hello");
}
for (int i=0; i>=9; i++) {
gridLabel[i].setIcon(gridPic);
add(gridLabel[i]);
}
}
}
所有帮助 Appericiated
改你的for循环,按照你的条件不会进入循环
将循环更改为此..
for (int i=0; i<8; i++) {
gridLabel[i] = new JLabel("Hello");
}
for (int i=0; i<8; i++) {
gridLabel[i].setIcon(gridPic);
add(gridLabel[i]);
}
它会起作用..
package com.brendenbunker;
import javax.swing.*;
import java.awt.*;
public class ScreenGenerator extends JFrame{
//Intro Components
//JLabel temp;
JLabel[] gridLabel;
ImageIcon gridPic;
//intro Vars
public ScreenGenerator() {
setLayout(new FlowLayout());
gridPic = new ImageIcon(getClass().getResource("/Grid_Unified.png"));
gridLabel = new JLabel[8];
//for (int i=0; i>=9; i++) {
//gridLabel[i] = new JLabel("Hello");
// }
for (int i=0; i>=9; i++) {
gridLabel[i] = new JLabel(gridPic);
add(gridLabel[i]);
}
}
}
如果您需要图标,则使用上面的代码;如果您需要文本和图标,则以下更改将帮助您
gridLabel[i] = new JLabel("hello", gridPic, JLabel.CENTER);
希望能有所帮助
看看这个:Displaying Image in Java
我曾经在 Java 编程。我已经转到 Python,但我记得这有很多困难!使用IO文件系统来显示它。您可以在此处找到示例。
如果我错了请纠正我,这是Java?
我正在尝试创建一个数独游戏。我想使用 JSwing API。所以,我使用 JLabel 数组来显示网格。我有一张 3x3 网格的图片,我想在 3x3 网格中显示它。我的问题是它不会显示图像。有人可以帮我解决问题吗?
我当前的代码看起来像这样,分为两个 class。
Main.class
package com.brendenbunker;
import javax.swing.*;
import java.awt.*;
public class Main{
FileMaker fileMaker;
void init() {
fileMaker = new FileMaker();
}
public static void main(String args[]){
ScreenGenerator gui = new ScreenGenerator();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
double width = gui.gridPic.getIconWidth();
double height = gui.gridPic.getIconHeight();
int h = (int) height*4;
int w = (int) width*3;
gui.setSize(w,h);
gui.setTitle("Suduko");
gui.setVisible(true);
}
}
ScreenGenerator.class
package com.brendenbunker;
import javax.swing.*;
import java.awt.*;
public class ScreenGenerator extends JFrame{
//Intro Components
//JLabel temp;
JLabel[] gridLabel;
ImageIcon gridPic;
//intro Vars
public ScreenGenerator() {
setLayout(new FlowLayout());
gridPic = new ImageIcon(getClass().getResource("/Grid_Unified.png"));
gridLabel = new JLabel[8];
for (int i=0; i>=9; i++) {
gridLabel[i] = new JLabel("Hello");
}
for (int i=0; i>=9; i++) {
gridLabel[i].setIcon(gridPic);
add(gridLabel[i]);
}
}
}
所有帮助 Appericiated
改你的for循环,按照你的条件不会进入循环
将循环更改为此..
for (int i=0; i<8; i++) {
gridLabel[i] = new JLabel("Hello");
}
for (int i=0; i<8; i++) {
gridLabel[i].setIcon(gridPic);
add(gridLabel[i]);
}
它会起作用..
package com.brendenbunker;
import javax.swing.*;
import java.awt.*;
public class ScreenGenerator extends JFrame{
//Intro Components
//JLabel temp;
JLabel[] gridLabel;
ImageIcon gridPic;
//intro Vars
public ScreenGenerator() {
setLayout(new FlowLayout());
gridPic = new ImageIcon(getClass().getResource("/Grid_Unified.png"));
gridLabel = new JLabel[8];
//for (int i=0; i>=9; i++) {
//gridLabel[i] = new JLabel("Hello");
// }
for (int i=0; i>=9; i++) {
gridLabel[i] = new JLabel(gridPic);
add(gridLabel[i]);
}
}
}
如果您需要图标,则使用上面的代码;如果您需要文本和图标,则以下更改将帮助您
gridLabel[i] = new JLabel("hello", gridPic, JLabel.CENTER);
希望能有所帮助
看看这个:Displaying Image in Java
我曾经在 Java 编程。我已经转到 Python,但我记得这有很多困难!使用IO文件系统来显示它。您可以在此处找到示例。
如果我错了请纠正我,这是Java?