JButtons没有被放置在正确的位置,或者消失了。

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

JButtons are not being placed in the correct spot or are disappearing

问题

I am a beginner programmer and I was trying to make a simple calculator, but for some reason, the buttons don't seem to work properly and they either disappear, fill up the whole panel, or are set in the wrong positions. The weird thing is that the buttons appear when I hover and click my mouse over them. I might be missing something very obvious but please help (Also the code isn't very clean). Thank you!

Main

public abstract class Main {
	public static JFrame frame = new JFrame("Calculator");
	public static JPanel panel = new JPanel();
	
	public static CaculatorButton buttons = new CalculatorButton(); 
	
	public static void main(String[] args) {
		Window.window(frame, panel);
		buttons.b0(panel); 
	}
}

CalculatorButton

public class CaculatorButton {
	
	// ... (The rest of the code for the CalculatorButton class)
	
}

Window

public class Window extends JFrame {	
	public static void window(JFrame frame, JPanel panel) {		
		frame.setSize(370, 522);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);	
		frame.setResizable(false);
		frame.setLocationRelativeTo(null);
		frame.setLayout(new BorderLayout());
		
		frame.add(panel, BorderLayout.CENTER); 
		panel.setBackground(Color.DARK_GRAY);
	}

}
英文:

I am a beginner programmer and I was trying to make a simple calculator, but for some reason, the buttons don't seem to work properly and they either disappear, fill up the whole panel, or are set in the wrong positions. The weird thing is that the buttons appear when I hover and click my mouse over them. I might be missing something very obvious but please help (Also the code isn't very clean). Thank you!

Main

public abstract class Main {
	public static JFrame frame = new JFrame("Calculator");
	public static JPanel panel = new JPanel();
	
	public static CaculatorButton buttons = new CalculatorButton(); 
	
	public static void main(String[] args) {
		Window.window(frame, panel);
		buttons.b0(panel); 
	}
}

CalculatorButton

public class CaculatorButton {
	
	private static JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9; 
	private static JTextArea text;
	private static JButton plus;
	private static JButton minus;
	private static JButton equals; 
	private static String temp;
	
	public void b0(JPanel panel) {
		b0 = new JButton("0");		
		b0.setFocusable(false);
		b0.setBackground(Color.GRAY);
		b0.setPreferredSize(new Dimension(80, 80));
		panel.add(b0);		
		b0.setBounds(10, 395, 80, 80);	
		
		b1(panel); b2(panel); b3(panel); b4(panel); b5(panel); b6(panel); b7(panel); b8(panel); b9(panel); textArea(panel); plus(panel); minus(panel); 
		equals(panel); input();
	}
	
	private static void b1(JPanel panel) {
		b1 = new JButton("1");
		b1.setFocusable(false);
		b1.setBackground(Color.GRAY);
		b1.setPreferredSize(new Dimension(80, 80));
		panel.add(b1);
		b1.setBounds(10, 140, 80, 80);
	}

	private static void b2(JPanel panel) {
		b2 = new JButton("2");
		b2.setFocusable(false);
		b2.setBackground(Color.GRAY);
		b2.setPreferredSize(new Dimension(80, 80));
		panel.add(b2);
		b2.setBounds(95, 140, 80, 80);
	}
	
	private static void b3(JPanel panel) {

		b3 = new JButton("3");
		b3.setFocusable(false);
		b3.setBackground(Color.GRAY);
		b3.setPreferredSize(new Dimension(80, 80));
		panel.add(b3);
		b3.setBounds(180, 140, 80, 80);
	}
	
	private static void b4(JPanel panel) {
		b4 = new JButton("4");
		b4.setFocusable(false);
		b4.setBackground(Color.GRAY);
		b4.setPreferredSize(new Dimension(80, 80));
		panel.add(b4);
		b4.setBounds(10, 225, 80, 80);
	}

