JPanel Paint方法

时间:2016-04-13 14:38:04

标签: java swing paint

我试图在使用重绘方法选择文件后显示图形,它没有向我提供任何错误但是我做错了它没有显示图形,我是新来的油漆方法,所以我认为这就是为什么它不起作用。

代码:

private final JPanel graphArea = new JPanel();
    private final JButton read = new PosJButton("Read", 0);
    private final JButton breadth = new PosJButton("Breadth", 0);
    private final JButton depth = new PosJButton("Depth", 0);

以下是我试图输出图表的JPanel:

private class graphArea extends JPanel    {

        public graphArea()  {
            graphArea.setPreferredSize(new Dimension(255,255));
        }

        @Override
        public void paintComponent(Graphics g) {

    }
    }

    //method creating a new gui
    public static void makeAndShowGUI() {
        graphGUI gGUI = new graphGUI();
        gGUI.showGUI();
    }

        //setting items for gui
yout);
            listArea.setRows(1);
);
            contentPane.add(buttonPanel);
            contentPane.add(graphArea);    




                                    newStation.addStation(name, posx, posy);
                                    System.out.println("Station test: " + name + " " + posx + " " + posy);
                                }
                                else if(type.equals("Connection:")){
                                    String statA = sc.next();
                                    String statB = sc.next();
                                    double dist = sc.nextDouble();



                                    newStation.addConnection(statA, statB, dist);
                                    System.out.println("Connection test: " + statA + " " + statB + " " + dist);
                                }
                            }

我重新粉刷小组的课程:

graphArea.repaint();
                        }catch(FileNotFoundException ex){
                            JOptionPane.showMessageDialog(null, "invalid file format", "Error", JOptionPane.ERROR_MESSAGE);
                        }


        }
      }
    });

这是我尝试重新绘制jpanel graphArea的地方:

inv_id   inv_date    inv_export   inv_code
1        2016-03-14  0            a2
2        2016-03-13  0            a1
3        2016-04-13  1            a1
4        2016-03-14  0            a1

1 个答案:

答案 0 :(得分:0)

首先,类名应以大写字母开头。

GraphArea的大小(注意正确的名称)是(0,0),所以没有什么可以画的。

您需要覆盖getPreferredSize()方法以返回面板的大小。然后布局管理器可以正常工作。