问题描述
我正在尝试从头开始构建自己的计算器。
当我按原样运行程序时,一些按钮不会显示,直到我用光标将鼠标悬停在它们上方,并且所有边界都被填满。但是,当我将window.setVisible(true);
移到构造函数的开头时,所有对象的每个边界都正确放置了,但是 all 我的对象只显示一次鼠标移过。
package guitest;
import java.awt.Color;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import static javax.swing.JFrame.*;
public class Frame implements ActionListener{
public static int Calculator(int n1,int n2){
return n1 + n2;
}
JTextField num1,num2,ans;
JButton calculate,add,sub,pro,div;
JPanel textFields,actions;
Frame(){
//Window is being created.
JFrame window = new JFrame("Calculator");
window.setDefaultCloSEOperation(EXIT_ON_CLOSE);
window.setResizable(false);
window.setSize(400,400);
//Creating panel
textFields = new JPanel();
actions = new JPanel();
//Adjusting JPanel
textFields.setBounds(0,240,400);
actions.setBounds(240,160,400);
//Creating textfields.
num1 = new JTextField("Number 1");
num2 = new JTextField("Number 2");
ans = new JTextField("Answer");
ans.setEditable(false);
//Creating calculate button.
calculate = new JButton("Calclulate");
add = new JButton("+");
sub = new JButton("-");
pro = new JButton("*");
div = new JButton("/");
//adjusting TextFields to my window
num1.setBounds(30,20,200,20);
num2.setBounds(30,60,20);
ans.setBounds(30,100,20);
//adjusting Buttons to my window
calculate.setBounds(30,140,90,30);
add.setBounds(20,50,50);
sub.setBounds(75,50);
pro.setBounds(20,75,50);
div.setBounds(75,50);
calculate.addActionListener(this);
//adding to my window
textFields.add(num1);textFields.add(num2);textFields.add(ans);textFields.add(calculate);
actions.add(add);actions.add(sub);actions.add(pro);actions.add(div);
window.add(textFields);window.add(actions);
window.setVisible(true);
//Setting everything visible
//textFields.setVisible(true);actions.setVisible(true);
//num1.setVisible(true);num2.setVisible(true);ans.setVisible(true);
//calculate.setVisible(true);add.setVisible(true);sub.setVisible(true);pro.setVisible(true);div.setVisible(true);
}
public void actionPerformed(ActionEvent e){
String n1 = num1.getText();
String n2 = num2.getText();
int a = Integer.parseInt(n1);
int b = Integer.parseInt(n2);
int c;
c = Calculator(a,b);
String result = String.valueOf(c);
ans.setText(result);
}
public static void main(String[] args) {
new Frame();
}
}
when window.setVisible(true);
is in the bottom.
when window.setVisible(true);
is at the top.
How it is supposed to look.(我已经将鼠标悬停在所有对象上。)
解决方法
我更改了您的代码,并使用布局而不是setBouds()
。
Swing提供了不同的LayoutManager,它们用于以特定方式布置组件。以下类用于表示Swing中的布局管理器:
1) java.awt.BorderLayout
2) java.awt.FlowLayout
3) java.awt.GridLayout
4) java.awt.CardLayout
5) java.awt.GridBagLayout
6) javax.swing.BoxLayout
7) javax.swing.GroupLayout
8) javax.swing.ScrollPaneLayout
9) javax.swing.SpringLayout etc.
它们每个都将按特定顺序排列您的组件,您可以在此处(more details...)上了解更多信息
public class Frame implements ActionListener {
public static int Calculator(int n1,int n2) {
return n1 + n2;
}
JTextField num1,num2,ans;
JButton calculate,add,sub,pro,div;
JPanel textFields,actions;
Frame() {
// Window is being created.
JFrame window = new JFrame("Calculator");
window.setDefaultCloseOperation(EXIT_ON_CLOSE);
window.setResizable(false);
window.setSize(400,400);
window.setLayout(new BorderLayout());
// Creating panel
// Creating textfields.
num1 = new JTextField("Number 1");
num2 = new JTextField("Number 2");
ans = new JTextField("Answer");
ans.setEditable(false);
// Creating calculate button.
calculate = new JButton("Calclulate");
add = new JButton("+");
sub = new JButton("-");
pro = new JButton("*");
div = new JButton("/");
calculate.addActionListener(this);
textFields = new JPanel();
actions = new JPanel();
GridLayout txtBox = new GridLayout(4,1);
txtBox.setVgap(20);
textFields.setLayout(txtBox);
GridLayout actionBox = new GridLayout(2,2);
actions.setLayout(actionBox);
actions.setPreferredSize(new Dimension(100,100));
// adding to my window
textFields.add(num1);
textFields.add(num2);
textFields.add(ans);
textFields.add(calculate);
actions.add(add);
actions.add(sub);
actions.add(pro);
actions.add(div);
window.setLayout(new FlowLayout());
window.add(textFields);
window.add(actions);
window.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String n1 = num1.getText();
String n2 = num2.getText();
int a = Integer.parseInt(n1);
int b = Integer.parseInt(n2);
int c;
c = Calculator(a,b);
String result = String.valueOf(c);
ans.setText(result);
}
public static void main(String[] args) {
new Frame();
}
}