[英]How to give the JLabel Positioning from top to bottom and left to right
您好,我想設置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);
}
});
}
}
提前致謝
首先,您需要設置布局,然后應用位置。 一個有效的示例是將布局設置為Border布局,然后將JPanel相對於中心放置,如果您不希望框架上有任何其他項目,則它應該覆蓋孔框架。 框架類的示例代碼:
this.setLayout(new BorderLayout());
this.add(new JPanel(), BorderLayout.Center);
如果沒有LayoutManager,則可以執行此操作。 您必須將layoutmanager設置為null。 使用setBounds設置JComponent的位置,然后調用repaint。 有關更多信息,請訪問http://docs.oracle.com/javase/tutorial/uiswing/layout/none.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.