Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java BoxLayout中单个字符在Y轴上的对齐偏离中心_Java_Swing_Char_Jlabel_Boxlayout - Fatal编程技术网

Java BoxLayout中单个字符在Y轴上的对齐偏离中心

Java BoxLayout中单个字符在Y轴上的对齐偏离中心,java,swing,char,jlabel,boxlayout,Java,Swing,Char,Jlabel,Boxlayout,在Java中,将某些字符沿y轴对齐到BoxLayout的中心似乎存在问题。我不知道是什么原因导致了这种情况,我已经创建了一个SSCCE来演示这种效果。在这个例子中,我只使用了字符“a”&我在每个JPanel的正中间画了一条线来演示每个案例离中心有多远。粗体文本的大小写似乎排列得很好,但正常格式和斜体都严重偏离中心,尽管使用了setAlignmentX和setHorizontalAlignment。任何有助于理解这一效应的帮助都将不胜感激 如果问题出在我的特定计算机上的Java上,这是我运行SSC

在Java中,将某些字符沿y轴对齐到BoxLayout的中心似乎存在问题。我不知道是什么原因导致了这种情况,我已经创建了一个SSCCE来演示这种效果。在这个例子中,我只使用了字符“a”&我在每个JPanel的正中间画了一条线来演示每个案例离中心有多远。粗体文本的大小写似乎排列得很好,但正常格式和斜体都严重偏离中心,尽管使用了setAlignmentX和setHorizontalAlignment。任何有助于理解这一效应的帮助都将不胜感激

如果问题出在我的特定计算机上的Java上,这是我运行SSCCE时屏幕上显示的图像,它沿y轴加载三个不同的JPanel,其中包含BoxLayouts,并放置一个居中的JLabel,每个JLabel中仅包含字符“a”:

&以下是SSCCE的代码:

import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;

public class AlignmentTest extends JPanel
{
    public AlignmentTest(char label, int style)
    {
        JLabel l = new JLabel(Character.toString(label));
        setBorder(BorderFactory.createLineBorder(Color.BLACK,1));
        setBackground(Color.WHITE);
        setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
        setPreferredSize(new Dimension(300,50));
        add(Box.createVerticalGlue());
        add(l);
            l.setFont(l.getFont().deriveFont(style));
            l.setAlignmentX(CENTER_ALIGNMENT);
            l.setHorizontalAlignment(JLabel.CENTER);
        add(Box.createVerticalGlue());
    }
    public static void main(String[] args)
    {
        JFrame f = new JFrame("Alignment Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLayout(new GridLayout(1,0,5,5));
        f.add(new AlignmentTest('a',Font.PLAIN));
        f.add(new AlignmentTest('a',Font.BOLD));
        f.add(new AlignmentTest('a',Font.ITALIC));
        f.pack();
        f.setVisible(true);
    }
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.drawLine(getWidth()/2,0,getWidth()/2,getHeight());
    }
}

在Windows7上使用JDK7时,没有一个字符是中心对齐的

我做了一些更改来显示JTextField,并使用了JTextField的列(1、3、5)。随着柱数的增加,中心对齐的情况有所改善,在柱数5及以上的情况下是合理的。 所以问题在某种程度上与组件的宽度有关

我猜布局中有一些奇怪的舍入错误。这对我来说似乎是一个错误

如果您对提供与BoxLayout类似功能的布局感兴趣,可以查看。对示例的更改很小:

import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;

