【问题标题】:How to give the JLabel Positioning from top to bottom and left to right如何给 JLabel 定位从上到下和从左到右
【发布时间】:2014-04-13 15:04:06
【问题描述】:

嗨,我想从上到下和从左到右设置 jlabel 的定位,我该怎么做 我尝试了 setlocation(),setsize() 和其他方法,但无法达到我想要的输出。

这是我的代码

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

public class TesstDemo1 extends JPanel {

    private static final int MAX = 20;
    private static final Font sans = new Font("SansSerif", Font.PLAIN, 16);
    private static final Border border =
            BorderFactory.createMatteBorder(4, 16, 4, 16, Color.black);
    private JLabel imageLabel = new JLabel();

    public TesstDemo1() {
        this.setLayout(new BorderLayout());
        ImageIcon image = new ImageIcon("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\a0.png");
        JLabel titleLabel = new JLabel(image, SwingConstants.CENTER);
        // titleLabel.setText("ImageSlider");
        titleLabel.setHorizontalAlignment(JLabel.CENTER);
        titleLabel.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 24));
        //   titleLabel.setBorder(border);
        this.add(titleLabel, BorderLayout.NORTH);
        imageLabel.setPreferredSize(new Dimension(800, 600));
        imageLabel.setBackground(Color.BLUE);
        imageLabel.setOpaque(true);
        imageLabel.setAlignmentX(LEFT_ALIGNMENT);
        JPanel imageConstrain = new JPanel(new FlowLayout(SwingConstants.LEFT));
        imageConstrain.add(imageLabel);
        imageLabel.setHorizontalAlignment(JLabel.LEFT);
        imageLabel.setBorder(border);
        this.add(imageLabel, BorderLayout.CENTER);
        // this.add(imageConstrain, BorderLayout.CENTER);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                TesstDemo1 go = new TesstDemo1();
                frame.add(go);
                frame.setTitle("ImageSlider");
                //  frame.setSize(400, 300);
                frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
                frame.setUndecorated(true);
                frame.setVisible(true);
            }
        });
    }
}

提前致谢

【问题讨论】:

  • 你在用什么layout?如果你显示你的代码会更好。
  • 对不起,我没有明白你的意思
  • 你可以并排使用两个网格布局。
  • 是的,我正在使用流布局和边框
  • @anonymous...我怎么用你能给我演示一下吗

标签: java swing alignment jlabel imageicon


【解决方案1】:

首先您需要设置布局,然后应用位置。应该工作的一个示例是将布局设置为边框布局,然后将 JPanel 相对于中心放置,如果您不希望框架上的任何其他项目,它应该覆盖孔框架。框架类的示例代码:

this.setLayout(new BorderLayout());
this.add(new JPanel(), BorderLayout.Center);

【讨论】:

    【解决方案2】:

    如果您没有 LayoutManager,您可以这样做。您必须将您的布局管理器设置为空。使用 setBounds 设置 JComponent 的位置并调用 repaint。欲了解更多信息,请查看http://docs.oracle.com/javase/tutorial/uiswing/layout/none.html

    【讨论】:

    • 我不想使用空布局
    • 那么如果你想拥有实际的绝对位置就没有别的办法了。如果您尝试添加不可见的组件,则可以解决。但 BorderLayout 可以忽略您喜欢的 Dimension。
    猜你喜欢
    • 2010-12-02
    • 1970-01-01
    • 2017-05-08
    • 2012-07-24
    • 2015-02-12
    • 2014-12-31
    • 1970-01-01
    • 1970-01-01
    • 2019-10-22
    相关资源
    最近更新 更多