有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java JLabel宽度不工作

我已经将JLable的宽度设置为字符串的宽度,但是当我这样做时,JLabp使用……因此,与其说“问我一个问题!”,不如说,结果是“问我一个问题……”。这可能是我使用的字体有问题,但我不确定。有什么帮助吗

下面是我的自定义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) 个答案

  1. # 1 楼答案

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

  2. # 2 楼答案

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

    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
    
  3. # 3 楼答案

    我给你的第一条建议是避免使用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);
            }
    
        }
    }