按钮没有放在我告诉它们的位置。

huangapple go评论63阅读模式
英文:

Buttons are not being place where I tell them

问题

以下是您要翻译的内容:

"So I was trying to place 10 buttons in a grid at the bottom of my frame (without using a layout manager) and for some reason I cannot figure out why this isn't working, I was wondering if anyone could help.

I should probably explain the code:

Basically I have just created a loop that stores a 'while' in it, this while is repeated 3 times before ending and going back to the loop. After the while is finished, the Y changes by 85 to start a new layer for the next 3 buttons. The loop repeats 4 times to create 4 different layers of 10 buttons.

Thanks!

Main

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);
	}

}

Buttons

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); 
	}
	
}

Window

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); 
	}
}
英文:

So I was trying to place 10 buttons in a grid at the bottom of my frame (without using a layout manager) and for some reason I cannot figure out why this isn't working, I was wondering if anyone could help.

I should probably explain the code:

Basically I have just created a loop that stores a "while" in it, this while is repeated 3 times before ending and going back to the loop. After the while is finished, the Y changes by 85 to start a new layer for the next 3 buttons. The loop repeats 4 times to create 4 different layers of 10 buttons.

Thanks!

Main

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);
	}

}

Buttons

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); 
	}
	
}

Window

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); 
	}
}

答案1

得分: -2

Here's the translated code:

感谢这个代码有效并解决了我的一个问题另一个问题是在第二次循环后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); 
	}
	
}

I've translated the code as requested.

英文:

Thanks this works, and solved one of my problems. The other problem was that the "while" did not work after the second loop so I had to rewrite the loop system, here is the solution just in case you want to use it (don't use this, I'm a noob. Also don't be like me, use a layout manager.):

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); 
}

}

答案2

得分: -3

也许你认为你没有使用布局管理器,因为你没有在你的 JFrame 上调用 setLayout,但事实上,默认情况下,JFrame 会自动使用 BorderLayout 布局。

如果你确实想要不使用布局管理器,请在你的设置代码中添加以下行:

frame.setLayout(null);
英文:

Maybe you think you're working without a layout manager because you haven't called setLayout on your JFrame, but in fact the JFrame gave itself a BorderLayout by default.

If you truly want to work without a layout manager, add the line

frame.setLayout(null);

in your setup code.

huangapple
  • 本文由 发表于 2020年8月1日 11:57:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63201609.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定