Java GUI(视图)启动混乱
我以前曾在创建GUI方面得到过帮助(谢谢@Hovercraft的鳗鱼) 最初,我的GUI在启动时是这样的。 当你运行这个程序时,它看起来是这样的。 现在在我编辑它之后,它的启动看起来是这样的 然后当你运行程序时,它看起来是这样的。 基本上,启动屏幕看起来乱七八糟,但当你按下按钮时,它会恢复正常,但背景看起来乱七八糟 下面是代码的样子 主要类别:Java GUI(视图)启动混乱,java,swing,model-view-controller,Java,Swing,Model View Controller,我以前曾在创建GUI方面得到过帮助(谢谢@Hovercraft的鳗鱼) 最初,我的GUI在启动时是这样的。 当你运行这个程序时,它看起来是这样的。 现在在我编辑它之后,它的启动看起来是这样的 然后当你运行程序时,它看起来是这样的。 基本上,启动屏幕看起来乱七八糟,但当你按下按钮时,它会恢复正常,但背景看起来乱七八糟 下面是代码的样子 主要类别: public static void main(String[] args) { JFrame f = new JFrame("Envr
public static void main(String[] args) {
JFrame f = new JFrame("Envrionment Stimulation");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(input.getGui()); //input is a class that takes the size of the grid and uses it to call the Layout class
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
这是input在获得输入后调用的布局类
public class Layout extends JPanel {
private static final int WIDTH = 75;
private static final int SPACING = 15;
private final GridControls btnAndTxt;
private final Grid map;
public Layout(Earth earth, int xCount, int yCount) {
//Making our borders
setBorder(BorderFactory.createEmptyBorder(SPACING, SPACING, SPACING,SPACING));
setLayout(new BorderLayout(SPACING, SPACING));
//The Map
map = new Grid(WIDTH, xCount, yCount);
//Buttons and Text
btnAndTxt = new GridControls(map, earth);
//Adding the JPanels.
add(btnAndTxt, BorderLayout.PAGE_START);
add(map, BorderLayout.CENTER);
}
}
接下来是网格,它是如何初始化GUI的
public class Grid extends JPanel {
private final JLabel[][] label;
private final int xCount, yCount;
public Grid(int width, int xCount, int yCount) {
this.xCount = xCount;
this.yCount = yCount;
setBackground(Color.BLACK);
setBorder(BorderFactory.createLineBorder(Color.BLACK));
setLayout(new GridLayout(yCount, xCount, 1, 1));
label = new JLabel[yCount][xCount];
for (int y = 0; y < yCount; y++)
for (int x = 0; x < xCount; x++) {
label[y][x] = new JLabel(".", SwingConstants.CENTER);
label[y][x].setPreferredSize(new Dimension(width, width));
label[y][x].setBackground(Color.GREEN);
label[y][x].setOpaque(true);
label[y][x].setFont(new Font("Ariel", Font.PLAIN, 25));
add(label[y][x]);
}
}
public void updateGrid(Mappable[][] map) {
for (int y = 0; y < yCount; y++)
for (int x = 0; x < xCount; x++) {
label[y][x].setText(map[y][x] == null ? "." : map[y][x].toString());
}
}
public int getY() {
return yCount;
}
public int getX() {
return xCount;
}
}
公共类网格扩展了JPanel{
专用最终JLabel[][]标签;
私人最终int xCount,yCount;
公共网格(整数宽度、整数x计数、整数y计数){
this.xCount=xCount;
this.yCount=yCount;
挫折背景(颜色:黑色);
setboorder(BorderFactory.createLineBorder(Color.BLACK));
setLayout(新网格布局(yCount,xCount,1,1));
label=新的JLabel[yCount][xCount];
对于(int y=0;y
对于GridControl类,它扩展了Jpanel。我对按钮和文本(按钮、文本、按钮)顺序使用add()方法,但没有使用任何布局管理器。这可能是问题所在吗?最初的GUI也没有为按钮和文本面板使用布局管理器。(布局类保持不变)
*编辑1:更新了网格的外观
在类扩展JPanel时,我使用了getX()和getY()方法。它覆盖了方法,这就是它在GUI上导致错误的原因。在您的上一个问题中,我发布了我的答案,一个小型的自足的可运行GUI程序,它演示了我试图在我的答案中提出的观点,a。对于这个问题,你也应该做同样的事情:创建并发布一个小的自包含GUI程序,以可复制的方式向我们演示你的问题。好的,我试着花一点时间。@HovercraftFullOfEels我制作了一个没有模型的单独的小程序,并且它具有所需的正确GUI屏幕。该模型本身非常大,我想我无法制作一个更小的版本。@NoobestPros,
我制作了一个没有该模型的单独的小程序,并且它具有所需的适当GUI屏幕
-因此您已经证明了基本逻辑是有效的<代码>我认为我无法制作更小的版本。-因此,您需要调试实际应用程序,看看有什么不同。“我们猜不出你在做什么不同的事情。”几个小时后,我终于解决了这个问题。在扩展JPanel时,我有一个getX()和getY()方法。它重写了该方法并创建了错误。一旦我把它取下来,它现在就可以正常工作了。