简体   繁体   English

为什么我的RichJLabel文本的一部分看起来被覆盖/隐藏了?

[英]Why does part of my RichJLabel text look covered/hidden?

I've been reading the Swing Hacks book and have used some of their code for the RichJLabel part. 我一直在阅读《 Swing Hacks》一书,并在RichJLabel部分中使用了一些代码。 I understand what the code does, but not why some of the word is covered or looks hidden. 我了解代码的作用,但不了解为什么某些单词被覆盖或看起来是隐藏的。 It's not that it's not drawing all of the text because even half of the 'a' in horizontal is missing. 不是因为它没有绘制所有文本,因为水平的'a'一半都丢失了。

//imported libraries for the GUI
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.*;

import java.awt.Toolkit;
import java.awt.Dimension;
import java.awt.Container;
import java.awt.BorderLayout;
import java.awt.*;

//Rich JLabel
import java.awt.GraphicsEnvironment;
import java.awt.Graphics2D;
import java.awt.FontMetrics;
import java.awt.RenderingHints;
import java.awt.font.*;
import javax.swing.ListSelectionModel;
import java.awt.Color; 
import java.awt.Font;


public class nameInterfaceOne
{
    //Declared components
    static JFrame frame;
    static JPanel TotalGUI, northP, southP, eastP, centerP, westP;
    static JButton buttons;



//Frame method
public nameInterfaceOne()
{       
    try {
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        }   //For a different look & feel, change the text in the speech marks

    catch (ClassNotFoundException e) {}
    catch (InstantiationException e) {}
    catch (IllegalAccessException e) {}
    catch (UnsupportedLookAndFeelException e) {}



    frame = new JFrame("Interface");
    frame.setExtendedState(frame.NORMAL);

    frame.getContentPane().add(create_Content_Pane());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500, 500); //Size of main window
    frame.setVisible(true);

    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

    // gets & sets frame size/location
    int fw = frame.getSize().width;
    int fh = frame.getSize().height;
    int fx = (dim.width-fw)/2;
    int fy = (dim.height-fh)/2;

    //moves the frame
    frame.setLocation(fx, fy);
}

public JPanel create_Content_Pane()
{
    TotalGUI = new JPanel();
    TotalGUI.setLayout(new BorderLayout(5,5));  //set layout for the Container Pane

    northP = new JPanel();
    northP.setBorder(new TitledBorder(new EtchedBorder(), "Label"));
    TotalGUI.add(northP, BorderLayout.NORTH);

        RichJLabel label = new RichJLabel("Horizontal", 1);
        label.setLeftShadow(1,1,Color.white);
        label.setRightShadow(1,1,Color.gray);
        label.setForeground(Color.black);
        label.setFont(label.getFont().deriveFont(20f));

    Box top = Box.createHorizontalBox();
    top.add(Box.createHorizontalStrut(10));
    top.add(label);
    top.add(Box.createHorizontalStrut(10));
    northP.add(top);


    //EAST Panel
    eastP = new JPanel();
    eastP.setBorder(new TitledBorder(new EtchedBorder(), "Boxes"));
    TotalGUI.add(eastP, BorderLayout.EAST);

    Box right = Box.createVerticalBox();
    right.add(Box.createVerticalStrut(20));
    right.add(new JLabel("EAST SIDE!"));
    eastP.add(right);

    //WEST Panel
    westP = new JPanel();
    westP.setBorder(new TitledBorder(new EtchedBorder(), "Buttons"));
    TotalGUI.add(westP, BorderLayout.WEST);

    Box left = Box.createVerticalBox();
    left.add(Box.createVerticalStrut(10));
    ButtonGroup JbuttonGroup = new ButtonGroup();
    JButton buttons;
    JbuttonGroup.add(buttons = new JButton("One"));
    buttons.setToolTipText("This is Button One");
    left.add(buttons);
    left.add(Box.createVerticalStrut(10));
    JbuttonGroup.add(buttons = new JButton("Two"));
    buttons.setToolTipText("This is Button Two");
    left.add(buttons);
    left.add(Box.createVerticalStrut(10));
    JbuttonGroup.add(buttons = new JButton("Three"));
    buttons.setToolTipText("This is Button Three");
    left.add(buttons);
    left.add(Box.createVerticalStrut(10));
    westP.add(left);


    TotalGUI.setOpaque(true);
    return(TotalGUI);
}
    //Main method calling a new object of 
