英文:
Formatting wiggle inspite of using String.format in Swing component
问题
我正在使用以下代码来在鼠标移动到 JFrame 上时,通过 JLabel 显示填充有空格的整数。考虑到这可能是标签的问题,我额外添加了一个 JFormattedTextField,但它也遇到了相同的问题:随着坐标从单个数字变化为多位数,文本会跳动。如何解决?
以下是一些示例代码:
package all.code.classes;
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
public class rough3 {
    static String toString(int a, int b, int c) {
        return String.format("w:%5d h:%5d n:%5d", a, b, c);
    }
    
    JFormattedTextField ftf = new JFormattedTextField();
    JLabel la = new JLabel();
    static int x, y;
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            rough3 xx = new rough3();
            xx.la.setHorizontalAlignment(SwingConstants.RIGHT);
            JFrame fr = new JFrame();
            fr.setLayout(new BorderLayout());
            fr.getContentPane().add(xx.ftf, BorderLayout.SOUTH);
            fr.getContentPane().add(xx.la, BorderLayout.NORTH);
            fr.setSize(500, 100);
            fr.setVisible(true);
            fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            fr.addMouseMotionListener(new MouseMotionAdapter() {
                @Override
                public void mouseMoved(MouseEvent e) {
                    super.mouseMoved(e);
                    x = e.getX();
                    y = e.getY();
                    xx.ftf.setText(xx.toString(x, y, 1));
                    xx.la.setText(xx.toString(x, y, 135));
                }
            });
        });
    }
}
英文:
I am using
String.format("w:%5d h:%5d n:%5d",a,b,c);
to show space padded ints in a JLabel as the mouse moves over a JFrame. Thinking it might be a label artifact, I added a JFormattedTextField for good measure but it suffers with  the same problem: the text dances around as the coords change from single to multi-digit. How to solve this?
Here's some example code
package all.code.classes;
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
public class rough3 {
    static String toString(int a,int b,int c)
    {
        return String.format("w:%5d h:%5d n:%5d",a,b,c);
    }
    JFormattedTextField ftf=new JFormattedTextField();
    JLabel la=new JLabel();
    static int x,y;
    public static void main(String[] args) {
        SwingUtilities.invokeLater(()-> {
            rough3 xx = new rough3();
            xx.la.setHorizontalAlignment(SwingConstants.RIGHT);
            JFrame fr = new JFrame();
            fr.setLayout(new BorderLayout());
            fr.getContentPane().add(xx.ftf, BorderLayout.SOUTH);
            fr.getContentPane().add(xx.la, BorderLayout.NORTH);
            fr.setSize(500, 100);
            fr.setVisible(true);
            fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            fr.addMouseMotionListener(new MouseMotionAdapter() {
                @Override
                public void mouseMoved(MouseEvent e) {
                    super.mouseMoved(e);
                    x = e.getX();
                    y = e.getY();
                    xx.ftf.setText(xx.toString(x,y,1));
                    xx.la.setText(xx.toString(x,y,135));
                }
            });
        });
    }
}
答案1
得分: 1
应该添加以下代码行。
xx.ftf.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
xx.la.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
英文:
You should add these lines.
xx.ftf.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
xx.la.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论