【问题标题】:Buttons are not being place where I tell them按钮不在我告诉他们的地方
【发布时间】:2020-11-21 21:03:34
【问题描述】:

所以我试图在框架底部的网格中放置 10 个按钮(不使用布局管理器),由于某种原因,我无法弄清楚为什么这不起作用,我想知道是否有人可以提供帮助。

我可能应该解释一下代码:

基本上我刚刚创建了一个循环,其中存储了一个“while”,这个 while 在结束并返回循环之前重复了 3 次。一段时间结束后,Y 变化 85 为接下来的 3 个按钮开始一个新图层。循环重复 4 次以创建 4 个不同的层,每层 10 个按钮。

谢谢!

主要

public class Main {

    private static JFrame frame = new JFrame();
    private static JPanel panel = new JPanel();
    
    public static void main(String[] args) {
        Buttons.setButtons(frame, panel);
        Window.setFrame(frame, panel);
    }

}

按钮

public class Buttons {
    
    private static JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9;
    private static JButton equals;
    private static JButton minus;
    private static JButton plus; 
    private static int i = 0;
    private static int x = 10;
    private static int y = 140;
    
    private static JButton buttons[] = {b0, b1, b2, b3, b4, b5, b6, b7, b8, b9};
    
    public static void setButtons(JFrame frame, JPanel panel) {
        
        for (int b = 0; b < 4; b++) {
            while (i < 3) {
                buttons[i] = new JButton(Integer.toString(i)); 
                buttons[i].setBounds(x, y, 80, 80);
                x = x + 85; 
                buttons[i].setFocusable(false);
                buttons[i].setBackground(Color.GRAY);
                panel.add(buttons[i]);
                i++;
            }
            y = y + 85; 
            x = 10;
        }
        
        System.out.print(y);
        frame.setVisible(true); 
    }
    
}

窗口

public class Window {

    public static void setFrame(JFrame frame, JPanel panel) {
        
        frame.setSize(370, 525);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        
        panel.setBounds(10, 140, 200, 200);
        panel.setBackground(Color.DARK_GRAY); 
        frame.add(panel); 
    }
}

【问题讨论】:

  • 在您的 while 循环中,将 i &lt; 3 替换为 i &lt; 10 &amp;&amp; i &lt; (i + 3)

标签: java swing jpanel jbutton layout-manager


【解决方案1】:

感谢这项工作,并解决了我的一个问题。另一个问题是“while”在第二个循环之后不起作用,所以我不得不重写循环系统,这是解决方案以防万一你想使用它(不要使用这个,我是菜鸟。也不要像我一样,使用布局管理器。):

public class Buttons {

private static JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9;
private static JButton dot;
private static JButton equals;
private static JButton minus;
private static JButton plus;
private static int count = 9;
private static int x = 10;
private static int y = 145;

private static JButton buttons[] = {b0, b1, b2, b3, b4, b5, b6, b7, b8, b9};

public static void setButtons(JFrame frame, JPanel panel) {
    
    for (int b = 3; b > 0; b--) {
        for (int i = 3; i > 0 ; i--) {
        buttons[count] = new JButton(Integer.toString(count)); 
        buttons[count].setBounds(x, y, 80, 80);
        x = x + 85; 
        buttons[count].setFocusable(false);
        buttons[count].setBackground(Color.GRAY);
        panel.add(buttons[count]);
        count--;
        }
        y = y + 85; 
        x = 10;
    }
    
    dot = new JButton(".");
    dot.setBounds(x, y, 80, 80);
    dot.setFocusable(false);
    dot.setBackground(new Color(0, 200, 150));
    panel.add(dot);
    
    buttons[0] = new JButton("0");
    x = x + 85;
    buttons[0].setBounds(x, y, 80, 80);
    buttons[0].setFocusable(false);
    buttons[0].setBackground(Color.GRAY);
    panel.add(buttons[0]);
    
    plus = new JButton("+");
    x = x + 85; 
    plus.setBounds(x, y, 80, 80); 
    plus.setFocusable(false);
    plus.setBackground(new Color(0, 200, 150));
    panel.add(plus);
    
    minus = new JButton("-");
    x = x + 85;
    minus.setBounds(x, y, 80, 80);
    minus.setFocusable(false);
    minus.setBackground(new Color(0, 200, 150));
    panel.add(minus); 
    
    equals = new JButton("=");
    y = 145;
    equals.setBounds(x, y, 80, 250);
    equals.setFocusable(false);
    equals.setBackground(new Color(240, 150, 50));
    panel.add(equals); 
    
    frame.setVisible(true); 
}

}

【讨论】:

  • 没有人应该使用这个解决方案。静态变量是不必要的。 null 布局是不必要的。
  • 抱歉,我是 Java 新手。我不使用布局管理器的原因是我使用的按钮位置很奇怪(而且我在使用布局管理器时遇到了困难,这是我的错)。显然我也误解了静态的含义,所以我必须学习。感谢您的反馈,我将编辑上面的解决方案并警告可能考虑使用它的人。
【解决方案2】:

也许你认为你在没有布局管理器的情况下工作,因为你没有在你的JFrame 上调用setLayout,但实际上JFrame 给了自己一个BorderLayout by默认。

如果您真的想在没有布局管理器的情况下工作,请添加该行

frame.setLayout(null);

在您的设置代码中。

【讨论】:

  • "如果你真的想......" ..破坏 GUI ..*"..添加行 frame.setLayout(null);"* 直到你找出原因是次优建议,请停止提供需要跨多个平台工作的布局的建议。
  • 在没有布局管理器的情况下工作会导致比您想象的更多的问题。
猜你喜欢
  • 2018-05-27
  • 1970-01-01
  • 2017-07-03
  • 1970-01-01
  • 2022-11-15
  • 2021-10-28
  • 2015-10-11
  • 2013-08-23
  • 2012-10-25
相关资源
最近更新 更多