0

我有一个 Jgoodies 表格,这里是代码。

public class MainForm{
    MainForm(){
        createUIComponents();
    }
    public JPanel getMainPanel() {
        return mainPanel;
    }
    private void createUIComponents() {
       drawingPanel=new DrawingPanel();
       //there is some code, which adds components to combo boxes
    }
    private JPanel mainPanel;
    private JComboBox directDirectionCombobox;
    private JButton directLineOkButton;
    private JButton crossLineOkButton;
    private JComboBox crossLineComboBox;
    private JTextField crossLineSizeValue;
    private JButton clearButton;
    private JLabel directLineLabel;
    private JPanel directLinePanel;
    private JLabel crossLineLabel;
    private JPanel crossLinePanel;
    private JPanel okClearButtonPanel;
    private JTextField directLineSizeValue;
    private JButton saveButton;
    private JPanel drawingPanel;
}

DrawingPanel 是一个自定义类,它扩展了我想在面板上添加的 JPanel。这是用户界面的屏幕截图。用户界面 好吧,它不起作用。JPanel 已创建,但它不是我的自定义类。我试图在属性检查器中启用自定义创建字段,但它说有空指针异常。有什么方法可以将自定义类添加到 JPanel?

更新:这是 DrawingPanel 类和 MouseHandler 的代码

public class DrawingPanel extends JPanel{

        DrawingPanel(){
            mouseHandler=new MouseHandler(this);
            addMouseListener(mouseHandler);
        }
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            Point=mouseHandler.getPoint();
            if (Point!=null){
                System.out.println(Point.getX()+" "+Point.getY());
            }
            g2 = (Graphics2D) g;
            g2.setColor(Color.BLACK);
            g2.setStroke(new BasicStroke(2));
            g2.draw(new Line2D.Double(0,0,10,10));
        }
        private Graphics2D g2;
        private MouseHandler mouseHandler;
        private Point2D Point;
    }

public class MouseHandler implements MouseListener
{
    MouseHandler(DrawingPanel thePanel)
    {
        this.panel=thePanel;
    }
    public void mouseClicked(MouseEvent event)
    {
        System.out.println("OK");
        Point=event.getPoint();
        panel.repaint();
    }

    public void mouseEntered(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    public void mouseExited(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    public void mousePressed(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseReleased(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }
    public Point2D getPoint()
    {
        Point2D thePoint=this.Point;
        this.Point=null;
        return thePoint;
    }
    private Point2D Point;
    private DrawingPanel panel;
}

因此,当我在白色字段上单击鼠标时,它应该在控制台上写“OK”,但这并没有发生。

4

0 回答 0