- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试创建一个文本游戏,并决定最好的方法是使用 UI 而不是 cin.nextLine() 方法和编译器输出。我一直在尝试做的是将 KeyEvent 添加到我的 TextField 中以供用户输入,当用户按下 Enter 键时,他们的文本将进入我在上面设置的任何其他文本下方的 TextArea 中,并且输入将消失。我还尝试将他们在按 Enter 键之前输入的字符串添加到字符串变量中,该变量稍后将在 if/else 语句中进行处理。
我尝试添加文本 main.input.setText("");但这也不起作用。另外,我尝试在 TextField 减速和初始化之前添加“public”一词,但这产生了自己的错误。我也不完全知道应该如何正确访问 TextField 内部的文本将其添加到上面的 TextArea 并将其作为字符串变量进行处理。
import java.awt.*;
import java.awt.event.*;
public class UI extends Frame implements KeyListener
{
public void test()
{
Frame main=new Frame();
main.setSize(1366,768);
main.setLayout(null);
main.setVisible(true);
TextArea mainText=new TextArea("Come on and do something!");
mainText.setBounds(10,30,1366,728);
main.setBackground(Color.white);
mainText.setEditable(false);
TextField input=new TextField("");
input.setBounds(10,738,1366,20);
main.add(input);
main.add(mainText);
input.addKeyListener(this);
}
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_ENTER) {
Toolkit.getDefaultToolkit().beep();
input.setText("");
}
}
public static void main(String[] args)
{
UI set = new UI();
set.test();
}
}
我希望这段代码将 TextField 设置为空白,并且我希望进一步了解如何将代码添加到变量和文本区域。相反,我收到此错误消息:
Error: cannot find symbol
symbol: variable input
location: class UI
编辑:好吧,所以我不知道 awt 已经过时了,所以我确实将其更改为 Swing 版本。我还删除了扩展并将 KeyListener 更改为 ActionListener。但是,现在发生的情况是 ActionListener 没有正确添加到 JTextField。下面是我更新的代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class UI
{
private JFrame main;
private JTextArea mainText;
public UI()
{
main=new JFrame("Text Game");
main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
main.setSize(1366,768);
mainText=new JTextArea("Come on and do something!");
mainText.setBounds(10,100,1366,728);
mainText.setEditable(false);
JTextField input=new JTextField("");
input.setBounds(10,700,1366,20);
input.addActionListener(this);
main.add(input);
main.add(mainText);
main.setVisible(true);
}
public static void main(String[] args)
{
new UI();
}
}
对于我现在做错了什么有什么想法吗?
最佳答案
基本问题是上下文问题。 input
在构造函数的上下文中声明为局部变量,因此无法从任何其他上下文访问它
public void test()
{
//...
TextField input=new TextField("");
//...
}
说实话,这是非常基本的 Java 101,您应该首先查看 Understanding Class Members 。在开始 GUI 开发之前,您应该已经了解这个概念(恕我直言)
KeyListener
(一般情况下),尤其是对于文本组件ActionListener
null
布局,它们会浪费您的时间Frame
等顶级容器进行扩展。它们将您锁定在单个用例中,降低了可重用性,而且您实际上并没有向类中添加任何新功能关于java - 将 KeyEvent 与 Java.awt 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53873112/
AWT-EventQueue 线程和 AWT-Shutdown 线程没有在我们的应用程序中关闭。有没有一种调试技术可以找出它们不存在的原因?有什么特别的东西要寻找吗? 最佳答案 如果你的意思是关闭所有
java.awt.* 和 java.awt.event.* 有什么区别? 最佳答案 这只是两个不同的包。 当您说import java.awt.*时,它仅导入那些完全属于java.awt包的类,而不是
我正在将 aadhar 集成到 liferay 中。我尝试了这个链接 https://developer.uidai.gov.in/site/book/export/html/18 所以我想将其集成到
我想知道如何确定 Java.awt.Rectangle 是否包含特定 Java.awt.Color 的像素。我一直在到处寻找,但找不到任何关于此的信息,甚至找不到任何可能的信息。 所以我想知道如何确定
我试图在组件和图像之间切换面板的内容,它适用于组件: imgpanel.removeAll(); Component comp; if ((comp = player.getVisualCompone
我在使用 JAVA 编码时遇到一些错误,我一直在尝试解决这个问题,也试图找到其他有同样问题的人并修复它,但没有任何效果... 嗯..这是代码 package ca.vanzeben.game;
我想我可以尝试一下 JAXB 来处理存储和恢复设置。但即使是“最简单”的例子我也遇到了麻烦: import java.awt.Point; public class Config { public
这个问题已经有答案了: Import package.* vs import package.SpecificType [duplicate] (10 个回答) 已关闭 7 年前。 在我现在正在进行的
private static byte[] get_byte_data(BufferedImage image) { //WritableRaster raster = image.get
是否有可能获得标准 AWT Cursor以位图图像的形式(例如 BufferedImage )或任何可在 Graphics2D 上绘制的图像?例如,文本光标 new Cursor(Cursor.TEX
我的代码中有三个点,我想填充它们之间的区域,或者换句话说,在 3 个点之间绘制并填充一个三角形。 我想过简单地用 for 循环绘制线条(从 x1 到 x2),但我认为这不会有效,是否有其他更有效的方法
我正在制作一个小脚本,我使用鼠标键来节省我的工作时间。我可以正确、良好地使用鼠标键。但是,当使用 java.awt.Robot 和 java.awt.event.KeyEvent 时,鼠标键基本上被忽
我正在尝试在 scala 中使用 java awt 来制作一个简单的桌面应用程序。我已经在它上面工作了几天,没有任何问题,直到我有 2 天没有碰它,当我回来时,我得到一个 java.lang.NoCl
我在 VisualVM 和线程 View 中监视一个 JavaFX 程序,不断有 AWT-EventQueue-0 和 AWT-Shutdown 线程被创建和销毁。这是正常行为吗?这是什么原因? 最佳
我需要将 java.awt.geom.Area 或 java.awt.Shape 转换为 java.awt.Polygon。我所知道的是:isSingular = true、isPolygonal =
我正在重新使用 Java 并审查我的一些旧代码,并且我看到了很多我已经完成的地方 import javax.swing.*; import java.awt.*; 或者实际上从 swing/awt 包
晚上, 我在玩一个小的 swing 应用程序,我添加了一个按钮来响应按下。因此我需要实现 ActionListener。我已经添加了这一行: import java.awt.*; 但它告诉我找不到“A
我有这个 java 代码: Editor() { javax.swing.SwingUtilities.invokeLater(new Runnable() { pub
请帮我解决这个问题 sun.awt.image.ToolkitImage 无法转换为 java.awt.image.BufferedImage if (shape.hasImage())
嗨 Stackoverflow 的 friend 们 我最近将 Jenkins 服务器配置到 Apache Tomcat 7.0.42我制作的程序是将 jenkins.war 文件部署到 tomcat
我是一名优秀的程序员,十分优秀!