JLabel宽度不起作用

时间:2015-03-27 22:04:59

标签: java fonts jpanel width jlabel

我已将JLabel的宽度设置为字符串的宽度,但是当我这样做时,JLabel会使用...来切断中间的字符串,所以不要“问我一个问题!”,它最终成为“问我一个问题......”。这可能是我正在使用的字体的问题,但我不确定。有什么帮助吗?

这是我的自定义JLabel类,名为BLANKJLabel:

package BLANK.BLANK.menufeatures;

import BLANK.BLANK.main.QnABotJPanel;

import javax.swing.*;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.AffineTransform;

public class BLANKJLabel extends JLabel {

    public AffineTransform affineTransform = new AffineTransform();
    public FontRenderContext fontRenderContext = new FontRenderContext(affineTransform, true, true);

    public BLANKJLabel(ImageIcon icon, String text, int x, int y) {
        int stringWidth = (int) (QnABotJPanel.font.getStringBounds(text, fontRenderContext).getWidth());
        int stringHeight = (int) (QnABotJPanel.font.getStringBounds(text, fontRenderContext).getHeight());

        this.setText(text);
        this.setFont(BLANKJPanel.font);
        this.setLocation(x, y);
        this.setIcon(icon);
        this.setSize(stringWidth, stringHeight);
    }

    public BLANKLabel(String text, int x, int y) {
        int stringWidth = (int) (QnABotJPanel.font.getStringBounds(text, fontRenderContext).getWidth());
        int stringHeight = (int) (QnABotJPanel.font.getStringBounds(text, fontRenderContext).getHeight());

        Dimension d = new Dimension(this.getPreferredSize());

        this.setText(text);
        this.setFont(BLANKJLabel.font);
        this.setLocation(x, y);
        this.setSize(d);
    }

}

这是使用它的类,BLANKJPanel:

package BLANK.BLANK.main;

import BLANK.BLANK.menufeatures.BLANKButton;
import BLANK.BLANK.menufeatures.BLANKJLabel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.font.FontRenderContext;
import java.awt.geom.AffineTransform;

public class QnABotJPanel extends JPanel {

    public AffineTransform affineTransform = new AffineTransform();
    public FontRenderContext fontRenderContext = new FontRenderContext(affineTransform, true, true);

    static QnABotButton submitButton;
    static QnABotJLabel askMeAQuestionLabel;

    public static Font font = new Font("Avenir Next", Font.PLAIN, 20);

    String askMeAQuestion = "Ask me a question!";

    int askMeAQuestionWidth = (int)(font.getStringBounds(askMeAQuestion, fontRenderContext).getWidth());
    int askMeAQuestionHeight = (int)(font.getStringBounds(askMeAQuestion, fontRenderContext).getHeight());

    public QnABotJPanel() {
        setLayout(new GroupLayout(this));

        submitButton = new QnABotButton("Submit", "/submit", QnABotWindow.windowWidth / 2 - 100, (QnABotWindow.windowHeight / 4) * 3 - 50);

        askMeAQuestionLabel = new QnABotJLabel(askMeAQuestion, QnABotWindow.windowWidth / 4 - askMeAQuestionWidth / 2, QnABotWindow.windowHeight / 3 - askMeAQuestionHeight / 2);

        this.add(submitButton);
        this.add(askMeAQuestionLabel);

        this.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (QnABotJPanel.submitButton.getActionCommand().equalsIgnoreCase(e.getActionCommand())) {

                }
            }

        });
    }

    public void addActionListener(ActionListener listener) {
        submitButton.addActionListener(listener);
    }

}

3 个答案:

答案 0 :(得分:1)

我给你的第一条建议是避免使用null布局,像素完美布局是现代ui设计中的一种幻觉。影响组件个体大小的因素太多,您无法控制。 Swing旨在与布局管理器一起工作,放弃这些将导致问题和问题的结束,您将花费越来越多的时间来纠正。

您可能遇到的下一个问题是尝试执行标签已经能够执行的操作,计算它的首选大小。

如果您在设置文本和字体后使用JLabel#getPreferredSize,它将告诉您组件的大小应该是什么。这是布局管理器API默认执行的操作

