简体   繁体   中英

How to give the JLabel Positioning from top to bottom and left to right

hi i want to set Positioning of jlabel from top to bottom and left to right how can i do this i tried setlocation(),setsize() and also other method but not able to achieve my desired output.

Here is my code

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

Thanks in advance

First of all you need to set the layout then you apply the location. One example that should work is to set the layout to a Border layout then put the JPanel relative to center and if you don't want any other item on the frame it should cover the hole frame. Example code on frame class:

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

You can do this if you have no LayoutManager. You'll have to set your layoutmanager to null. Setting your position of your JComponent with setBounds and call repaint. For more information take a look at http://docs.oracle.com/javase/tutorial/uiswing/layout/none.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM