2014-09-09 12 views
1

응용 프로그램에서 Nimbus LaF와 함께 JCombobox를 사용하고 있습니다. 서로 다른 상태에 대해 ArrowButton의 Combobox 및 Foregroundpainter의 Backgroundpainter를 재정의했습니다. 화가들은 잘 작동합니다. 그러나 글꼴 크기를 늘리면 Combobox의 화살 버튼이 늘어나고 좋아 보이지 않습니다.Combobox ArrowButton이 nimbus LaF에서 글꼴 크기를 각각 strecthing합니다.

어떻게 그것을 unstretchable하게 만드시겠습니까?

저는 Nimbus LaF의 화가를 사용하여 저 자신의 paintContext를 제공해야합니다.

다음은 문제를 나타내는 코드입니다. 여기

public class ComboTest { 

    public ComboTest(){ 

     String labels[] = { "A", "B", "C", "D" }; 

    JFrame frame = new JFrame("ComboBox Demo"); 

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    JComboBox comboBox = new JComboBox(labels); 

    frame.add(comboBox, BorderLayout.NORTH); 

    frame.setSize(300, 100); 

    frame.setVisible(true); 
     } 

    public static void main(String[] args) 
    { 

     try { 
      UIManager.setLookAndFeel(NimbusLookAndFeel.class.getName()); 

      } 

     catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
     } 

    UIManager.getLookAndFeelDefaults().put("defaultFont", new Font("Segoe UI", Font.PLAIN,16)); 

// if i put the different font size than it is strecthing, with the size 12 it looks good. 

    //UIManager.getLookAndFeelDefaults().put("defaultFont", new Font("SegoeUI",Font.PLAIN,12)); 

    UIManager.getLookAndFeelDefaults().put("ComboBox:\"ComboBox.arrowButton"[Pressed].foregroundPainter", new ComboArrowPainter(1)); 

    javax.swing.SwingUtilities.invokeLater(new Runnable() { 

    public void run() { 

      new ComboTest(); 

     } 
     }); 
    } 

    } 

내가 다행히 후광

에서
public class ComboArrowPainter extends AbstractRegionPainter { 

    static final int FOREGROUND_PRESSED = 1; 

     private int state; //refers to one of the static final ints above 

     private PaintContext ctx; 
     //the following 4 variables are reused during the painting code of the layers 

     private Path2D path = new Path2D.Float(); 

     private Color color31 = decodeColor("textForeground", 0.0f, -0.6357143f, 0.45098037f, 0); 

     public ComboArrowPainter(int state) { 

    super(); 

     this.state = state; 

      this.ctx = new PaintContext(new Insets(6,10,6,10), new Dimension(19,19), false);; 
     } 

    private Object[] componentColors; 

     @Override 

     protected void doPaint(Graphics2D g, JComponent c, int width, int height, Object[] extendedCacheKeys) { 

    //populate componentColors array with colors calculated in getExtendedCacheKeys call 

      componentColors = extendedCacheKeys; 

      paintForegroundPressed(g); 

     } 

    @Override 

     protected final PaintContext getPaintContext() { 

      return ctx; 

     } 

     private void paintForegroundPressed(Graphics2D g) { 

      path = decodePath8(); 

      g.setPaint(color31); 

      g.fill(path); 

     } 

     private Path2D decodePath8() { 

      path.reset(); 

      path.moveTo(decodeX(1.0242647f), decodeY(1.3526785f)); 

      path.lineTo(decodeX(2.0f), decodeY(0.8333333f)); 

      path.lineTo(decodeX(2.0f), decodeY(1.8571429f)); 

      path.lineTo(decodeX(1.0242647f), decodeY(1.3526785f)); 

      path.closePath(); 

      return path; 

     } 

    } 
+0

도움을 주셔서 감사합니다. [MCVE] (http://stackoverflow.com/help/mcve) – alex2410

+0

문제를 나타내는 코드를 제공해 드렸습니다. – user3792685

답변

0

했다 ArrowPainter의 클래스이다, 나는 해결책을 발견했다. 나는 글꼴 크기에 따라 컴포넌트 크기를 계산했다. 우리는 그 값을 Paintcontext로 사용했습니다. 여기 남아있는 화가 클래스의 업데이트 된 생성자는 동일합니다.

public ComboArrowPainter(int state, int FontSize) { 
      super(); 
      this.state = state; 
      double value = FontSize*1.31031746; 
      int topBottomInsssetValue =(int) Math.ceil((value)/2); 
      this.ctx = new PaintContext(new Insets(6,topBottomInsssetValue,6,topBottomInsssetValue), new Dimension(19,19), false); 

    }