尽管在Swing组件中使用了String.format,仍然出现格式问题。

huangapple go评论61阅读模式
英文:

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));

huangapple
  • 本文由 发表于 2020年7月26日 13:23:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63096390.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定