【问题标题】:Java Swing-GUI class does not identify the interface classJava Swing-GUI类不识别接口类
【发布时间】:2015-09-14 13:59:24
【问题描述】:

我正在尝试制作一个简单的计算器,但是当我单击按钮时遇到了问题。单击按钮时没有任何反应。这变得越来越困难,因为我无法检测到代码中的任何问题。

请帮忙!

问题 - 单击按钮时没有任何反应!

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

【问题讨论】:

  • 您是否将调试放入actionPerformed 并验证该方法已被调用并且getActionCommand 返回您所期望的?

标签: java swing jtextarea flowlayout


【解决方案1】:

您的代码有效,但由于您使用了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);

【讨论】:

  • 谢谢。由于上述更改,代码现在可以工作了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-24
  • 2020-05-19
  • 1970-01-01
相关资源
最近更新 更多