- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在你说之前,我打算使用空布局管理器。我知道这不是最好的做法。
这是我的代码。请原谅效率低下,我只是 java 的初学者,不知道该怎么做。
package AEngine;
import java.awt.*;
import javax.swing.*;
import AEngine.Sprites.*;
import AEngine.Maps.Crossroads;
@SuppressWarnings("unused")
public class WindowManager {
public static ImageIcon x1y1 = new ImageIcon("");
public static ImageIcon x2y1 = new ImageIcon("");
public static ImageIcon x3y1 = new ImageIcon("");
public static ImageIcon x4y1 = new ImageIcon("");
public static ImageIcon x5y1 = new ImageIcon("");
public static ImageIcon x6y1 = new ImageIcon("");
public static ImageIcon x7y1 = new ImageIcon("");
public static ImageIcon x8y1 = new ImageIcon("");
public static ImageIcon x9y1 = new ImageIcon("");
public static ImageIcon x10y1 = new ImageIcon("");
public static ImageIcon x11y1 = new ImageIcon("");
public static ImageIcon x12y1 = new ImageIcon("");
public static ImageIcon x1y2 = new ImageIcon("");
public static ImageIcon x2y2 = new ImageIcon("");
public static ImageIcon x3y2 = new ImageIcon("");
public static ImageIcon x4y2 = new ImageIcon("");
public static ImageIcon x5y2 = new ImageIcon("");
public static ImageIcon x6y2 = new ImageIcon("");
public static ImageIcon x7y2 = new ImageIcon("");
public static ImageIcon x8y2 = new ImageIcon("");
public static ImageIcon x9y2 = new ImageIcon("");
public static ImageIcon x10y2 = new ImageIcon("");
public static ImageIcon x11y2 = new ImageIcon("");
public static ImageIcon x12y2 = new ImageIcon("");
public static ImageIcon x1y3 = new ImageIcon("");
public static ImageIcon x2y3 = new ImageIcon("");
public static ImageIcon x3y3 = new ImageIcon("");
public static ImageIcon x4y3 = new ImageIcon("");
public static ImageIcon x5y3 = new ImageIcon("");
public static ImageIcon x6y3 = new ImageIcon("");
public static ImageIcon x7y3 = new ImageIcon("");
public static ImageIcon x8y3 = new ImageIcon("");
public static ImageIcon x9y3 = new ImageIcon("");
public static ImageIcon x10y3 = new ImageIcon("");
public static ImageIcon x11y3 = new ImageIcon("");
public static ImageIcon x12y3 = new ImageIcon("");
public static ImageIcon x1y4 = new ImageIcon("");
public static ImageIcon x2y4 = new ImageIcon("");
public static ImageIcon x3y4 = new ImageIcon("");
public static ImageIcon x4y4 = new ImageIcon("");
public static ImageIcon x5y4 = new ImageIcon("");
public static ImageIcon x6y4 = new ImageIcon("");
public static ImageIcon x7y4 = new ImageIcon("");
public static ImageIcon x8y4 = new ImageIcon("");
public static ImageIcon x9y4 = new ImageIcon("");
public static ImageIcon x10y4 = new ImageIcon("");
public static ImageIcon x11y4 = new ImageIcon("");
public static ImageIcon x12y4 = new ImageIcon("");
public static ImageIcon x1y5 = new ImageIcon("");
public static ImageIcon x2y5 = new ImageIcon("");
public static ImageIcon x3y5 = new ImageIcon("");
public static ImageIcon x4y5 = new ImageIcon("");
public static ImageIcon x5y5 = new ImageIcon("");
public static ImageIcon x6y5 = new ImageIcon("");
public static ImageIcon x7y5 = new ImageIcon("");
public static ImageIcon x8y5 = new ImageIcon("");
public static ImageIcon x9y5 = new ImageIcon("");
public static ImageIcon x10y5 = new ImageIcon("");
public static ImageIcon x11y5 = new ImageIcon("");
public static ImageIcon x12y5 = new ImageIcon("");
public static ImageIcon x1y6 = new ImageIcon("");
public static ImageIcon x2y6 = new ImageIcon("");
public static ImageIcon x3y6 = new ImageIcon("");
public static ImageIcon x4y6 = new ImageIcon("");
public static ImageIcon x5y6 = new ImageIcon("");
public static ImageIcon x6y6 = new ImageIcon("");
public static ImageIcon x7y6 = new ImageIcon("");
public static ImageIcon x8y6 = new ImageIcon("");
public static ImageIcon x9y6 = new ImageIcon("");
public static ImageIcon x10y6 = new ImageIcon("");
public static ImageIcon x11y6 = new ImageIcon("");
public static ImageIcon x12y6 = new ImageIcon("");
public static ImageIcon x1y7 = new ImageIcon("");
public static ImageIcon x2y7 = new ImageIcon("");
public static ImageIcon x3y7 = new ImageIcon("");
public static ImageIcon x4y7 = new ImageIcon("");
public static ImageIcon x5y7 = new ImageIcon("");
public static ImageIcon x6y7 = new ImageIcon("");
public static ImageIcon x7y7 = new ImageIcon("");
public static ImageIcon x8y7 = new ImageIcon("");
public static ImageIcon x9y7 = new ImageIcon("");
public static ImageIcon x10y7 = new ImageIcon("");
public static ImageIcon x11y7 = new ImageIcon("");
public static ImageIcon x12y7 = new ImageIcon("");
public static ImageIcon x1y8 = new ImageIcon("");
public static ImageIcon x2y8 = new ImageIcon("");
public static ImageIcon x3y8 = new ImageIcon("");
public static ImageIcon x4y8 = new ImageIcon("");
public static ImageIcon x5y8 = new ImageIcon("");
public static ImageIcon x6y8 = new ImageIcon("");
public static ImageIcon x7y8 = new ImageIcon("");
public static ImageIcon x8y8 = new ImageIcon("");
public static ImageIcon x9y8 = new ImageIcon("");
public static ImageIcon x10y8 = new ImageIcon("");
public static ImageIcon x11y8 = new ImageIcon("");
public static ImageIcon x12y8 = new ImageIcon("");
public static ImageIcon x1y9= new ImageIcon("");
public static ImageIcon x2y9 = new ImageIcon("");
public static ImageIcon x3y9 = new ImageIcon("");
public static ImageIcon x4y9 = new ImageIcon("");
public static ImageIcon x5y9 = new ImageIcon("");
public static ImageIcon x6y9 = new ImageIcon("");
public static ImageIcon x7y9 = new ImageIcon("");
public static ImageIcon x8y9 = new ImageIcon("");
public static ImageIcon x9y9 = new ImageIcon("");
public static ImageIcon x10y9 = new ImageIcon("");
public static ImageIcon x11y9 = new ImageIcon("");
public static ImageIcon x12y9 = new ImageIcon("");
public static ImageIcon x1y10 = new ImageIcon("");
public static ImageIcon x2y10 = new ImageIcon("");
public static ImageIcon x3y10 = new ImageIcon("");
public static ImageIcon x4y10 = new ImageIcon("");
public static ImageIcon x5y10 = new ImageIcon("");
public static ImageIcon x6y10 = new ImageIcon("");
public static ImageIcon x7y10 = new ImageIcon("");
public static ImageIcon x8y10 = new ImageIcon("");
public static ImageIcon x9y10 = new ImageIcon("");
public static ImageIcon x10y10 = new ImageIcon("");
public static ImageIcon x11y10 = new ImageIcon("");
public static ImageIcon x12y10 = new ImageIcon("");
public static ImageIcon x1y11 = new ImageIcon("");
public static ImageIcon x2y11 = new ImageIcon("");
public static ImageIcon x3y11 = new ImageIcon("");
public static ImageIcon x4y11 = new ImageIcon("");
public static ImageIcon x5y11 = new ImageIcon("");
public static ImageIcon x6y11 = new ImageIcon("");
public static ImageIcon x7y11 = new ImageIcon("");
public static ImageIcon x8y11 = new ImageIcon("");
public static ImageIcon x9y11 = new ImageIcon("");
public static ImageIcon x10y11 = new ImageIcon("");
public static ImageIcon x11y11 = new ImageIcon("");
public static ImageIcon x12y11 = new ImageIcon("");
public static ImageIcon x1y12 = new ImageIcon("");
public static ImageIcon x2y12 = new ImageIcon("");
public static ImageIcon x3y12 = new ImageIcon("");
public static ImageIcon x4y12 = new ImageIcon("");
public static ImageIcon x5y12 = new ImageIcon("");
public static ImageIcon x6y12 = new ImageIcon("");
public static ImageIcon x7y12 = new ImageIcon("");
public static ImageIcon x8y12 = new ImageIcon("");
public static ImageIcon x9y12 = new ImageIcon("");
public static ImageIcon x10y12 = new ImageIcon("");
public static ImageIcon x11y12 = new ImageIcon("");
public static ImageIcon x12y12 = new ImageIcon("");
public static JFrame MFrame = new JFrame();
@SuppressWarnings("static-access")
public static void main(String[] args){
MFrame.setLayout(null);
MFrame.setSize(576, 576);
JPanel MPanel = new JPanel();
MFrame.setContentPane(MPanel);
AEngine.Maps.Crossroads Map = new AEngine.Maps.Crossroads();
AEngine.Sprites.Sprites SManager = new AEngine.Sprites.Sprites();
int[][] TileData = Map.TileData();
x1y1 = SManager.GetSprite(TileData[1][1]);
x2y1 = SManager.GetSprite(TileData[2][1]);
x3y1 = SManager.GetSprite(TileData[3][1]);
x4y1 = SManager.GetSprite(TileData[4][1]);
x5y1 = SManager.GetSprite(TileData[5][1]);
x6y1 = SManager.GetSprite(TileData[6][1]);
x7y1 = SManager.GetSprite(TileData[7][1]);
x8y1 = SManager.GetSprite(TileData[8][1]);
x9y1 = SManager.GetSprite(TileData[9][1]);
x10y1 = SManager.GetSprite(TileData[10][1]);
x11y1 = SManager.GetSprite(TileData[11][1]);
x12y1 = SManager.GetSprite(TileData[12][1]);
x1y2 = SManager.GetSprite(TileData[1][2]);
x2y2 = SManager.GetSprite(TileData[2][2]);
x3y2 = SManager.GetSprite(TileData[3][2]);
x4y2 = SManager.GetSprite(TileData[4][2]);
x5y2 = SManager.GetSprite(TileData[5][2]);
x6y2 = SManager.GetSprite(TileData[6][2]);
x7y2 = SManager.GetSprite(TileData[7][2]);
x8y2 = SManager.GetSprite(TileData[8][2]);
x9y2 = SManager.GetSprite(TileData[9][2]);
x10y2 = SManager.GetSprite(TileData[10][2]);
x11y2 = SManager.GetSprite(TileData[11][2]);
x12y2 = SManager.GetSprite(TileData[12][2]);
x1y3 = SManager.GetSprite(TileData[1][3]);
x2y3 = SManager.GetSprite(TileData[2][3]);
x3y3 = SManager.GetSprite(TileData[3][3]);
x4y3 = SManager.GetSprite(TileData[4][3]);
x5y3 = SManager.GetSprite(TileData[5][3]);
x6y3 = SManager.GetSprite(TileData[6][3]);
x7y3 = SManager.GetSprite(TileData[7][3]);
x8y3 = SManager.GetSprite(TileData[8][3]);
x9y3 = SManager.GetSprite(TileData[9][3]);
x10y3 = SManager.GetSprite(TileData[10][3]);
x11y3 = SManager.GetSprite(TileData[11][3]);
x12y3 = SManager.GetSprite(TileData[12][3]);
x1y4 = SManager.GetSprite(TileData[1][4]);
x2y4 = SManager.GetSprite(TileData[2][4]);
x3y4 = SManager.GetSprite(TileData[3][4]);
x4y4 = SManager.GetSprite(TileData[4][4]);
x5y4 = SManager.GetSprite(TileData[5][4]);
x6y4 = SManager.GetSprite(TileData[6][4]);
x7y4 = SManager.GetSprite(TileData[7][4]);
x8y4 = SManager.GetSprite(TileData[8][4]);
x9y4 = SManager.GetSprite(TileData[9][4]);
x10y4 = SManager.GetSprite(TileData[10][4]);
x11y4 = SManager.GetSprite(TileData[11][4]);
x12y4 = SManager.GetSprite(TileData[12][4]);
x1y5 = SManager.GetSprite(TileData[1][5]);
x2y5 = SManager.GetSprite(TileData[2][5]);
x3y5 = SManager.GetSprite(TileData[3][5]);
x4y5 = SManager.GetSprite(TileData[4][5]);
x5y5 = SManager.GetSprite(TileData[5][5]);
x6y5 = SManager.GetSprite(TileData[6][5]);
x7y5 = SManager.GetSprite(TileData[7][5]);
x8y5 = SManager.GetSprite(TileData[8][5]);
x9y5 = SManager.GetSprite(TileData[9][5]);
x10y5 = SManager.GetSprite(TileData[10][5]);
x11y5 = SManager.GetSprite(TileData[11][5]);
x12y5 = SManager.GetSprite(TileData[12][5]);
x1y6 = SManager.GetSprite(TileData[1][6]);
x2y6 = SManager.GetSprite(TileData[2][6]);
x3y6 = SManager.GetSprite(TileData[3][6]);
x4y6 = SManager.GetSprite(TileData[4][6]);
x5y6 = SManager.GetSprite(TileData[5][6]);
x6y6 = SManager.GetSprite(TileData[6][6]);
x7y6 = SManager.GetSprite(TileData[7][6]);
x8y6 = SManager.GetSprite(TileData[8][6]);
x9y6 = SManager.GetSprite(TileData[9][6]);
x10y6 = SManager.GetSprite(TileData[10][6]);
x11y6 = SManager.GetSprite(TileData[11][6]);
x12y6 = SManager.GetSprite(TileData[12][6]);
x1y7 = SManager.GetSprite(TileData[1][7]);
x2y7 = SManager.GetSprite(TileData[2][7]);
x3y7 = SManager.GetSprite(TileData[3][7]);
x4y7 = SManager.GetSprite(TileData[4][7]);
x5y7 = SManager.GetSprite(TileData[5][7]);
x6y7 = SManager.GetSprite(TileData[6][7]);
x7y7 = SManager.GetSprite(TileData[7][7]);
x8y7 = SManager.GetSprite(TileData[8][7]);
x9y7 = SManager.GetSprite(TileData[9][7]);
x10y7 = SManager.GetSprite(TileData[10][7]);
x11y7 = SManager.GetSprite(TileData[11][7]);
x12y7 = SManager.GetSprite(TileData[12][7]);
x1y8 = SManager.GetSprite(TileData[1][8]);
x2y8 = SManager.GetSprite(TileData[2][8]);
x3y8 = SManager.GetSprite(TileData[3][8]);
x4y8 = SManager.GetSprite(TileData[4][8]);
x5y8 = SManager.GetSprite(TileData[5][8]);
x6y8 = SManager.GetSprite(TileData[6][8]);
x7y8 = SManager.GetSprite(TileData[7][8]);
x8y8 = SManager.GetSprite(TileData[8][8]);
x9y8 = SManager.GetSprite(TileData[9][8]);
x10y8 = SManager.GetSprite(TileData[10][8]);
x11y8 = SManager.GetSprite(TileData[11][8]);
x12y8 = SManager.GetSprite(TileData[12][8]);
x1y9 = SManager.GetSprite(TileData[1][9]);
x2y9 = SManager.GetSprite(TileData[2][9]);
x3y9 = SManager.GetSprite(TileData[3][9]);
x4y9 = SManager.GetSprite(TileData[4][9]);
x5y9 = SManager.GetSprite(TileData[5][9]);
x6y9 = SManager.GetSprite(TileData[6][9]);
x7y9 = SManager.GetSprite(TileData[7][9]);
x8y9 = SManager.GetSprite(TileData[8][9]);
x9y9 = SManager.GetSprite(TileData[9][9]);
x10y9 = SManager.GetSprite(TileData[10][9]);
x11y9 = SManager.GetSprite(TileData[11][9]);
x12y9 = SManager.GetSprite(TileData[12][9]);
x1y10 = SManager.GetSprite(TileData[1][10]);
x2y10 = SManager.GetSprite(TileData[2][10]);
x3y10 = SManager.GetSprite(TileData[3][10]);
x4y10 = SManager.GetSprite(TileData[4][10]);
x5y10 = SManager.GetSprite(TileData[5][10]);
x6y10 = SManager.GetSprite(TileData[6][10]);
x7y10 = SManager.GetSprite(TileData[7][10]);
x8y10 = SManager.GetSprite(TileData[8][10]);
x9y10 = SManager.GetSprite(TileData[9][10]);
x10y10 = SManager.GetSprite(TileData[10][10]);
x11y10 = SManager.GetSprite(TileData[11][10]);
x12y10 = SManager.GetSprite(TileData[12][10]);
x1y11 = SManager.GetSprite(TileData[1][11]);
x2y11 = SManager.GetSprite(TileData[2][11]);
x3y11 = SManager.GetSprite(TileData[3][11]);
x4y11 = SManager.GetSprite(TileData[4][11]);
x5y11 = SManager.GetSprite(TileData[5][11]);
x6y11 = SManager.GetSprite(TileData[6][11]);
x7y11 = SManager.GetSprite(TileData[7][11]);
x8y11 = SManager.GetSprite(TileData[8][11]);
x9y11 = SManager.GetSprite(TileData[9][11]);
x10y11 = SManager.GetSprite(TileData[10][11]);
x11y11 = SManager.GetSprite(TileData[11][11]);
x12y11 = SManager.GetSprite(TileData[12][11]);
x1y12 = SManager.GetSprite(TileData[1][12]);
x2y12 = SManager.GetSprite(TileData[2][12]);
x3y12 = SManager.GetSprite(TileData[3][12]);
x4y12 = SManager.GetSprite(TileData[4][12]);
x5y12 = SManager.GetSprite(TileData[5][12]);
x6y12 = SManager.GetSprite(TileData[6][12]);
x7y12 = SManager.GetSprite(TileData[7][12]);
x8y12 = SManager.GetSprite(TileData[8][12]);
x9y12 = SManager.GetSprite(TileData[9][12]);
x10y12 = SManager.GetSprite(TileData[10][12]);
x11y12 = SManager.GetSprite(TileData[11][12]);
x12y12 = SManager.GetSprite(TileData[12][12]);
MPanel.add(x1y1);
MFrame.add(MPanel);
}
}
问题出在 MPanel.add(x1y1); 上bit 它表示这不是正确的数据类型,并且它只能采用组件。另外,如果您有任何在 java 中动态创建变量的方法,请告诉我。
请帮忙?
最佳答案
您无法将 ImageIcons 添加到 JPanel,但可以添加到 JLabels。我建议您这样做,然后将 JLabel 添加到您的 JPanel - 使用合适的布局管理器。
顺便说一句:您的代码恳求您使用数组或 ArrayList。请不要忽视它的请求。
您的庞大代码列表可以缩短为这样,
int[][] TileData = Map.TileData();
ImageIcon[][] xyIcons = new ImageIcon[12][12];
for (int i = 0; i < xyIcons.length; i++) {
for (int j = 0; j < xyIcons[i].length; j++) {
xyIcons[i][j] = SManager.GetSprite(TileData[i][j]);
}
}
使维护和调试变得更加容易。
<小时/>编辑
您声明:
But it is my intent to use a null layout manager. I hate them. They are so confusing to work with. Java from the start should never of had it, in my opinion.
不要误会我的意思,空布局有一些用途,我用它来为组件设置动画,但通常应该避免。考虑在此网站上创建并发布一个新问题,其中您将尝试使用布局管理器创建 GUI,显示您尝试实现的布局的图像以及您将获得的内容的图像。如果您这样做,请确保您的代码编译并为我们运行时不做任何更改,但尽可能简单,包含简单的哑组件,例如没有附加 ActionListener 的按钮。我敢打赌我们可以帮助您实现一个不错的非空布局。另请记住,许多复杂且灵活的 GUI 是通过嵌套 JPanel 创建的,每个 JPanel 使用自己的布局。
关于java - 我无法将 ImageIcon 添加到 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21938264/
我正在尝试使用 NetBeans 在 Java 中制作类似幻灯片的应用程序。 我有一个 JFrame(主窗口),里面有两个用于导航的按钮(后退和下一步),还有一个 JPanel(mainPanel),
新代码 package test; import javax.swing.*; import java.awt.*; public class TestWindow extends JFrame{ /
我是 Java swing 编码新手。我正在尝试将 JPanel 内容复制到一个新的 JPanel,它使用原始 JPanel 的内容来显示。此外,原始 JPanel 内容随着记录的变化而变化。我尝试了
我正在尝试创建一个带有另外两个 JPanel 的 JPanel,但是,当时,当我要显示主要内容时JPanel,它只显示主Jpanel上的第一个Jpanel。 通过以下示例,您将更好地理解这个问题: 我
我正在尝试学习如何使用编码风格在 Java 中完成 GUI 的工作这就是我写的: import java.awt.Container; import java.awt.Panel; import ja
我从 Oracle 教程中得到了一个 JPanel 的例子我看到它使用默认方法关闭窗口 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 我想
我在使用 Java JPanel 时遇到问题。我想将 2 个具有不同布局的 JPanel 放入一个也有布局的 JPanel 中。有可能让它发挥作用吗? BibP() setLayout(new
我试图让一个 JPanel 出现在另一个 JPanel 中。目前,JPanel 位于外部 JFrame 中,并与我的其他 JFrame 一起加载。我希望 JPanel 位于另一个 JPanel 内部,
是否可以在 Java 的一个方法中在一个面板中包含一个面板? 就像,如果我在名为 CreatePanel 的方法中创建了一个 JPanel,我可以在其下面添加另一个吗?我正在尝试在一种方法中添加两个或
我对嵌套的 BoxLayouts 有疑问。 我想构建一个由 2 个子面板组成的 DropDownPanel:顶部的标题和底部的主体。 body 最初是隐藏的。通过单击标题,您可以切换正文的可见性并显示
我需要创建一个在 JFrame 或 JPanel 上显示多个矩形的程序。这是我到目前为止提出的代码: import javax.swing.*; import java.util.Random; im
将站点中的一些图像保存到 ArrayList 后,我试图创建一个 jpanel,它将在带有滚动 Pane 的单独 jpanel 中显示所有这些图像,以便我可以向每个图像添加 Action 事件。然
我正在开发一个简单的注册窗口,它会在 Java 应用程序打开时出现。 这是一个 JFrame,里面有一个 JPanel,它有文本字段、标签,另一个面板也包含文本字段和标签。我的问题是外部面板有背景图像
我有这个界面要创建。我有 JScrollPane 的问题: 我声明了一个带有 Gridlayout(8,1,0,2) 的 JPanel,我希望在这个面板中出现 8 行。 一行是一个JPanel,我设置
我已经创建了 JFreeChart 并将其放入图表面板(按照建议)。我还将它添加到 jPanel 中。我正在使用 jFrame。但是运行程序后我的图表不可见。有谁能帮帮我吗? final JFreeC
我在下面附上了我的代码,我的程序的屏幕截图,以及我希望标签看起来像的图形。我需要我的 JPanel textPanel 出现在 LLP 选项卡上的 JButton 下方。我试图将 textPanel
我想从窗口 (JFrame) 中删除旧的 JPanel 并添加一个新的。我该怎么做呢? 我尝试了以下方法: public static void showGUI() { JFrame fram
我刚刚接触 Java,正在为我的大学类(class)开发一个项目。我正在开发一款《百万富翁》游戏,但我陷入困境。 我有一个 JFrame 类,其中有 2 个面板。第一个是由按钮组成的,第二个是我想通过
所以,我正在制作一个绘画程序,我有一个主要的Paint类,它检测鼠标输入和绘画,还有一个Tools类,它是左侧的工具栏,拥有许多工具,例如画笔大小更改和形状更改。因此,我想向 Tools 类添加一个清
我正在尝试制作一个在 JToggleButton 的帮助下激活的弹出面板。我希望在选择 ToggleButton 时将 JPanel 添加到另一个 Jpanel 上,并在取消选择 ToggleButt
我是一名优秀的程序员,十分优秀!