Java Swing-GUI类不识别接口类

时间:2015-09-14 13:59:25

标签: java swing jtextarea flowlayout

我正在尝试制作一个简单的计算器但是当我点击一个按钮时我遇到了问题。点击按钮没有任何反应。由于我无法在代码中发现任何问题,因此很难实现。

请帮忙!

问题 - 单击按钮时没有任何操作!

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);
    }
}

1 个答案:

答案 0 :(得分:3)

您的代码有效,但由于您使用了FlowLayout,因此无法看到其结果。请考虑传递给JTextArea构造函数的大小是字符,而不是像素。因此,它不适合可见区域,但FlowLayout不会调整其大小。

将创建代码更改为new JTextArea(15,30),您将看到更新。但是,更好的选择是不指定固定大小,而是将布局更改为将文本区域调整为可用大小的内容。

E.g。将构造更改为new JTextArea()和代码

    //Adding the text area
    FlowLayout flo=new FlowLayout();
    pan1.setLayout(flo);
    pan1.add(area);
    add(pan1);

    //Adding the text area
    add(area);