How to initialize Graphics? because it says it was not initialized [closed]

血红的双手。 提交于 2020-01-11 14:35:08

问题


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);


}}


}

回答1:


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.



来源:https://stackoverflow.com/questions/19128648/how-to-initialize-graphics-because-it-says-it-was-not-initialized

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!