【发布时间】: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