如何初始化图形?因为它说它没有被初始化 [英] How to initialize Graphics? because it says it was not initialized

查看:171
本文介绍了如何初始化图形?因为它说它没有被初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的代码的这部分,但它不会初始化,我不知道该怎么做。它不断给我一个错误,如
线程main中的异常java.lang.NullPointerException
at memor.main(memor.java:131)

  import java.awt。*; 

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class memor extends JFrame
{
private static final long serialVersionUID = 1L;


public static void main(String args []){



final JPanel pan;
GridLayout h = new GridLayout(3,3);


pan = new JPanel(h);
JButton button1 =新JButton(1);
pan.add(button1);
final JLabel label1 = new JLabel(hi);
label1.setVisible(false);
pan.add(label1);
JButton button2 =新JButton(2);
pan.add(button2);
final JLabel label2 = new JLabel(hi);
label2.setVisible(false);
pan.add(label2);
JButton button3 =新JButton(3);
pan.add(button3);
final JLabel label3 = new JLabel(hi);
label3.setVisible(false);
pan.add(label3);
JButton button4 =新JButton(4);
pan.add(button4);
final JLabel label4 = new JLabel(hi);
label4.setVisible(false);
pan.add(label4);
JButton button5 =新JButton(5);
pan.add(button5);
final JLabel label5 = new JLabel(hi);
label5.setVisible(false);
pan.add(label5);
JButton button6 =新JButton(6);
pan.add(button6);
final JLabel label6 = new JLabel(hi);
label6.setVisible(false);
pan.add(label6);
JButton button7 =新JButton(7);
pan.add(button7);
final JLabel label7 = new JLabel(hi);
label7.setVisible(false);
pan.add(label7);
JButton button8 =新JButton(8);
pan.add(button8);
final JLabel label8 = new JLabel(howdy);
label8.setVisible(false);
pan.add(label8);
JButton button9 =新JButton(9);
pan.add(button9);

final JLabel label9 = new JLabel(hi);
label9.setVisible(false);
pan.add(label9);
JLabel l =新的JLabel(网格布局);
l.setHorizo​​ntalAlignment(SwingConstants.CENTER);
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0){
label1.setVisible(true);
}
});
button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0){
label2.setVisible(true);
}
});
button3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0){
label3.setVisible(true);
}
});
button4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0){
label4.setVisible(true);
}
});
button5.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0){
label5.setVisible(true);
}
});
button6.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0){
label6.setVisible(true);
}
});
button7.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0){
label7.setVisible(true);
}
});
button8.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0){
label8.setVisible(true);
}
});
button9.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0){
label9.setVisible(true);

}
} );


if(button1.isEnabled()){
Graphics g = pan.getGraphics();
g.setColor(new Color(156,93,82));
g.fill3DRect(21,3,7,12,true);
g.setColor(new Color(156,23,134));
g.fillOval(1,15,15,15);
g.fillOval(16,15,15,15);
g.fillOval(31,15,15,15);
g.fillOval(7,31,15,15);
g.fillOval(22,31,15,15);
g.fillOval(16,47,15,15);


}}


}


<如果这是一个Swing应用程序,那么你应该绘制一个BufferedImage,在这种情况下,你可以通过 getGraphics(),或者您将在JPanel的 paintComponent(Graphics g)方法中执行此绘图,并使用传入的Graphics对象方法由JVM。

为了配合JButton被启用或不启用,我会添加一个ChangeListener到按钮的模型,并调用repaint,如果enabled属性更改,然后在JPanel的paintComponent方法中有一个if块,并根据是否在按钮的启用状态上绘制形状。



有关更详细的帮助,请考虑发布 sscce


I have this part of my code but it will not initialize and I do not know how to do it. It keeps giving me an error such as Exception in thread "main" java.lang.NullPointerException at memor.main(memor.java:131)

import java.awt.*;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class memor extends JFrame
{
private static final long serialVersionUID = 1L;


public static void main(String args[]){



final JPanel pan;
GridLayout h=new GridLayout(3,3);


pan =new JPanel(h);
JButton button1= new JButton("1");
pan.add(button1);
final JLabel label1= new JLabel("hi");
label1.setVisible(false);
pan.add(label1);
JButton button2= new JButton("2");
pan.add(button2);
final JLabel label2= new JLabel("hi");
label2.setVisible(false);
pan.add(label2);
JButton button3= new JButton("3");
pan.add(button3);
final JLabel label3 = new JLabel("hi");
label3.setVisible(false);
pan.add(label3);
JButton button4 = new JButton("4");
pan.add(button4);
final JLabel label4 = new JLabel("hi");
label4.setVisible(false);
pan.add(label4);
JButton button5= new JButton("5");
pan.add(button5);
final JLabel label5= new JLabel("hi");
label5.setVisible(false);
pan.add(label5);
JButton button6= new JButton("6");
pan.add(button6);
final JLabel label6= new JLabel("hi");
label6.setVisible(false);
pan.add(label6);
JButton button7= new JButton("7");
pan.add(button7);
final JLabel label7= new JLabel("hi");
label7.setVisible(false);
pan.add(label7);
JButton button8= new JButton("8");
pan.add(button8);
final JLabel label8= new JLabel("howdy");
label8.setVisible(false);
pan.add(label8);
JButton button9= new JButton("9");
pan.add(button9);

final JLabel label9= new JLabel("hi");
label9.setVisible(false);
pan.add(label9);
JLabel l=new JLabel("grid layout");
l.setHorizontalAlignment(SwingConstants.CENTER);
button1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        label1.setVisible(true);
    }
});
button2.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        label2.setVisible(true);
    }
});
button3.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        label3.setVisible(true);
    }
});
button4.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        label4.setVisible(true);
    }
});
button5.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        label5.setVisible(true);
    }
});
button6.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        label6.setVisible(true);
    }
});
button7.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        label7.setVisible(true);
    }
});
button8.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        label8.setVisible(true);
    }
});
button9.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        label9.setVisible(true);

    }
});


if (button1.isEnabled()){
Graphics g= pan.getGraphics();
g.setColor(new Color(156, 93, 82));
g.fill3DRect(21,3,7,12, true);
g.setColor(new Color(156,23,134));
g.fillOval(1,15,15,15);
g.fillOval(16,15,15,15);
g.fillOval(31,15,15,15);
g.fillOval(7,31,15,15);
g.fillOval(22,31,15,15);
g.fillOval(16,47,15,15);


}}


}

解决方案

If this is a Swing application, then you should either be drawing to a BufferedImage, in which case you'd get the BufferedImage's Graphics object via getGraphics(), or you would be doing this drawing in a JPanel's paintComponent(Graphics g) method and would use the Graphics object passed into the method by the JVM.

To tie this in to a JButton's being enabled or not, I'd add a ChangeListener to the button's model, and call repaint if the enabled property changes, and then would have an if block in the JPanel's paintComponent method and base whether or not to paint the shapes on the button's enabled state.

For more detailed help, consider posting an sscce.

这篇关于如何初始化图形?因为它说它没有被初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