public static void main(String[] args)
{
    new nameInterfaceOne();
    }
}
//RICH JLABEL CLASS
class RichJLabel extends JLabel
{
    private int tracking;
    public RichJLabel(String text, int tracking) {
        super(text);
        this.tracking = tracking;
    }

    private int left_x, left_y, right_x, right_y;
    private Color left_color, right_color;

    public void setLeftShadow(int x, int y, Color color) {
        left_x = x;
        left_y = y;
        left_color = color;
    }
    public void setRightShadow(int x, int y, Color color) {
        right_x = x;
        right_y = y;
        right_color = color;
    }

public Dimension getPreferredSize() 
{
    String text = getText();
    FontMetrics fm = this.getFontMetrics(getFont());

    int w = fm.stringWidth(text);
    w += (text.length())*tracking;
    w += left_x + right_x;
    int h = fm.getHeight();
    h += left_y + right_y;

    return new Dimension(w,h); 
}

public void paintComponent(Graphics g) {
    ((Graphics2D)g).setRenderingHint(
        RenderingHints.KEY_TEXT_ANTIALIASING,
        RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    char[] chars = getText().toCharArray();

    FontMetrics fm = this.getFontMetrics(getFont());

    int h = fm.getAscent();
    int x = 0;

    for(int i=0; i<chars.length; i++) 
    {
        char ch = chars[i];
        int w = fm.charWidth(ch) + tracking;

        g.setColor(left_color);
        g.drawString(""+chars[i],x-left_x,h-left_y);

        g.setColor(right_color);
        g.drawString(""+chars[i],x+right_x,h+right_y);

        g.setColor(this.getForeground());
        g.drawString(""+chars[i],x,h);

        x+=w;
    }

    ((Graphics2D)g).setRenderingHint(
        RenderingHints.KEY_TEXT_ANTIALIASING,
        RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT);

} // end paintComponent()
}

在此处输入图片说明

Thank you in advance for any help :) 预先感谢您的任何帮助 :)

Empirically, the problem disappears when adding the label directly to the (default) FlowLayout of northP . 根据经验,将标签直接添加到northP的(默认) FlowLayout时,问题消失了。

northP.add(label);

在此处输入图片说明

Addendum: An alternative is to override getMaximumSize() , as suggested in How to Use BoxLayout: Specifying Component Sizes . 附录:一种替代方法是重写getMaximumSize() ,如如何使用BoxLayout:指定组件大小中所建议

@Override
public Dimension getMaximumSize() {
    return new Dimension(Short.MAX_VALUE, Short.MAX_VALUE);
}

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

相关问题 为什么我的JPanel在具有特定大小的情况下显示为灰色,而在没有其他大小的情况下却显示为灰色? - Why does my JPanel look gray with a certain size but not with another? 为什么我的swing GUI在设计上看起来与运行时不同? - Why does my swing GUI look different in design than at run-time? 为什么对话框中的JButton看起来很奇怪 - Why do the JButton in my dialog look weird 为什么当我启动我的程序时我的外观和感觉看起来很奇怪? - Why is my look and feel look very weird when I launch my program? Swing - 如何模仿为什么我的外观和感觉会禁用图标? - Swing - How to imitate the why my look and feel draws disabled icons? 我在Java中的滑块在所有平台上的外观和风格都不相同 - My slider in java does not give same look and feel in all platform JTextArea我想让我的文本部分变为粗体或草书 - JTextArea I want to make my part of text bold or cursive 为什么我的JFrame无法更新 - Why does my JFrame not update 为什么 FlowLayout 中的左侧 TextField 不显示文本提示,而右侧显示文本提示? - Why is my left TextField in FlowLayout not showing text prompt but the right one does? 为什么我的文本字段没有出现? - Why is my text field not appearing?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM