Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我在netbeans图形模式下的java代码中遇到了一个问题_Java_Button - Fatal编程技术网

我在netbeans图形模式下的java代码中遇到了一个问题

我在netbeans图形模式下的java代码中遇到了一个问题,java,button,Java,Button,我尝试用java创建按钮,用这些代码绘制一些东西 import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class primeraapplet extends Applet implements ActionListener{ Button bt1, bt2, bt3; int type=-1; @Override public void init() {

我尝试用java创建按钮,用这些代码绘制一些东西

import java.applet.Applet;
import java.awt.*;
import  java.awt.event.*;

public class primeraapplet extends Applet implements ActionListener{

    Button bt1, bt2, bt3;
    int type=-1;

    @Override
    public void init() {
    setLayout(null);
    bt1= new Button("Linia");
    bt2=new Button("Rectangle");
    bt2=new Button("Rodona");

    bt1.addActionListener(this);
    bt2.addActionListener(this);
    bt3.addActionListener(this);

    bt1.setBounds(10, 20, 100, 50);
    bt2.setBounds(120, 20, 100, 50);
    bt3.setBounds(10, 20, 100, 50);

    bt1.setForeground(Color.blue);
    bt1.setBackground(Color.black);

    bt1.setFont(new Font("times New Roman", Font.BOLD,16));
    bt2.setForeground(Color.red);
    bt2.setBackground(Color.yellow);
    bt2.setFont(new Font("times New Roman", Font.BOLD,16));

    bt3.setForeground(Color.black);
    bt3.setBackground(Color.white);
    bt3.setFont(new Font("times New Roman", Font.BOLD,16));

    add(bt1);
    add(bt2);
    add(bt3);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Button ref;
        ref = (Button) e.getSource();
        if (ref==bt1)
            type=1;
        else if (ref==bt2)
            type=2;
        else
            type=3;
        repaint();
    }

    @Override
    public void paint(Graphics g) {
        if (type==1)
            g.drawLine(100, 65, 90, 150);
        else if (type==2)
           g.drawRect(100, 65, 90, 190);
        else if(type==2)
            g.drawOval(100, 65, 90, 90);
    }
}
这给了我一个错误:

java.lang.NullPointerException
    at primeraapplet.init(primeraapplet.java:34)
    at sun.applet.AppletPanel.run(AppletPanel.java:434)
    at java.lang.Thread.run(Thread.java:745)

为什么要将布局设置为空

setLayout(null);  // shouldn't this line be the source of your problems?

代码中的变量错误。您不能将相同的nam与新按钮一起设置为相同的变量

指出错误实际发生在哪一行是很好的,特别是因为我删除了一堆无用的注释。在
paint()
方法中,为什么有两个条件完全相同的
else if
语句?另外,若语句需要大括号,那个么它就不起作用了。同上
actionPerformed()
。还有
paint()
使用了
type
,但从未声明过。这里有很多问题。避免使用
null
布局,像素完美的布局在现代ui设计中是一种错觉。影响零部件单个尺寸的因素太多,您无法控制。该API旨在与核心的布局管理器协同工作,丢弃它们将导致无止境的问题,您将花费越来越多的时间试图纠正这些问题
@Override
    public void init() {
    setLayout(null);
    bt1= new Button("Linia");
    bt2=new Button("Rectangle");
    bt3=new Button("Rodona");  //You had bt2=new Button("Rodona");