gpt4 book ai didi

java - 创建一个在激活 actionListener 时在小程序上绘制的方法

转载 作者:行者123 更新时间:2023-12-01 23:09:42 25 4
gpt4 key购买 nike

问题:我正在尝试添加多个使用 Java 内置图形方法的方法。绘制到屏幕的方法使用 if 语句。

我尝试过的:尝试使用方法名称并更改(图形g)中的字符。我还尝试将遇到问题的方法放入绘制方法 block 中。

使用的变量:

  • wordLengths = 一个数组,存储一定长度的单词数量。例如。如果我有 1, 1 个字母单词,wordLengths[0] 将为 1。

以下代码打印我的姓名缩写,清空文本字段并绘制图表的 x 和 y 轴:

public void paint(Graphics g) {
g.drawString("BWD", 450, 490); // Puts my signature in the bottom corner
inputField.setText("");
inputText = ("");
g.drawLine(50, 450, 460, 450);//Paints x-axis
g.drawLine(50, 450, 50, 100);//Paints the y-axis
}

以下代码的方法名称为paint。我试图改变这一点,但它仍然返回问题。单击提交按钮时,我希望将以下代码绘制到屏幕上:

public void paint (Graphics g)
{
for(int j=0, x=50, y = 100; j< wordLengths.length;j++)
{
if (wordLengths[j] == 1)
{
y = y+15;
g.drawString(wordLengths[j]+ " word of length "+ (j+1), x, y);
}

g.drawString("There are: ", 50, 90);

if (wordLengths[j] > 1)
{
y = y+15;
g.drawString(wordLengths[j]+ " words of length "+ (j+1), x, y);
}
}
}

理想的代码是这样的:

public void paint(Graphics g) {
g.drawString("BWD", 450, 490); // Puts my signature in the bottom corner
inputField.setText("");
inputText = ("");
g.drawLine(50, 450, 460, 450);//Paints x-axis
g.drawLine(50, 450, 50, 100);//Paints the y-axis

for(int j=0, x=50, y = 100; j< wordLengths.length;j++)
{
if (wordLengths[j] == 1)
{
y = y+15;
g.drawString(wordLengths[j]+ " word of length "+ (j+1), x, y);
}

g.drawString("There are: ", 50, 90);

if (wordLengths[j] > 1)
{
y = y+15;
g.drawString(wordLengths[j]+ " words of length "+ (j+1), x, y);
}
}

}

如果需要任何其他信息,请随时添加评论,我会尽快添加!

提前致谢!

Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at java_assignment.JavaAppletMain.paint(JavaAppletMain.java:114)
at sun.awt.RepaintArea.paintComponent(RepaintArea.java:264)
at sun.lwawt.LWRepaintArea.paintComponent(LWRepaintArea.java:54)
at sun.awt.RepaintArea.paint(RepaintArea.java:240)
at sun.lwawt.LWComponentPeer.handleJavaPaintEvent(LWComponentPeer.java:1267)
at sun.lwawt.LWComponentPeer.handleEvent(LWComponentPeer.java:1150)
at java.awt.Component.dispatchEventImpl(Component.java:4937)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91))

Thread [AWT-EventQueue-1] (Suspended (breakpoint at line 112 in JavaAppletMain))
JavaAppletMain.paint(Graphics) line: 112
LWRepaintArea(RepaintArea).paintComponent(Component, Graphics) line: 264
LWRepaintArea.paintComponent(Component, Graphics) line: 54
LWRepaintArea(RepaintArea).paint(Object, boolean) line: 240
LWPanelPeer(LWComponentPeer<T,D>).handleJavaPaintEvent() line: 1267
LWPanelPeer(LWComponentPeer<T,D>).handleEvent(AWTEvent) line: 1150
JavaAppletMain(Component).dispatchEventImpl(AWTEvent) line: 4937
JavaAppletMain(Container).dispatchEventImpl(AWTEvent) line: 2287
JavaAppletMain(Component).dispatchEvent(AWTEvent) line: 4687
EventQueue.dispatchEventImpl(AWTEvent, Object) line: 735
EventQueue.access$200(EventQueue, AWTEvent, Object) line: 103
EventQueue$3.run() line: 694
EventQueue$3.run() line: 692
AccessController.doPrivileged(PrivilegedAction<T>, AccessControlContext) line: not available [native method]
ProtectionDomain$1.doIntersectionPrivilege(PrivilegedAction<T>, AccessControlContext, AccessControlContext) line: 76
ProtectionDomain$1.doIntersectionPrivilege(PrivilegedAction<T>, AccessControlContext) line: 87
EventQueue$4.run() line: 708
EventQueue$4.run() line: 706
AccessController.doPrivileged(PrivilegedAction<T>, AccessControlContext) line: not available [native method]
ProtectionDomain$1.doIntersectionPrivilege(PrivilegedAction<T>, AccessControlContext, AccessControlContext) line: 76
EventQueue.dispatchEvent(AWTEvent) line: 705
EventDispatchThread.pumpOneEventForFilters(int) line: 242
EventDispatchThread.pumpEventsForFilter(int, Conditional, EventFilter) line: 161
EventDispatchThread.pumpEventsForHierarchy(int, Conditional, Component) line: 150
EventDispatchThread.pumpEvents(int, Conditional) line: 146
EventDispatchThread.pumpEvents(Conditional) line: 138
EventDispatchThread.run() line: 91

最佳答案

您只是在 actionPerformed 中初始化数组 wordLenghts。但是 paint 在程序启动时被调用,并且 actionPerformed 从未被调用来初始化数组。这就是您获得 NPE 的原因。

您应该在构造函数或 init 方法中初始化它。

private int[] wordLength;

@Overrde
public void init() {
wordLength = new int[0];
}

关于java - 创建一个在激活 actionListener 时在小程序上绘制的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22206998/

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