我有一个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”,但这不会发生。