public class AlignmentTest extends JPanel
{
    public AlignmentTest(char label, int style)
    {
        JLabel l = new JLabel(Character.toString(label));
        setBorder(BorderFactory.createLineBorder(Color.BLACK,1));
        setBackground(Color.WHITE);
//        setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
        setLayout(new RelativeLayout(RelativeLayout.Y_AXIS));
        setPreferredSize(new Dimension(300,50));
//        add(Box.createVerticalGlue());
        add(Box.createVerticalGlue(), new Float(1));
        add(l);
            l.setFont(l.getFont().deriveFont(style));
            l.setAlignmentX(CENTER_ALIGNMENT);
            l.setHorizontalAlignment(JLabel.CENTER);
//        add(Box.createVerticalGlue());
        add(Box.createVerticalGlue(), new Float(1));
    }
    public static void main(String[] args)
    {
        JFrame f = new JFrame("Alignment Test");
        JScrollPane scroller = new JScrollPane();
            JPanel panel = new JPanel(new GridLayout(1,0,5,5));
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLayout(new GridLayout(1,0,5,5));
        f.add(new AlignmentTest('a',Font.PLAIN));
        f.add(new AlignmentTest('a',Font.BOLD));
        f.add(new AlignmentTest('a',Font.ITALIC));
        f.pack();
        f.setVisible(true);
    }
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.drawLine(getWidth()/2,0,getWidth()/2,getHeight());
    }
}

您观察到的效果似乎是这种工作方式的产物。从“当
BoxLayout
从左到右布局组件时,……任何额外的空间都会出现在容器的右侧。”插入。当封闭容器的初始大小是标签(固定)大小的小倍数时,如下图所示,异常最小;水平拉伸框架以查看其增长情况。一个解决办法是尽量减少人为扩大封闭容器的首选尺寸的程度

import javax.swing.*;
import java.awt.*;

public class AlignmentTest extends JPanel {
    private final JLabel l;
    public AlignmentTest(String label, int style) {
        setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
        setBackground(Color.WHITE);
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        l = new JLabel(label, JLabel.CENTER);
        l.setFont(l.getFont().deriveFont(style));
        l.setAlignmentX(CENTER_ALIGNMENT);
        l.setOpaque(true);
        l.setBackground(Color.cyan);
        add(Box.createVerticalGlue());
        add(l);
        add(Box.createVerticalGlue());
    }

    @Override
    public Dimension getPreferredSize() {
        int w = l.getPreferredSize().width;
        int h = l.getPreferredSize().height;
        return new Dimension(w * 3, h * 3);
    }

