2015-10-30 49 views
2

我使用DrawPad example.在Model UIDelegate中創建了一個服裝組件。但是,出於某種原因,我收到了錯誤UIDefaults.getUI() failed: no ComponentUI class。我甚至不確定我是否正確實施了Model UIDelegate。但是,爲什麼我得到這個錯誤?服裝組件錯誤「no ComponentUI class」

主要

public class Main { 
    static JFrame frame; 
    static JButton clearButton; 
    static DrawPad drawPad; 

    public static void main(String[] args) { 
     UIManager.put("DrawPadUI", "BasicDrawPadUI"); 
     frame = new JFrame(); 
     drawPad = new DrawPad(); 
     clearButton = new JButton("Clear"); 
     frame.add(drawPad, BorderLayout.CENTER); 

     clearButton.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       // TODO Auto-generated method stub 
       Graphics g = frame.getGraphics(); 
       g.setColor(Color.white); 
       g.fillRect(0, 0, frame.getWidth(), frame.getHeight()); 

      } 
     }); 
     frame.add(clearButton, BorderLayout.SOUTH); 
     frame.setSize(280, 300); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 
    } 
} 

BasicDrawPadUI

public class BasicDrawPadUI extends ComponentUI implements MouseListener, MouseMotionListener { 
    Image image; 
    Graphics2D graphics2D; 
    int currentX, currentY, oldX, oldY; 

    JFrame frame; 
    JButton clearButton; 

    public static ComponentUI createUI(JComponent c) { 
     return new BasicDrawPadUI(); 
    } 

    public void paintComponent(Graphics g, JComponent c) { 
     if (image == null) { 
      image = c.createImage(c.getWidth(), c.getHeight()); 
      graphics2D = (Graphics2D) image.getGraphics(); 
      graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
      RenderingHints.VALUE_ANTIALIAS_ON); 
      clear(c); 
     } 
     g.drawImage(image, 0, 0, null); 
     } 

    @Override 
    public void mouseDragged(MouseEvent e) { 
     // TODO Auto-generated method stub 
     currentX = e.getX(); 
     currentY = e.getY(); 
     if (graphics2D != null) 
      graphics2D.drawLine(oldX, oldY, currentX, currentY); 
     //repaint(); 
     oldX = currentX; 
     oldY = currentY; 
    } 

    public void clear(JComponent c) { 
     graphics2D.setPaint(Color.white); 
     graphics2D.fillRect(0, 0, c.getWidth(), c.getHeight()); 
     graphics2D.setPaint(Color.black); 
     c.repaint(); 
    } 


    @Override 
    public void mouseMoved(MouseEvent e) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void mouseClicked(MouseEvent e) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void mousePressed(MouseEvent e) { 
     // TODO Auto-generated method stub 
     oldX = e.getX(); 
     oldY = e.getY(); 
    } 

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

    } 

    @Override 
    public void mouseEntered(MouseEvent e) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void mouseExited(MouseEvent e) { 
     // TODO Auto-generated method stub 

    } 
} 

DrawPad

class DrawPad extends JComponent { 

    private final static String ID = "DrawPadUI"; 

    public DrawPad() { 
     updateUI(); 
    } 

    public void updateUI() { 
     setUI(UIManager.getUI(this)); 
    } 

    @Override 
    public String getUIClassID() { 
     return ID; 
    } 
} 
+0

你有什麼包你班? – MadProgrammer

+0

所有的課程都在同一個軟件包'drawpad' –

回答

4

變化UIManager.put("DrawPadUI", "BasicDrawPadUI");UIManager.put("DrawPadUI", "drawpad.BasicDrawPadUI");

你需要指定日的UI類電子全限定類名,包括包名

,我需要看看可以肯定的,但是當它調用createUI中,你可以抓住一個參考組件

ComponentUI覆蓋installUI方法,並在調用它時維護對傳遞給您的組件的引用。

private DrawPad drawPad; 
//... 
@Override 
public void installUI(JComponent c) { 
    drawPad = (DrawPad) c; 
    // Install required listeners and other functionality 
} 

同樣,在uninstallUI,您應該取消引用任何強引用你有和卸載你的聽衆

@Override 
public void uninstallUI(JComponent c) { 
    // Uninstall any listeners 
    drawPad = null; 
} 

看一看custom java Swing component Model, UIDelegate, component format一個完整的實現。

而且,你永遠不應該保持對你沒有創建自己Graphics2D上下文的引用

+0

工作感謝。如果可能,還有一個問題。當我嘗試從BasouDrawPadUI中的mousedragged訪問repaint()時,我不能。這是爲什麼?我想我必須像使用clear()方法一樣使用JConponent,但我該怎麼做? –

+0

我需要確保一下,但是當它調用createUI時,您可以獲取對組件的引用 – MadProgrammer

+0

如果這是一個愚蠢的問題,我表示歉意,但是您是什麼意思「獲取對組件的引用」 ? –