我想做一个简单的计算器,但我面临的问题,当我点击一个按钮。点击按钮什么也不会发生。这变得越来越困难,因为我无法检测到代码中的任何问题。
请帮忙!
问题-点击按钮没有任何反应!
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Toolkit;
public class Calculator extends JFrame
{
Calculate c=new Calculate(this);
JPanel pan1=new JPanel();
JTextArea area=new JTextArea(350,150);
JPanel pan2=new JPanel();
JButton one=new JButton("1");
JButton two=new JButton("2");
JButton three=new JButton("3");
JButton four=new JButton("4");
JButton five=new JButton("5");
JButton six=new JButton("6");
JButton seven=new JButton("7");
JButton eight=new JButton("8");
JButton nine=new JButton("9");
JButton zero=new JButton("0");
JButton dot=new JButton(".");
JButton equals=new JButton("=");
JPanel pan3=new JPanel();
JButton del=new JButton("DEL");
JButton divide=new JButton("/");
JButton multiply=new JButton("*");
JButton minus=new JButton("-");
JButton plus=new JButton("+");
JPanel pan4=new JPanel();
public Calculator()
{
try {
// Set System L&F
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
}
catch (UnsupportedLookAndFeelException e) {
// handle exception
System.out.println("1");
}
catch (ClassNotFoundException e) {
// handle exception
System.out.println("2");
}
catch (InstantiationException e) {
// handle exception
System.out.println("3");
}
catch (IllegalAccessException e) {
// handle exception
System.out.println("4");
}
setTitle("Calculator");
setSize(350,550);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridLayout bigLayout=new GridLayout(2,1);
setLayout(bigLayout);
//Adding ActionListeners
one.addActionListener(c);
two.addActionListener(c);
three.addActionListener(c);
four.addActionListener(c);
five.addActionListener(c);
six.addActionListener(c);
seven.addActionListener(c);
eight.addActionListener(c);
nine.addActionListener(c);
zero.addActionListener(c);
plus.addActionListener(c);
minus.addActionListener(c);
multiply.addActionListener(c);
divide.addActionListener(c);
del.addActionListener(c);
dot.addActionListener(c);
equals.addActionListener(c);
//Adding the text area
FlowLayout flo=new FlowLayout();
pan1.setLayout(flo);
pan1.add(area);
add(pan1);
//Adding the numbers
GridLayout numbersLayout=new GridLayout(4,3);
pan2.setLayout(numbersLayout);
pan2.add(seven);
pan2.add(eight);
pan2.add(nine);
pan2.add(four);
pan2.add(five);
pan2.add(six);
pan2.add(one);
pan2.add(two);
pan2.add(three);
pan2.add(dot);
pan2.add(zero);
pan2.add(equals);
//Adding the operations
GridLayout operationsLayout=new GridLayout(5,1);
pan3.setLayout(operationsLayout);
pan3.add(del);
pan3.add(divide);
pan3.add(multiply);
pan3.add(minus);
pan3.add(plus);
//Adding the keypad
GridLayout keypadLayout=new GridLayout(1,2);
pan4.setLayout(keypadLayout);
pan4.add(pan2);
pan4.add(pan3);
add(pan4);
setVisible(true);
}
public static void main(String[] arguments)
{
Calculator cal=new Calculator();
}
}
这里是接口类,现在,它被用来在JTextArea区域显示文本
import javax.swing.*;
import java.awt.event.*;
public class Calculate implements ActionListener{
Calculator gui;
public Calculate(Calculator in)
{
gui=in;
}
public void actionPerformed(ActionEvent event)
{
String enterString;
enterString=event.getActionCommand();
gui.area.setText(enterString);
}
}
您的代码工作了,但是由于使用了FlowLayout
,您没有看到它的结果。考虑您传递给JTextArea
构造函数的大小是以字符为单位,而不是以像素为单位。因此,它不适合可见区域,但FlowLayout
不会调整其大小。
将创建代码更改为new JTextArea(15,30)
,您将看到您的更新。然而,更好的选择是不指定一个固定的大小,而是改变布局,使文本区域调整到可用的大小。
。将结构改为new JTextArea()
,代码为
//Adding the text area
FlowLayout flo=new FlowLayout();
pan1.setLayout(flo);
pan1.add(area);
add(pan1);
//Adding the text area
add(area);