gpt4 book ai didi

java - 用 Java 设计一个计算器

转载 作者:行者123 更新时间:2023-11-30 09:33:44 24 4
gpt4 key购买 nike

我是 Java 的新手,我发现这里很难实现 GUI,这与在 Visual Basic 中不同。

import javax.swing.*;
import java.awt.*;


public class CalculatorGui extends JFrame{
private static final int WIDTH = 250;
private static final int HEIGHT = 250;

private JTextField enterTextField;

private JButton sevenB, eightB, nineB, divideB, fourB, fiveB, sixB, multiplyB, oneB, twoB, threeB, subtractB, zeroB, dotB, equalsB, addB;

public CalculatorGui() {
setTitle("Calculator");
setSize(WIDTH, HEIGHT);

setLayout(new GridLayout(4, 4));

enterTextField = new JTextField(30);

sevenB = new JButton("7");
eightB = new JButton("8");
nineB = new JButton("9");
divideB = new JButton("/");
fourB = new JButton("4");
fiveB = new JButton("5");
sixB = new JButton("6");
multiplyB = new JButton("*");
oneB = new JButton("1");
twoB = new JButton("2");
threeB = new JButton("3");
subtractB = new JButton("-");
zeroB = new JButton("0");
dotB = new JButton(".");
equalsB = new JButton("=");
addB = new JButton("+");

/*JPanel dataPanel = new JPanel();
JPanel buttonPanel = new JPanel();

FlowLayout flowCenter = new FlowLayout(FlowLayout.RIGHT);
GridLayout aGrid = new GridLayout(4, 4);

buttonPanel.setLayout(flowCenter);
dataPanel.setLayout(aGrid);


dataPanel.add(enterTextField);
buttonPanel.add(sevenB);
buttonPanel.add(eightB);
buttonPanel.add(nineB);
buttonPanel.add(divideB);
buttonPanel.add(fourB);
buttonPanel.add(fiveB);
buttonPanel.add(sixB);
buttonPanel.add(multiplyB);
buttonPanel.add(oneB);
buttonPanel.add(twoB);
buttonPanel.add(threeB);
buttonPanel.add(subtractB);
buttonPanel.add(zeroB);
buttonPanel.add(dotB);
buttonPanel.add(equalsB);
buttonPanel.add(addB);

add(dataPanel, BorderLayout.NORTH);
add(buttonPanel, BorderLayout.CENTER);*/

//add(enterTextField);
add(sevenB);
add(eightB);
add(nineB);
add(divideB);
add(fourB);
add(fiveB);
add(sixB);
add(multiplyB);
add(oneB);
add(twoB);
add(threeB);
add(subtractB);
add(zeroB);
add(dotB);
add(equalsB);
add(addB);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);

}

public static void main(String[] args) {

CalculatorGui calcu = new CalculatorGui();

}

}

它可以运行,但我需要在这些按钮之前放置一个文本字段(数字)。文本字段应流动,并且在窗口最大化时不会放大(与 4x4 按钮不同)。我试过 JPanel(见评论),但它输出的设计很糟糕。

最佳答案

给你,在构建 GUI 时要遵循一些规则 (rules):
不要为应用程序需要子类化 AWT/Swing/X 组件

import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class CalculatorGui {

private static final int WIDTH = 250;
private static final int HEIGHT = 250;
private JTextField enterTextField;
private JButton sevenB, eightB, nineB, divideB, fourB, fiveB, sixB, multiplyB, oneB, twoB, threeB, subtractB, zeroB, dotB, equalsB, addB;

public CalculatorGui() {
JFrame frame = new JFrame("Calculator");
frame.setTitle("Calculator");
frame.setSize(WIDTH, HEIGHT);

JPanel buttonPanel = new JPanel(new GridLayout(4, 4));
enterTextField = new JTextField();

frame.add(enterTextField, BorderLayout.NORTH);

sevenB = new JButton("7");
eightB = new JButton("8");
nineB = new JButton("9");
divideB = new JButton("/");
fourB = new JButton("4");
fiveB = new JButton("5");
sixB = new JButton("6");
multiplyB = new JButton("*");
oneB = new JButton("1");
twoB = new JButton("2");
threeB = new JButton("3");
subtractB = new JButton("-");
zeroB = new JButton("0");
dotB = new JButton(".");
equalsB = new JButton("=");
addB = new JButton("+");

buttonPanel.add(sevenB);
buttonPanel.add(eightB);
buttonPanel.add(nineB);
buttonPanel.add(divideB);
buttonPanel.add(fourB);
buttonPanel.add(fiveB);
buttonPanel.add(sixB);
buttonPanel.add(multiplyB);
buttonPanel.add(oneB);
buttonPanel.add(twoB);
buttonPanel.add(threeB);
buttonPanel.add(subtractB);
buttonPanel.add(zeroB);
buttonPanel.add(dotB);
buttonPanel.add(equalsB);
buttonPanel.add(addB);

frame.add(buttonPanel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

public static void main(String[] args) {
CalculatorGui calcu = new CalculatorGui();
}
}

我建议您使用 WindowsBuilder Pro在对一些 layoutcomponents

进行良好的手动练习之后

关于java - 用 Java 设计一个计算器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12016391/

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