gpt4 book ai didi

java - 组合框箭头按钮根据 nimbus LaF 中的字体大小进行拉伸(stretch)

转载 作者:太空宇宙 更新时间:2023-11-04 14:34:37 24 4
gpt4 key购买 nike

我在我的应用程序中使用 JCombobox 和 Nimbus LaF。我已经针对不同的状态覆盖了 Combobox 的 Backgroundpainter 和 ArrowButton 的 Foregroundpainter 。油漆工们工作得很好。但是当我增加字体大小时,Combobox 的箭头按钮会拉伸(stretch),看起来不太好。

如何使其不可拉伸(stretch)?

我使用了 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();

}
});
}

}

这是 ArrowPainter 的类(class),它是我从 Nimbus 获取的

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;

}

}

最佳答案

幸运的是,我已经找到了解决办法。我已经计算了相对于字体大小的组件大小。我们已使用该值作为 Paintcontext。这是更新后的 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);

}

关于java - 组合框箭头按钮根据 nimbus LaF 中的字体大小进行拉伸(stretch),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25743102/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com