import java.awt.*;
import java.awt.event.*;
public class sample2 extends Frame
{
Button b[];
public sample2()
{
super("trying");
b=new Button[10];
setLayout(new FlowLayout());
for(int i=0;i<10;i++)
add(b[i]);
}
public static void main(String args[])
{
sample2 obj=new sample2();
obj.setSize(500,100);
obj.setVisible(true);
}
}
Run Code Online (Sandbox Code Playgroud)
例外情况如下
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1037)
at java.awt.Container.add(Container.java:373)
at sample2.<init>(sample2.java:13)
at sample2.main(sample2.java:17)
Run Code Online (Sandbox Code Playgroud) 我正在Java Applet中创建一个文本游戏,所以我可以在我的网站上显示它并让人们在那里播放它,但是我遇到了在TextArea中显示任何文本的问题.
这是我的主要课程:
package com.game.main;
import java.applet.*;
import java.awt.*;
public class Main extends Applet {
private TextField commandInput;
private TextArea messageDisplay;
private Button button;
public Message messages;
// Initialisation method
public void init() {
super.init();
// Define colours
setBackground(Color.white);
setForeground(Color.black);
Panel appletPanel = new Panel();
// Use a border layout
BorderLayout b = new BorderLayout();
appletPanel.setLayout(b);
add(appletPanel);
this.setSize(800, 400);
// Define UI items
commandInput = new TextField(20);
messageDisplay = new TextArea(20, 60); // 20 rows x 60 chars
button = …Run Code Online (Sandbox Code Playgroud) 我在一个框架中绘制了两个字符串的简单Java应用程序的一部分.渲染第一个字符串,但第二个字符串不渲染.
import java.awt.*;
//Begin troublesome section
class NewFrame extends Frame {
public void paint (Graphics g)
{
g.drawString ("Foo", 70, 70);
}
public void paint2 (Graphics g)
{
g.drawString ("Bar", 600, 600);
}
}
//End troublesome section
public class FooBar {
public static void main (String[] argv)
{
NewFrame nf = new NewFrame ();
nf.setTitle ("Foo Bar");
nf.setResizable (true);
nf.setBackground (Color.cyan);
nf.setSize (700, 700);
nf.setVisible (true);
}
}
Run Code Online (Sandbox Code Playgroud)
有问题的代码部分是所谓的"麻烦部分".
我正在为我的AP计算机科学课创建一个tic tac toe游戏,到目前为止它运行没有错误,或者错误已通过简单的修复解决.但是,当我向程序添加if语句时,它会不断显示一条错误,指出"无法访问的代码".我仍然无法确定原因或如何解决此问题.
我使用"魔方"设置游戏,每行和对角线设置为一个变量,最多加15,这是程序确定胜利者的方式.
此if语句中出现错误:
if(topx == 15 || middlex == 15 || bottomx == 15 || leftx == 15 || centerx == 15 || rightx == 15 || diag1x == 15 || diag2x == 15){ String XWIN =("X wins!"); g.drawString(XWIN,60,50); }
上面的if语句属于x播放器,但o播放器的if语句也有相同的错误.
整个代码如下所示(我为可怜的笔记道歉)
public void paint(Graphics g){
this.setSize(450, 430); //sets the game screen size.
//initial directions
System.out.println("Player 1 (x) goes first. there are 9 boxes available.");
System.out.println("The numbers correspond to the boxes respectively.");
System.out.println("1 being top left, 2 being …Run Code Online (Sandbox Code Playgroud) 我有一个代码,其中有一个v默认值为0 的变量.我也有2个按钮:ok和nope.使用我的代码,当ok按下时,值v应为1,当nope按下时,值v应为2.但在这两种情况下,它打印的值为v2.为什么这样?我该如何纠正?
编辑:我犯了一个小错误,我已经纠正了,多亏了MadProgrammer.
import java.awt.*;
public class chk extends java.applet.Applet
{
Button ok = new Button("OK!");
Button nope = new Button("Nope");
int v = 0;
public void init()
{
setBackground(Color.white);
add(ok);
add(nope);
}
public boolean action(Event evt , Object arg)
{
if(evt.target instanceof Button)
{
check((Button)evt.target);
return true;
}
return false;
}
public void check(Button b)
{
if(b == ok);
{
v= 1;
repaint(); …Run Code Online (Sandbox Code Playgroud)