gpt4 book ai didi

java - 无法让 JTextArea 显示在计算器上

转载 作者:太空宇宙 更新时间:2023-11-04 12:39:42 25 4
gpt4 key购买 nike

我正在尝试为学校项目制作计算器,但无法显示 JTextArea。我需要一个窗口,以便用户可以看到他们输入计算器的数字和输出。

这是我的代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.ActionListener;

public class Calculator extends JFrame
{

JButton[] nums = new JButton [10];

JButton num1 = new JButton ("1");
JButton num2 = new JButton ("2");
JButton num3 = new JButton ("3");
JButton num4 = new JButton ("4");
JButton num5 = new JButton ("5");
JButton num6 = new JButton ("6");
JButton num7 = new JButton ("7");
JButton num8 = new JButton ("8");
JButton num9 = new JButton ("9");
JButton num0 = new JButton ("0");
JButton decimal = new JButton (".");
JButton clear = new JButton ("C");
JButton sqrt = new JButton ("\u221A");
JButton mod = new JButton ("%");
JButton dividebyone = new JButton ("1/x");
JButton factorn = new JButton ("!n");

public Calculator ()
{
JFrame window = new JFrame ("Calculator");
setSize (260, 325);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
setVisible (true);

JTextArea screen = new JTextArea (20, 20);
JScrollPane pls = new JScrollPane (screen);
screen.setBackground (Color.WHITE);
screen.setVisible (true);
getContentPane ().add (pls);

getContentPane ().add (stylize (num1));
num1.setBounds (20, 75, 45, 45);
num1.addActionListener (new ActionListener ()
{

public void actionPerformed (ActionEvent e)
{
//Execute when button is pressed
System.out.println ("1");
}
}
);

getContentPane ().add (stylize (num2));
num2.setBounds (70, 75, 45, 45);
num2.addActionListener (new ActionListener ()
{

public void actionPerformed (ActionEvent e)
{
System.out.println ("2");
}
}
);

getContentPane ().add (stylize (num3));
num3.setBounds (120, 75, 45, 45);
num3.addActionListener (new ActionListener ()
{

public void actionPerformed (ActionEvent e)
{
System.out.println ("3");
}
}
);

getContentPane ().add (stylize (num4));
num4.setBounds (20, 125, 45, 45);
num4.addActionListener (new ActionListener ()
{

public void actionPerformed (ActionEvent e)
{
System.out.println ("4");
}
}
);

getContentPane ().add (stylize (num5));
num5.setBounds (70, 125, 45, 45);
num5.addActionListener (new ActionListener ()
{

public void actionPerformed (ActionEvent e)
{
System.out.println ("5");
}
}
);

getContentPane ().add (stylize (num6));
num6.setBounds (120, 125, 45, 45);
num6.addActionListener (new ActionListener ()
{

public void actionPerformed (ActionEvent e)
{
System.out.println ("6");
}
}
);

getContentPane ().add (stylize (num7));
num7.setBounds (20, 175, 45, 45);
num7.addActionListener (new ActionListener ()
{

public void actionPerformed (ActionEvent e)
{
System.out.println ("7");
}
}
);

getContentPane ().add (stylize (num8));
num8.setBounds (70, 175, 45, 45);
num8.addActionListener (new ActionListener ()
{

public void actionPerformed (ActionEvent e)
{
System.out.println ("8");
}
}
);

getContentPane ().add (stylize (num9));
num9.setBounds (120, 175, 45, 45);
num9.addActionListener (new ActionListener ()
{

public void actionPerformed (ActionEvent e)
{
System.out.println ("9");
}
}
);

getContentPane ().add (stylize (num0));
num0.setBounds (20, 225, 45, 45);
num0.addActionListener (new ActionListener ()
{

public void actionPerformed (ActionEvent e)
{
System.out.println ("0");
}
}
);

getContentPane ().add (stylize (decimal));
decimal.setBounds (70, 225, 45, 45);
decimal.addActionListener (new ActionListener ()
{

public void actionPerformed (ActionEvent e)
{
System.out.println (".");
}
}
);

getContentPane ().add (stylize (clear));
clear.setBounds (120, 225, 45, 45);
clear.addActionListener (new ActionListener ()
{

public void actionPerformed (ActionEvent e)
{
System.out.println ("c");
}
}
);

getContentPane ().add (stylize (sqrt));
sqrt.setBounds (170, 75, 51, 45);
sqrt.addActionListener (new ActionListener ()
{

public void actionPerformed (ActionEvent e)
{
System.out.println ("sqrt");
}
}
);

getContentPane ().add (stylize (mod));
mod.setBounds (170, 125, 51, 45);
mod.addActionListener (new ActionListener ()
{

public void actionPerformed (ActionEvent e)
{
System.out.println ("mod");
}
}
);

getContentPane ().add (stylize (factorn));
factorn.setBounds (170, 175, 51, 45);
factorn.addActionListener (new ActionListener ()
{

public void actionPerformed (ActionEvent e)
{
System.out.println ("!n");
}
}
);

getContentPane ().add (stylize (dividebyone));
dividebyone.setBounds (170, 225, 51, 45);
dividebyone.addActionListener (new ActionListener ()
{

public void actionPerformed (ActionEvent e)
{
System.out.println ("1/x");
}
}
);

} //end Calculator


public static JButton stylize (JButton button)
{
button.setBackground (Color.WHITE);
return button;
} //end stylize


public static void main (String args[])
{
new Calculator ();
} //end main
} //end Calculator class

最佳答案

尝试这样做:

Public static void main(String arts[])
{
Calculator c= new Calculator();
c.setVisible(true);
}

从构造函数中删除 setVisible(true)。希望这有效。
由于您的框架在添加组件之前是可见的,因此它不会显示您的组件。

关于java - 无法让 JTextArea 显示在计算器上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36941862/

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