Java中具有两行文本字段的计算器
我试图在Java GUI类中制作一个计算器,它具有以下属性:Java中具有两行文本字段的计算器,java,swing,user-interface,Java,Swing,User Interface,我试图在Java GUI类中制作一个计算器,它具有以下属性: 操作发生的文本字段必须由两行组成 第1行打印我们所做的操作 第2行打印操作结果 calclator的按钮必须按照下图所示的方式组织: 使用GridLayout和BorderLayout布局管理器,我无法获得所需的结果 如何解决这个问题 下面是我写的代码 import java.awt.GridLayout; import java.awt.BorderLayout; import javax.swing.*; import ja
import java.awt.GridLayout;
import java.awt.BorderLayout;
import javax.swing.*;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class calculator {
public static JTextField field;
public static String pStr="";
// Create an array containing the buttons texts
public static final String[][] BUTTON_TEXTS = {
{" ", " ", "d", " c"},
{"7", "8", "9", "+"},
{"4", "5", "6", "-"},
{"1", "2", "3", "*"},
{"0", ".", "/", "="}
};
// Create the button hnadler
private static class ButtonHandler implements ActionListener{
private static int clicksNumber = 0;
private String c;
public void numberButtonsAction(JButton a) {
this.c = a.getText();
}
public void actionPerformed(ActionEvent e){
clicksNumber ++ ;
pStr +=((JButton) e.getSource()).getText();
System.out.println(pStr);
field.setText(pStr);
}
}
//Create Font used in caluclator
public static final Font BTN_FONT = new Font(Font.SANS_SERIF, Font.ITALIC, 15);
public static void main(String[] args){
// create the field in which the operations are printed
JTextField text = new JTextField();
text.setSize(20, 500);
field = text;
text.setFont(BTN_FONT); // Set the field font
JLabel myLabel = new JLabel("Se7002");
// Create the grid that will contain the buttons
JPanel btnPanel = new JPanel(new GridLayout(BUTTON_TEXTS.length,
BUTTON_TEXTS[0].length));
// Create and Fill grid with buttons
for(int i=0 ;i<BUTTON_TEXTS.length ; i++){
for(int j=0 ; j< BUTTON_TEXTS[0].length ; j++){
JButton btn = new JButton(BUTTON_TEXTS[i][j]);
btn.setFont(BTN_FONT);
btn.setSize(60,90);
btnPanel.add(btn);
ButtonHandler listener = new ButtonHandler();
btn.addActionListener(listener);
}
}
//Create the content of the calculator
JPanel content = new JPanel(new BorderLayout());
content.add(text,BorderLayout.PAGE_START);
content.add(btnPanel,BorderLayout.CENTER);
content.add(myLabel,BorderLayout.SOUTH);
JFrame CalcTest = new JFrame("Calculator");
CalcTest.setContentPane(content);
CalcTest.setSize(300,350);
CalcTest.setLocation(100, 100);
CalcTest.setVisible(true);
}
}
导入java.awt.GridLayout;
导入java.awt.BorderLayout;
导入javax.swing.*;
导入java.awt.Font;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
公共类计算器{
公共静态JTextField字段;
公共静态字符串pStr=“”;
//创建包含按钮文本的数组
公共静态最终字符串[][]按钮文本={
{“”,“d”,“c”},
{"7", "8", "9", "+"},
{"4", "5", "6", "-"},
{"1", "2", "3", "*"},
{"0", ".", "/", "="}
};
//创建按钮hnadler
私有静态类ButtonHandler实现ActionListener{
私有静态int clicksNumber=0;
私有字符串c;
公共无效编号按钮操作(JButton a){
this.c=a.getText();
}
已执行的公共无效操作(操作事件e){
单击成员++;
pStr+=((JButton)e.getSource()).getText();
系统输出打印项次(pStr);
字段设置文本(pStr);
}
}
//创建caluclator中使用的字体
公共静态最终字体BTN_Font=新字体(Font.SANS_SERIF,Font.ITALIC,15);
公共静态void main(字符串[]args){
//创建打印操作的字段
JTextField text=新的JTextField();
文本设置大小(20500);
字段=文本;
text.setFont(BTN_FONT);//设置字段字体
JLabel myLabel=新JLabel(“Se7002”);
//创建包含按钮的网格
JPanel btnPanel=新的JPanel(新的网格布局(BUTTON_text.length,
按钮(文本[0]。长度));
//使用按钮创建并填充网格
对于(int i=0;i这是我设计的计算器GUI:
我对你的代码做了一些修改
我删除了几乎所有的静态方法和变量
通过在main方法中调用SwingUtilities invokeLater方法,我在上启动了JavaSwingGUI
我创建了一个Button和Buttons(Java对象)类,这样我就可以在一个地方定义计算器按钮
我使用GridBagLayout定位计算器按钮。我使用FlowLayout定位JTextArea。我使用BorderLayout将JTextArea JPanel和计算器按钮JPanel放置在JFrame中。我删除了所有绝对定位和间距
我对代码进行了格式化和组织,以便更易于阅读和修改
我将JTextArea和计算器按钮的生成分为两种不同的方法
我没有对动作听众做任何事
这是密码
package com.ggl.testing;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class Calculator implements Runnable {
private static final Font BTN_FONT = new Font(Font.SANS_SERIF, Font.ITALIC,
15);
private Buttons buttons;
private JTextArea textArea;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Calculator());
}
public Calculator() {
buttons = new Buttons();
}
@Override
public void run() {
JFrame calcTest = new JFrame("Calculator");
calcTest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
calcTest.add(createTextPanel(), BorderLayout.NORTH);
calcTest.add(createButtonPanel(), BorderLayout.SOUTH);
calcTest.pack();
calcTest.setLocationByPlatform(true);
calcTest.setVisible(true);
}
private JPanel createTextPanel() {
JPanel panel = new JPanel();
// Create the field in which the operations are printed
textArea = new JTextArea(2, 15);
textArea.setEditable(false);
textArea.setFont(BTN_FONT); // Set the field font
panel.add(textArea);
return panel;
}
private JPanel createButtonPanel() {
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
ButtonHandler buttonHandler = new ButtonHandler();
for (Button button : buttons.getButtons()) {
JButton jButton = new JButton(button.getLabel());
jButton.addActionListener(buttonHandler);
jButton.setFont(BTN_FONT);
addComponent(panel, jButton, button.getGridx(), button.getGridy(),
button.getGridwidth(), button.getGridheight(),
button.getInsets(), GridBagConstraints.LINE_START,
GridBagConstraints.BOTH);
}
return panel;
}
private void addComponent(Container container, Component component,
int gridx, int gridy, int gridwidth, int gridheight, Insets insets,
int anchor, int fill) {
GridBagConstraints gbc = new GridBagConstraints(gridx, gridy,
gridwidth, gridheight, 1.0D, 1.0D, anchor, fill, insets, 0, 0);
container.add(component, gbc);
}
private class Buttons {
private List<Button> buttons;
public Buttons() {
Insets leftTopInsets = new Insets(10, 10, 4, 4);
Insets topInsets = new Insets(10, 0, 4, 4);
Insets rightTopInsets = new Insets(10, 0, 4, 10);
Insets leftInsets = new Insets(0, 10, 4, 4);
Insets normalInsets = new Insets(0, 0, 4, 4);
Insets rightInsets = new Insets(0, 0, 4, 10);
Insets leftBottomInsets = new Insets(0, 10, 10, 4);
Insets bottomInsets = new Insets(0, 0, 10, 4);
Insets rightBottomInsets = new Insets(0, 0, 10, 10);
this.buttons = new ArrayList<>(18);
this.buttons.add(new Button("7", 0, 0, 1, 1, leftTopInsets));
this.buttons.add(new Button("8", 1, 0, 1, 1, topInsets));
this.buttons.add(new Button("9", 2, 0, 1, 1, topInsets));
this.buttons.add(new Button("/", 3, 0, 1, 1, topInsets));
this.buttons.add(new Button("C", 4, 0, 1, 1, rightTopInsets));
this.buttons.add(new Button("4", 0, 1, 1, 1, leftInsets));
this.buttons.add(new Button("5", 1, 1, 1, 1, normalInsets));
this.buttons.add(new Button("6", 2, 1, 1, 1, normalInsets));
this.buttons.add(new Button("*", 3, 1, 1, 1, normalInsets));
this.buttons.add(new Button("<-", 4, 1, 1, 1, rightInsets));
this.buttons.add(new Button("1", 0, 2, 1, 1, leftInsets));
this.buttons.add(new Button("2", 1, 2, 1, 1, normalInsets));
this.buttons.add(new Button("3", 2, 2, 1, 1, normalInsets));
this.buttons.add(new Button("-", 3, 2, 1, 1, normalInsets));
this.buttons.add(new Button("=", 4, 2, 1, 2, rightBottomInsets));
this.buttons.add(new Button("0", 0, 3, 2, 1, leftBottomInsets));
this.buttons.add(new Button(",", 2, 3, 1, 1, bottomInsets));
this.buttons.add(new Button("+", 3, 3, 1, 1, bottomInsets));
}
public List<Button> getButtons() {
return buttons;
}
}
private class Button {
private final String label;
private final int gridx;
private final int gridy;
private final int gridwidth;
private final int gridheight;
private final Insets insets;
public Button(String label, int gridx, int gridy, int gridwidth,
int gridheight, Insets insets) {
this.label = label;
this.gridx = gridx;
this.gridy = gridy;
this.gridwidth = gridwidth;
this.gridheight = gridheight;
this.insets = insets;
}
public String getLabel() {
return label;
}
public int getGridx() {
return gridx;
}
public int getGridy() {
return gridy;
}
public int getGridwidth() {
return gridwidth;
}
public int getGridheight() {
return gridheight;
}
public Insets getInsets() {
return insets;
}
}
// Create the button handler
private class ButtonHandler implements ActionListener {
private int clicksNumber = 0;
private String c;
public void numberButtonsAction(JButton a) {
this.c = a.getText();
}
public void actionPerformed(ActionEvent e) {
clicksNumber++;
String pStr = ((JButton) e.getSource()).getText();
System.out.println(pStr);
}
}
}
package com.ggl.testing;
导入java.awt.BorderLayout;
导入java.awt.Component;
导入java.awt.Container;
导入java.awt.Font;
导入java.awt.GridBagConstraints;
导入java.awt.GridBagLayout;
导入java.awt.Insets;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.util.ArrayList;
导入java.util.List;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.JTextArea;
导入javax.swing.SwingUtilities;
公共类计算器实现可运行{
私有静态最终字体BTN_Font=新字体(Font.SANS_SERIF,Font.ITALIC,
15);
私人按钮;
私人JTEXTEXTAREA textArea;
公共静态void main(字符串[]args){
调用器(新计算器());
}
公共计算器(){
按钮=新按钮();
}
@凌驾
公开募捐{
JFrame calcTest=新的JFrame(“计算器”);
calcTest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
添加(createTextPanel(),BorderLayout.NORTH);
添加(createButtonPanel(),BorderLayout.SOUTH);
计算测试包();
计算测试setLocationByPlatform(真);
计算测试设置可见(真);
}
私有JPanel createTextPanel(){
JPanel面板=新的JPanel();
//创建打印操作的字段
textArea=新的JTextArea(2,15);
textArea.setEditable(false);
textArea.setFont(BTN_FONT);//设置字段字体
面板。添加(文本区域);
返回面板;
}
私有JPanel createButtonPanel(){
JPanel面板=新的JPanel();
panel.setLayout(新的GridBagLayout());
ButtonHandler ButtonHandler=新ButtonHandler();
用于(按钮:buttons.getButtons()){
JButton JButton=newjbutton(button.getLabel());
addActionListener(buttonHandler);
setFont(BTN_字体);
addComponent(面板,jButton,button.getGridx(),button.getGridy(),按钮,
button.getGridwidth(),button.getGridheight(),
button.getInsets(),GridBagConstraints.LINE\u START,
(二者皆有);
}
返回面板;
}
私有void addComponent(容器容器、组件组件、,
int gridx、int gridy、int GRIDWITH、int gridheight、Insets Insets、,
int锚定,int填充){
GridBagConstraints gbc=新的GridBagConstraints(gridx、gridy、,
网格宽度、网格高度、1.0D、1.0D、锚定、填充、插入、0、0);
容器。添加(组件,gbc);
}
私有类按钮{
私有列表按钮;
聚氨基甲酸酯