簡體   English   中英

Java Swing-GUI類無法識別接口類

[英]Java Swing-GUI class does not identify the interface class

我正在嘗試制作一個簡單的計算器,但單擊按鈕時遇到了問題。 單擊按鈕沒有任何反應。 由於我無法在代碼中檢測到任何問題,因此變得越來越困難。

請幫忙!

問題-單擊按鈕無反應!

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM