gpt4 book ai didi

java - 如何排列Java GUI按钮?

转载 作者:行者123 更新时间:2023-12-02 01:26:35 24 4
gpt4 key购买 nike

我需要按特定顺序排列按钮,但无论我做什么修改,它只会改变按钮的大小,而不改变位置。抱歉,如果这是一个简单的问题,这是我的第一个 Java 类,我无法理解如何安排内容和事件处理。

public class SwingCalculator extends JFrame {
private JButton jbtReset;
private JButton jbtAdd;
private JButton jbtSubtract;

private double TEMP;
private double SolveTEMP;
private JTextField jtfResult;

Boolean addBool = false;
Boolean subBool = false;

String display = "";

public SwingCalculator()
{
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(4, 3));
p1.add(jbtReset = new JButton("Reset"));

JPanel p2 = new JPanel();
p2.setLayout(new FlowLayout());
p2.add(jtfResult = new JTextField(10));
jtfResult.setEditable(true);

JPanel p3 = new JPanel();
p3.setLayout(new GridLayout(5, 1));
p3.add(jbtAdd = new JButton("+"));
p3.add(jbtSubtract = new JButton("-"));

JPanel p = new JPanel();
p.setLayout(new GridLayout());
p.add(p2);
p.add(p1);
p.add(p3);

add(p);

jbtAdd.addActionListener(new ListenToAdd());
jbtSubtract.addActionListener(new ListenToSubtract());
jbtReset.addActionListener(new ListenToReset());
} //JavaCaluclator()

class ListenToReset implements ActionListener {
public void actionPerformed(ActionEvent e) {
//display = jtfResult.getText();
jtfResult.setText("");
addBool = false;
subBool = false;
TEMP = 0;
SolveTEMP = 0;
}
}

class ListenToAdd implements ActionListener {
public void actionPerformed(ActionEvent e) {
TEMP = Double.parseDouble(jtfResult.getText());
jtfResult.setText("");
addBool = true;
SolveTEMP = SolveTEMP + TEMP;
jtfResult.setText( Double.toString(SolveTEMP));
addBool = false;
subBool = false;
}
}

class ListenToSubtract implements ActionListener {
public void actionPerformed(ActionEvent e) {
TEMP = Double.parseDouble(jtfResult.getText());
jtfResult.setText("");
subBool = true;
SolveTEMP = SolveTEMP - TEMP;
jtfResult.setText( Double.toString(SolveTEMP));
addBool = false;
subBool = false;
}
}

public static void main(String[] args) {
// TODO Auto-generated method stub
SwingCalculator calc = new SwingCalculator();
calc.pack();
calc.setLocationRelativeTo(null);
calc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
calc.setVisible(true);
}

} //JavaCalculator

我的结果:

enter image description here

预期结果:

enter image description here

最佳答案

您可以将 GridBagLayout 与各种选项一起使用。

例如:

GridBagLayout grid = new GridBagLayout();  
GridBagConstraints gbc = new GridBagConstraints();
setLayout(grid);
//add TF
gbc.gridx=1;
gbc.gridy=0;
this.add(new JTextField(10),gbc);
gbc.gridx=0;
gbc.gridy=1;
this.add(new JButton("+"),gbc);
gbc.gridx=1;
this.add(new JButton("-"),gbc);
gbc.gridx=2;
this.add(new JButton("Reset"),gbc);

//also for another display (first TF will be on the same x as - Button)
//(and maybe you want TF to cover the space for all 3 Button +,-,Reset)
//you could add 2 panels one over other (x=0 : y=0,y=1), place TF on P1 and all Buttons on P2

关于java - 如何排列Java GUI按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56816067/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com