	private static void b5(JPanel panel) {
		b5 = new JButton("5");
		b5.setFocusable(false);
		b5.setBackground(Color.GRAY);
		b5.setPreferredSize(new Dimension(80, 80));
		panel.add(b5);
		b5.setBounds(95, 225, 80, 80);
	}

	private static void b6(JPanel panel) {
		b6 = new JButton("6");
		b6.setFocusable(false);
		b6.setBackground(Color.GRAY);
		b6.setPreferredSize(new Dimension(80, 80));
		panel.add(b6);
		b6.setBounds(180, 225, 80, 80);
	}

	private static void b7(JPanel panel) {
		b7 = new JButton("7");
		b7.setFocusable(false);
		b7.setBackground(Color.GRAY);
		b7.setPreferredSize(new Dimension(80, 80));
		panel.add(b7);
		b7.setBounds(10, 310, 80, 80);
	}
	
	private static void b8(JPanel panel) {
		b8 = new JButton("8");
		b8.setFocusable(false);
		b8.setBackground(Color.GRAY);
		b8.setPreferredSize(new Dimension(80, 80));
		panel.add(b8);
		b8.setBounds(95, 310, 80, 80);
	}
	
	private static void b9(JPanel panel) {
		b9 = new JButton("9");
		b9.setFocusable(false);
		b9.setBackground(Color.GRAY);
		b9.setPreferredSize(new Dimension(80, 80));
		panel.add(b9);
		b9.setBounds(180, 310, 80, 80);
	}
	
	private static void plus(JPanel panel) {
		plus = new JButton("+");
		plus.setFocusable(false);
		plus.setBackground(new Color(0, 200, 150));
		plus.setPreferredSize(new Dimension(80, 80));
		panel.add(plus); 
		plus.setBounds(95, 395, 80, 80);
	}
	
	private static void minus(JPanel panel) {
		minus = new JButton("-");
		minus.setFocusable(false);
		minus.setBackground(new Color(0, 200, 150));
		b0.setPreferredSize(new Dimension(80, 80));
		panel.add(minus); 
		minus.setBounds(180, 395, 80, 80);
	}
	
	private static void equals(JPanel panel) {
		equals = new JButton("="); 
		equals.setFocusable(false);
		equals.setBackground(new Color(200, 125, 0));
		b0.setPreferredSize(new Dimension(80, 335));
		panel.add(equals);
		equals.setBounds(265, 140, 80, 335);
	}
	
	private static void input() {

		JButton[] buttons = {b0, b1, b2, b3, b4, b5, b6, b7, b8, b9};
		
		ActionListener al = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				
				for (int i = 0; i <= 9; i++) {
					if(e.getSource()== buttons[i]) {
						String str = Integer.toString(i); 
						if (temp == null) {
							temp = str;
						} else {
							temp = temp + str;
							text.setText(temp);
						}
					}
				}
			}	
		};
		
		b0.addActionListener(al);
		b1.addActionListener(al);
		b2.addActionListener(al);
		b3.addActionListener(al);
		b4.addActionListener(al);
		b5.addActionListener(al);
		b6.addActionListener(al);
		b7.addActionListener(al);
		b8.addActionListener(al);
		b9.addActionListener(al);
	}

	private static void textArea(JPanel panel) {
		
		text = new JTextArea("");
		panel.add(text);	
		text.setVisible(true);
		text.setFocusable(true);
		text.setForeground(new Color(51,255,255));
		text.setFont(text.getFont().deriveFont(25f));
		text.setBackground(Color.DARK_GRAY);
		text.setBounds(10, 10, 335, 120);
	}
}

Window

public class Window extends JFrame {	
	public static void window(JFrame frame, JPanel panel) {		
		frame.setSize(370, 522);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);	
		frame.setResizable(false);
		frame.setLocationRelativeTo(null);
		frame.setLayout(new BorderLayout());
		
