gpt4 book ai didi

Java Swing-GUI类不识别界面类

转载 作者:行者123 更新时间:2023-12-01 22:06:49 24 4
gpt4 key购买 nike

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

请帮忙!

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

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

这里是接口(interface)类,现在用于在 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);

关于Java Swing-GUI类不识别界面类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32566744/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com