Without layout manager

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new QnABotJPanel());
                frame.setSize(200, 50);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public static class QnABotJPanel extends JPanel {

//      public AffineTransform affineTransform = new AffineTransform();
//      public FontRenderContext fontRenderContext = new FontRenderContext(affineTransform, true, true);

//      static QnABotButton submitButton;
//      static QnABotJLabel askMeAQuestionLabel;

        public static Font font = new Font("Avenir Next", Font.PLAIN, 20);

//      String askMeAQuestion = "Ask me a question!";
//
//      int askMeAQuestionWidth = (int) (font.getStringBounds(askMeAQuestion, fontRenderContext).getWidth());
//      int askMeAQuestionHeight = (int) (font.getStringBounds(askMeAQuestion, fontRenderContext).getHeight());

        public QnABotJPanel() {
            setLayout(null);

            add(new BLANKJLabel("This is a banana", 0, 0));

//          submitButton = new QnABotButton("Submit", "/submit", QnABotWindow.windowWidth / 2 - 100, (QnABotWindow.windowHeight / 4) * 3 - 50);
//
//          askMeAQuestionLabel = new QnABotJLabel(askMeAQuestion, QnABotWindow.windowWidth / 4 - askMeAQuestionWidth / 2, QnABotWindow.windowHeight / 3 - askMeAQuestionHeight / 2);
//
//          this.add(submitButton);
//          this.add(askMeAQuestionLabel);
//
//          this.addActionListener(new ActionListener() {
//
//              @Override
//              public void actionPerformed(ActionEvent e) {
//                  if (QnABotJPanel.submitButton.getActionCommand().equalsIgnoreCase(e.getActionCommand())) {
//
//                  }
//              }
//
//          });
        }

        public void addActionListener(ActionListener listener) {
//          submitButton.addActionListener(listener);
        }

    }

    public static class BLANKJLabel extends JLabel {

//      public AffineTransform affineTransform = new AffineTransform();
//      public FontRenderContext fontRenderContext = new FontRenderContext(affineTransform, true, true);
        public BLANKJLabel(ImageIcon icon, String text, int x, int y) {
//          int stringWidth = (int) (QnABotJPanel.font.getStringBounds(text, fontRenderContext).getWidth());
//          int stringHeight = (int) (QnABotJPanel.font.getStringBounds(text, fontRenderContext).getHeight());

            this.setText(text);
            this.setFont(QnABotJPanel.font);
            this.setLocation(x, y);
            this.setIcon(icon);
            this.setSize(getPreferredSize());
        }

        public BLANKJLabel(String text, int x, int y) {
//          int stringWidth = (int) (QnABotJPanel.font.getStringBounds(text, fontRenderContext).getWidth());
//          int stringHeight = (int) (QnABotJPanel.font.getStringBounds(text, fontRenderContext).getHeight());

            this.setText(text);
            this.setFont(QnABotJPanel.font);
            this.setLocation(x, y);
            this.setSize(getPreferredSize());
        }

    }
}

并使用布局管理API ......

With layout manager

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new QnABotJPanel());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public static class QnABotJPanel extends JPanel {

//      public AffineTransform affineTransform = new AffineTransform();
//      public FontRenderContext fontRenderContext = new FontRenderContext(affineTransform, true, true);

//      static QnABotButton submitButton;
//      static QnABotJLabel askMeAQuestionLabel;

        public static Font font = new Font("Avenir Next", Font.PLAIN, 20);

//      String askMeAQuestion = "Ask me a question!";
//
//      int askMeAQuestionWidth = (int) (font.getStringBounds(askMeAQuestion, fontRenderContext).getWidth());
//      int askMeAQuestionHeight = (int) (font.getStringBounds(askMeAQuestion, fontRenderContext).getHeight());

        public QnABotJPanel() {
            setLayout(new GridBagLayout());

            add(new BLANKJLabel("This is a banana"));

//          submitButton = new QnABotButton("Submit", "/submit", QnABotWindow.windowWidth / 2 - 100, (QnABotWindow.windowHeight / 4) * 3 - 50);
//
//          askMeAQuestionLabel = new QnABotJLabel(askMeAQuestion, QnABotWindow.windowWidth / 4 - askMeAQuestionWidth / 2, QnABotWindow.windowHeight / 3 - askMeAQuestionHeight / 2);
//
//          this.add(submitButton);
//          this.add(askMeAQuestionLabel);
//
//          this.addActionListener(new ActionListener() {
//
//              @Override
//              public void actionPerformed(ActionEvent e) {
//                  if (QnABotJPanel.submitButton.getActionCommand().equalsIgnoreCase(e.getActionCommand())) {
//
//                  }
//              }
//
//          });
        }

        public void addActionListener(ActionListener listener) {
//          submitButton.addActionListener(listener);
        }

    }

    public static class BLANKJLabel extends JLabel {

//      public AffineTransform affineTransform = new AffineTransform();
//      public FontRenderContext fontRenderContext = new FontRenderContext(affineTransform, true, true);
        public BLANKJLabel(ImageIcon icon, String text) {
//          int stringWidth = (int) (QnABotJPanel.font.getStringBounds(text, fontRenderContext).getWidth());
//          int stringHeight = (int) (QnABotJPanel.font.getStringBounds(text, fontRenderContext).getHeight());

            this.setText(text);
            this.setFont(QnABotJPanel.font);
            this.setIcon(icon);
        }

        public BLANKJLabel(String text) {
//          int stringWidth = (int) (QnABotJPanel.font.getStringBounds(text, fontRenderContext).getWidth());
//          int stringHeight = (int) (QnABotJPanel.font.getStringBounds(text, fontRenderContext).getHeight());

            this.setText(text);
            this.setFont(QnABotJPanel.font);
        }

    }
}

答案 1 :(得分:0)

以下是一种可以操作JLabel的方法(在本例中,它应用于JPopupMenu)

JLabel labelPop = new JLabel("Options de la table", JLabel.CENTER); //  JLabel with centered text
labelPop.setMaximumSize(new Dimension(10000, 0));                   // Determine the width
labelPop.setPreferredSize(new Dimension(0, 25));                    // Determine the height
labelPop.setOpaque(true);                                           // Set transparency 
labelPop.setBackground(new Color(0, 0, 51));                        // Apply the background color
labelPop.setForeground(Color.decode("#FFFFFF"));                    // Apply the text color

答案 2 :(得分:-1)

通过添加常量来增加stringWidth的大小。