    public static void main(String[] args) {
        JFrame f = new JFrame("Alignment Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLayout(new GridLayout(1, 0, 5, 5));
        f.add(new AlignmentTest("aMa", Font.PLAIN));
        f.add(new AlignmentTest("aMa", Font.BOLD));
        f.add(new AlignmentTest("aMa", Font.ITALIC));
        f.pack();
        f.setVisible(true);
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawLine(getWidth() / 2, 0, getWidth() / 2, getHeight());
    }
}

另一种避免“框布局特性:…容器右侧出现任何额外空间”的方法是,您需要覆盖
JLabel\getMinimumSize()
方法,以返回与
JLabel\getPreferredSize()相同的
维度

对不起,我误解了

正如@camickr已经说过的

我猜布局中有一些奇怪的舍入错误。这对我来说似乎是一个错误

这是非常正确的

固定示例:

//MinimumSize checkbox
//selected true: set min width = 100px
//selected false: set min width = 7px(default "a" width)
//Here's my attempt(I am running JDK 1.7.0_72 on Windows 7):
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class AlignmentTest4 extends JPanel {
  private static boolean FLAG = false;
  @Override public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawLine(getWidth() / 2, 0, getWidth() / 2, getHeight());
  }
  @Override public Dimension getPreferredSize() {
    return new Dimension(300, 80);
  }
  public static JLabel makeLabel(String label, int style) {
    JLabel l = new JLabel(label) {
      @Override public Dimension getPreferredSize() {
        return new Dimension(120, 30);
      }
      @Override public Dimension getMinimumSize() {
        Dimension d = super.getMinimumSize();
        if (FLAG) {
          d.width = 100;
        } else {
          d.width = 7;
        }
        return d;
        //if (FLAG) {
        //  return this.getPreferredSize();
        //} else {
        //  return super.getMinimumSize();
        //}
      }
    };
    l.setOpaque(true);
    l.setBackground(Color.ORANGE);
    l.setFont(l.getFont().deriveFont(style));
    l.setAlignmentX(Component.CENTER_ALIGNMENT);
    l.setAlignmentY(Component.CENTER_ALIGNMENT);
    l.setVerticalAlignment(SwingConstants.CENTER);
    l.setVerticalTextPosition(SwingConstants.CENTER);
    l.setHorizontalAlignment(SwingConstants.CENTER);
    l.setHorizontalTextPosition(SwingConstants.CENTER);
    return l;
  }
  public static JComponent makePanel() {
    JPanel p = new JPanel(new GridLayout(0, 1, 5, 5));

    JPanel p1 = new AlignmentTest4();
    p1.setBorder(BorderFactory.createTitledBorder("BoxLayout.X_AXIS"));
    p1.setLayout(new BoxLayout(p1, BoxLayout.X_AXIS));
    p1.add(Box.createHorizontalGlue());
    p1.add(makeLabel("a", Font.PLAIN));
    p1.add(Box.createHorizontalGlue());

    JPanel p2 = new AlignmentTest4();
    p2.setBorder(BorderFactory.createTitledBorder("BoxLayout.Y_AXIS"));
    p2.setLayout(new BoxLayout(p2, BoxLayout.Y_AXIS));
    p2.add(Box.createVerticalGlue());
    p2.add(makeLabel("a", Font.PLAIN));
    p2.add(Box.createVerticalGlue());

    for (JPanel c : Arrays.asList(p1, p2)) {
      c.setBackground(Color.WHITE);
      p.add(c);
    }
    return p;
  }
  public static JComponent makeUI() {
    final JPanel p = new JPanel(new BorderLayout());
    p.add(makePanel());
    p.add(new JCheckBox(new AbstractAction("MinimumSize") {
      @Override public void actionPerformed(ActionEvent e) {
        FLAG = ((JCheckBox) e.getSource()).isSelected();
        SwingUtilities.updateComponentTreeUI(p);
      }
    }), BorderLayout.SOUTH);
    return p;
  }
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() {
        createAndShowGUI();
      }
    });
  }
  public static void createAndShowGUI() {
    JFrame f = new JFrame("Alignment Test");
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.getContentPane().add(makeUI());
    f.pack();
    f.setLocationRelativeTo(null);
    f.setVisible(true);
  }
}

为了清楚起见,请使用
样式
。很好,我没有想到这一点。我现在已将代码更改为使用
style
名称。谢谢你的意见,很有意思。这很好,但它给我带来了另一个问题:我不明白Java为什么要使用
JPanel
getMinimumSize()
方法来确定如何从
JLabel
开始居中。它不应该使用
JPanel
的当前大小,而不是最小大小吗?
JPanel
getMinimumSize()
方法在这种情况下是不相关的。“this”关键字指的是匿名内部类(
JLabel
)本身
@Override public Dimension getMinimumSize(){System.out.println(这个JLabel实例);/*ture*/返回这个.getPreferredSize();}
啊,好的。我想在这种情况下,修改后的问题是:为什么使用
JLabel
getMinimumSize()
方法?为了确定对中的测量值,我想应该使用当前尺寸,而不是最小尺寸。
RelativeLayout
绝对是一个足够的解决方法。我以前没有听说过它,但我可以在这里使用它&在未来的应用中。不过,为了使用它,我不得不上网并将
RelativeLayout
的源代码复制到我的项目中。我有Java版本1.7.0_25。我是否需要更新才能将
RelativeLayout
类内置到Java中,而无需手动复制源代码?@ScottHetrick,RelativeLayout只是我编写的一些(不受支持的)代码,用于提供链接中描述的功能。它没有jar文件或任何东西,所以可以像自己编写的任何类一样使用它。这个类不依赖于版本。哦,我明白了。真令人印象深刻。谢谢你的建议,我以后可能会使用它。这是一个可行的解决办法,但为了完美地“解决”这个问题,需要一个更直接的解决方案。