gpt4 book ai didi

Java 合成器 : Problem with JTabbedPane and SynthGraphicsUtils

转载 作者:行者123 更新时间:2023-12-01 16:01:22 25 4
gpt4 key购买 nike

在我的 Java 应用程序中,我有一个 JTabbedPane 和一个合成器外观。 l&f 由 xml 文件定义。这对我来说非常有效。

现在的挑战是:我希望 JTabbedPane 选项卡中的文本垂直书写,而不是水平书写。为此,我对 SynthGraphicsUtils 进行了子类化,并重写了 PaintText(SynthContext ss, Graphics g, String text, int x, int y, int mnemonicIndex) 方法。这也有效。

现在的问题是:不同选项卡中的文本是垂直显示的,但每个选项卡的大小似乎计算错误。选项卡未正确分隔。不幸的是,我不允许发布图像...选项卡相互重叠。

我不知道如何解决这个问题:-(这是我的代码:

SwingTest.java

public SwingTest(JFrame owner) {

super(null);

setOpaque(true);
setSize(WIDTH, HEIGHT);
owner.setSize(WIDTH, HEIGHT);

JTabbedPane pane = new JTabbedPane(JTabbedPane.LEFT);
pane.setName("MyPane");
pane.setSize(WIDTH, HEIGHT);

// Add a tab
JPanel p1 = new JPanel();
p1.setSize(300, 300);

JPanel p2 = new JPanel();
p2.setSize(500, 500);

JPanel p3 = new JPanel();
p3.setSize(300, 300);

p3.add(new JLabel("HALLO"));

JPanel p4 = new JPanel();
p4.setSize(300, 300);

JButton roundedButton = new JButton("Halloi Button");
roundedButton.setName("roundedButton");

p2.add(roundedButton);

pane.addTab(EFFORT_CARD, p1);
pane.addTab(EMOTION_CARD, p2);
pane.addTab(COMFORT_CARD, p3);
pane.addTab(DRIVING_CARD, p4);

add(pane);

pane.updateUI();

laf.xml

<!-- 
TABNAVIGATION
-->
<style id="tabNavigationStyle">
<!-- Angabe ist wichtig, da die Hintergrundfarben sonst nicht angezeigt
werden. -->
<!-- <object id="verticalTextWriterClass" class="VerticalTextWriter" />
<painter method="text" idref="verticalTextWriterClass"/> -->
<opaque value="TRUE" />
<state>
<font name="Verdana" size="12" />
<color value="WHITE" type="BACKGROUND" />
<color value="BLACK" type="FOREGROUND" />
<color value="BLUE" type="TEXT_FOREGROUND" />
<object id="myGraphicsUtils" class="MySynthGraphicsUtils" />
<graphicsUtils idref="myGraphicsUtils" />
</state>
<!-- Fuer den Zustand "SELECTED" einen anderen Style definieren. -->
<state value="SELECTED">
<!-- Auf weiter oben definierte Farbe beziehen. -->
<color value="WHITE" type="BACKGROUND" />
<color idref="magentaForegroundColor" type="TEXT_FOREGROUND" />
</state>
<state value="FOCUSED">
<!-- Auf weiter oben definierte Farbe beziehen. -->
<color value="WHITE" type="BACKGROUND" />
<color idref="magentaForegroundColor" type="TEXT_FOREGROUND" />
</state>
<state value="PRESSED">
<!-- Auf weiter oben definierte Farbe beziehen. -->
<color value="WHITE" type="BACKGROUND" />
<color idref="magentaForegroundColor" type="TEXT_FOREGROUND" />
</state>
</style>
<!-- Den Style fuer die Tabnavigation an die Tabnavigation mit dem Namen (type="name")
"MyPane" binden. Der Name wird in Java ueber component.setName() festgelegt. -->
<bind style="tabNavigationStyle" type="region" key="TabbedPaneTab" />

MySynthGraphicsUtils.java

public class MySynthGraphicsUtils extends SynthGraphicsUtils {
public void paintText(SynthContext ss, Graphics g, String text, int x, int y, int mnemonicIndex) {

Graphics2D g2 = (Graphics2D) g;

System.out.println(ss.getComponent().getClass().getName());

Object oldAAValue = g2.getRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING);
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);

g2.setFont(ss.getStyle().getFont(ss));
AffineTransform tx = new AffineTransform();
tx.rotate(90 * Math.PI / 180, x, y);
g2.setTransform(tx);

g2.drawString(text, x, y);

// get metrics from the graphics
FontMetrics metrics = g2.getFontMetrics(ss.getStyle().getFont(ss));
// get the height of a line of text in this font and render context
int hgt = metrics.getHeight();
// get the advance of my text in this font and render context
int adv = metrics.stringWidth(text);
// calculate the size of a box to hold the text with some padding.
Dimension size = new Dimension(adv + 2, hgt + 2);

//g2.setClip(0, 0, size.width, size.height);

g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, oldAAValue);
}

}

非常感谢!!

最佳答案

作为解决方法,我使用左侧带有 sourceInset 的图像边框来分隔选项卡。我使图像的颜色与应用程序的背景颜色相同:

        <state>
<opaque value="true"/>
<color value="black" type="BACKGROUND" />
<color value="white" type="TEXT_FOREGROUND" />
<imagePainter method="tabbedPaneTabBorder" path="relative/path/to/white_border.png"
sourceInsets="0 1 0 0" paintCenter="false"/>
<insets top="3" bottom="3" right="3" left="3"/>
</state>

关于Java 合成器 : Problem with JTabbedPane and SynthGraphicsUtils,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3798561/

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