我得到了:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.fractal.walkers.DrawingPanel.myPaint(DrawingPanel.java:65)
at com.fractal.walkers.MainClass$2.actionPerformed(MainClass.java:1256)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
。 。
看来tmp正在初始化但我的DrawingPanel无法看到新值。我是一个初学者,试图通过反复试验来学习。我正在努力学习一个多星期。感谢帮助。
public class MainClass extends JFrame {
JFrame frame;
JPanel pane;
JTable table;
DefaultTableModel model;
ArrayList<Object> arrX;
ArrayList<Object> arrY;
JButton button;
JButton button2;
Integer [][] tmp;
Integer[] xAxis;
Integer[] yAxis;
ListIterator i;
int x1=0;
int y1=0;
int x2 = 0;
int y2= 0;
Thread thread;
JScrollPane scrollPane;
public void framesetter(){
pane = new JPanel();
button = new JButton();
button2 = new JButton();
scrollPane = new JScrollPane();
frame = new JFrame();
table = new JTable();
DrawingPanel drawpane = new DrawingPanel();
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1280,800);
arrX = new ArrayList<Object>();
arrY = new ArrayList<Object>();
table.setModel(new DefaultTableModel(
new Object[][] {
{null, null},
{null, null},
{null, null},
{null, null},
{null, null},
{null, null},
{null, null},
{null, null},
{null, null},
{null, null},
{null, null},
{null, null},
{null, null},
},
new String[] {
"X Axis", "Y Axis"
}
));
pane.setLayout(null);
frame.setLayout(null);
scrollPane.setViewportView(table);
scrollPane.setSize(180,220);
scrollPane.setLocation(10, 10);
button.setSize(100,50);
button.setLocation(50,250);
button2.setSize(100,50);
button2.setText("DRAW");
button2.setLocation(50,500);
pane.setLocation(1050,10 );
pane.setSize(200, 780);
pane.add(scrollPane);
pane.add(button);
pane.add(button2);
drawpane.setLocation(10, 10);
drawpane.setSize(1040, 780);
frame.add(drawpane);
frame.add(pane);
frame.setVisible(true);
model = (DefaultTableModel) table.getModel();
try
{
jbInit();
}
catch (Exception e)
{
e.printStackTrace();
}
button2.addActionListener(new ActionListener () {
public void actionPerformed(ActionEvent e) {
drawpane.decide = true;
drawpane.repaint();
// TODO Auto-generated method stub
}
});
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
i = arrX.listIterator();
if(model.getValueAt(0, 0)!=null){
for(int x = 0; x<model.getRowCount();x++){
if(model.getValueAt(x, 0)!=null){
arrX.add(model.getValueAt(x, 0));
arrY.add(model.getValueAt(x, 1));
if(x+1 == model.getRowCount()){
arrX.add(model.getValueAt(x, 0));
arrY.add(model.getValueAt(x, 1));
}
}
}
for(int y= 0 ; y<arrX.size()-1;y++){
x1 = Integer.valueOf((String) arrX.get(y));
y1 = Integer.valueOf((String) arrY.get(y));
x2 = Integer.valueOf((String) arrX.get(y+1));
y2 = Integer.valueOf((String) arrY.get(y+1));
System.out.println(x1 + " "+ y1 + " "+x2+" "+ arrX.size());
}
xAxis = new Integer[arrX.size()];
yAxis = new Integer[arrY.size()];
for(int xS = 0; xS<arrX.size();xS++){
xAxis[xS] = (Integer.valueOf((String) arrX.get(xS)));
;
}
for(int yS = 0; yS<arrY.size();yS++){
yAxis[yS] = (Integer.valueOf((String) arrY.get(yS)));
}
tmp = new Integer [arrX.size()][arrY.size()];
for(int xS = 0 ; xS<arrX.size();xS++){
for(int yS = 0 ; yS<2;yS++){
if(yS == 0){
tmp[xS][yS] =xAxis [xS];
}
else {tmp[xS][yS] =yAxis [xS];}
System.out.println("tmp "+"["+xS+"]" +"["+yS+"]= "+ tmp[xS][yS]);
}
}
}
}
});
}
public Integer recharge(int a , int b){
this.tmp[a][b]= tmp[a][b];
return tmp[a][b];
}
private void jbInit() throws Exception
{
// This is the line that does all the magic!
ExcelAdapter myAd = new ExcelAdapter(table);
}
public static void main(String[] args) {
MainClass f = new MainClass();
f.framesetter();
}
}
这是绘图类:
public class DrawingPanel extends JPanel{
Boolean decide = false;
MainClass f = new MainClass();
int x, y;
public DrawingPanel(){
}
public void paintBorder(Graphics g){
g.setColor(Color.BLACK);
g.drawRect(0, 0, 1000, 700);
}
@Override
public void paintComponent(Graphics g){
super.paintComponents(g);
Graphics2D g2d;
g2d = (Graphics2D) g;
g2d.setColor(Color.BLACK);
g2d.setBackground(Color.WHITE);
g2d.drawLine(500, 0, 500, 700);
g2d.drawLine(0, 350, 1000, 350);
if (decide) {
myPaint(g2d);
}
}
private void myPaint(Graphics g2d) {
Integer value = new Integer(f.tmp.length);
if(value != null && value != 0){
for(int xS = 0; xS<f.tmp.length;xS++){
g2d.drawLine(f.tmp[xS][0],f.tmp[xS][1],f.tmp[xS+1] [0],f.tmp[xS+1][1]);
}
}
decide = false;
}
}
我认为这与判断布尔值为false和true有关。如何初始化变量或将Array传递给绘图函数。谢谢。
jbinit用于粘贴Excel表格中的值。工作正常。