-2
我有我的代碼的這部分,但它不會初始化,我不知道該怎麼做。它不斷給我的錯誤,如 異常線程「main」顯示java.lang.NullPointerException 在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);
}}
}
爲了更好地提供幫助,請儘快發佈[SSCCE](http://sscce.org/) – Reimeus
上述代碼無法運行。好吧,它運行,但它沒有顯示GUI,它確實沒有意義。你所有的代碼都是一個很大的靜態主要方法。你可以從靜態主要方法繪製什麼GUI?如何發佈一個實際運行並顯示GUI的正在運行的* small *示例程序? –