简体   繁体   English

Combobox ArrowButton正在对应于nimbus LaF中的字体大小

[英]Combobox ArrowButton is strecthing respective to the font size in nimbus LaF

I am using JCombobox with Nimbus LaF in my application. 我在我的应用程序中将JCombobox与Nimbus LaF一起使用。 I have override the Backgroundpainter of Combobox and Foregroundpainter of ArrowButton for different states. 我已经为不同的状态覆盖了Combobox的Backgroundpainter和ArrowButton的Foregroundpainter。 The painters are working fine. 画家们做得很好。 but when I increase the size of the font, the arrowbutton of Combobox is stretching and it is not looking good. 但是当我增加字体大小时,Combobox的箭头按钮伸展了,并且看起来不太好。

How to make it unstretchable? 如何使其不可拉伸?

I have used the painters from Nimbus LaF itself there i have to provide my own paintContext. 我使用了Nimbus LaF本身的画家,在那里我必须提供自己的paintContext。

Here is the code that shows the problem. 这是显示问题的代码。

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();

      }
         });
    }

    }

Here is the class for ArrowPainter, which i took from Nimbus 这是我从Nimbus那里学习的ArrowPainter的类

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;

      }

    }

Fortunately, I have found the solution. 幸运的是,我找到了解决方案。 I have calculated the component size respective to the fontsize. 我已经计算了字体大小对应的组件大小。 We have used that value as our Paintcontext. 我们已将该值用作Paintcontext。 Here is the updated constructor of painter class remaining will be the same. 这是painter类的更新构造函数,其余部分将保持不变。

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); 

    }  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM