我正在尝试在Java GUI类中创建一个具有以下属性的计算器:
使用GridLayout和BorderLayout布局管理器我无法获得所需的结果 怎么解决这个问题?
我写的代码在
下面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);
}
}
答案 0 :(得分:3)
这是我提出的计算器GUI:
我对您的代码进行了一些更改。
我删除了几乎所有的静态方法和变量。
我通过在main方法中调用SwingUtilities invokeLater方法在Event Dispatch thread上启动了Java Swing GUI。
我创建了一个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);
}
}
}