		frame.add(panel, BorderLayout.CENTER); 
		panel.setBackground(Color.DARK_GRAY);
	}

}

答案1

得分: 2

不要使用静态变量。不要使用静态方法。这不是适当的设计。

然后您的类可以:

  1. 创建所需的实例变量。
  2. 创建并将所有按钮添加到面板中。

没有必要为每个按钮创建单独的方法。使用循环来创建并添加每个按钮到面板中。面板应该使用布局管理器,这样您就不需要担心按钮的大小/位置。GridLayout 是最容易使用的。它会将按钮按行/列网格添加。

请参阅:https://stackoverflow.com/questions/33739623/how-to-add-a-shortcut-key-for-a-jbutton-in-java/33739732#33739732,这是一个使用此方法的工作示例。它会展示一个更好的设计,将上述所有建议都纳入其中。

奇怪的是,当我将鼠标悬停并单击按钮时,按钮会出现。

在将组件添加到窗口可见之前,应将它们添加到窗口中。

英文:

Don't use static variables. Don't use static methods. This is not the proper design.

Your class can then:

  1. create the instance variables that are needed.
  2. create and add all the buttons to the panel.

There is no need to create a separate method for each button. Use a loop to create and then add each button to the panel. The panel should use a layout manager so you don't need to worry about the size/location of the button. The GridLayout would be the easies to use. It will add the buttons in a row/column grid.

See: https://stackoverflow.com/questions/33739623/how-to-add-a-shortcut-key-for-a-jbutton-in-java/33739732#33739732 for a working example that uses this approach. It will show a better design for your class that incorporates all the above suggestions.

> The weird thing is that the buttons appear when I hover and click my mouse over them.

Components should be added to the frame BEFORE the frame is made visible.

答案2

得分: 0

你的JPanel是使用默认构造函数构建的,这意味着它具有FlowLayout作为LayoutManager。这意味着手动设置每个按钮的边界将产生奇怪的效果。尝试使用适当的自定义LayoutManager。或者创建一个自定义的LayoutManager(通过实现该接口),它在重新验证时不会更改组件的位置。

关于LayoutManager有相应的教程:
https://docs.oracle.com/javase/tutorial/uiswing/layout/index.html

注意JPanelContainer的一种,所以该教程也适用于JPanel。您已经在名为frameJFrame上使用了LayoutManager(名为BorderLayout)……您也应该在JPanel上使用。默认情况下它具有FlowLayout,但通过手动设置每个JButton的边界,可以理解您实际上不需要FlowLayout,而可能需要其他布局。您可以在问题中为我们提供一个布局外观的图片,以便我们可以为您提供尝试哪种LayoutManager的指导。

编辑1:

因为您正在尝试创建一个计算器,我建议尝试使用具有3列的GridLayout(并将rows参数设为0,这将使网格始终在达到3列时创建新行)。

英文:

Your JPanel is constructed using the default constructor, which means it has a FlowLayout as the LayoutManager. That means that setting the bounds of each button manually will have strange effects. Try using an appropriate LayoutManager of your liking. Or create a custom LayoutManager (by implementing that interface) which will not change the location of the components on revalidation.

There is a corresponding tutorial on LayoutManagers:
https://docs.oracle.com/javase/tutorial/uiswing/layout/index.html

Note: JPanel is-a Container so that is why the tutorial applies also to JPanels. You are already using a LayoutManager (named BorderLayout) for the JFrame named frame... You should also do that for the JPanel. It has a FlowLayout by default, but by setting manually the bounds of each JButton one can understand that you don't really need a FlowLayout but probably something else. You can give us a picture in your question on how you would like the layout to look like, so that we can give you directions on which LayoutManager to try.

Edit 1:

Because you are trying to create a calculator, I would suggest to try out GridLayout with 3 columns (and 0 as the rows argument, which will make the grid of always creating a new row when 3 columns are already reached).

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

发表评论

匿名网友

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

确定