简体   繁体   English

如何在JFrame中的特定区域设置JLabel?

[英]How to set JLabel in a specific area in JFrame?

在此输入图像描述

I am trying to make a desktop application in Java Swing. 我正在尝试在Java Swing中创建一个桌面应用程序。 I am trying to create image slider in frame and I got it. 我试图在框架中创建图像滑块,我得到它。 Now problem in that I want to set the specific area for imagelabel in that frame. 现在的问题是我想在该帧中设置imagelabel的特定区域。 How can I do this? 我怎样才能做到这一点? I want to set imagelabel in left side. 我想在左侧设置imagelabel I am posting my snapshot which I am getting after running my program. 我正在发布我在运行程序后得到的快照。

Here is my code 这是我的代码

我在这里变得像这样

public class ImageSlider extends JPanel implements ActionListener {

    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.BLUE);

    private List<String> list = new ArrayList<String>(MAX);
    private List<ImageIcon> cache = new ArrayList<ImageIcon>(MAX);
    private JLabel imageLabel = new JLabel();
     //label = new JLabel( image, SwingConstants.CENTER);
    private JButton prevButton = new JButton();
    private JButton nextButton = new JButton();
    private JComboBox favorites;

    public ImageSlider() {
        this.setLayout(new BorderLayout());

        list.add("c.jpg");
        list.add("a0.png");
        list.add("yellow.png");
         list.add("a0.png");
         list.add("c.jpg");

        for (int i = 0; i < list.size(); i++) cache.add(i, null);
 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.setIcon(getImage(0));
        imageLabel.setAlignmentX(LEFT_ALIGNMENT);
        imageLabel.setHorizontalAlignment(JLabel.CENTER);
        imageLabel.setBorder(border);
        this.add(imageLabel, BorderLayout.CENTER);

        favorites = new JComboBox(
            list.toArray(new String[list.size()]));
        favorites.setActionCommand("favs");
        favorites.addActionListener(this);

        prevButton.setText("\u22b2Prev");
        prevButton.setFont(sans);
        prevButton.setActionCommand("prev");
        prevButton.addActionListener(this);

        nextButton.setText("Next\u22b3");
        nextButton.setFont(sans);
        nextButton.setActionCommand("next");
        nextButton.addActionListener(this);

        JPanel controlPanel = new JPanel();
        controlPanel.add(prevButton);
        controlPanel.add(favorites);
        controlPanel.add(nextButton);
        controlPanel.setBorder(border);
        this.add(controlPanel, BorderLayout.SOUTH);
    }

    public void actionPerformed(ActionEvent ae) {
        String cmd = ae.getActionCommand();
        if ("favs".equals(cmd)) {
            int index = favorites.getSelectedIndex();
            ImageIcon image = getImage(index);
            imageLabel.setIcon(image);
            if (image != null) imageLabel.setText("");
            else imageLabel.setText("Image not available.");
        }
        if ("prev".equals(cmd)) {
            int index = favorites.getSelectedIndex() - 1;
            if (index < 0) index = list.size() - 1;
            favorites.setSelectedIndex(index);
        }
        if ("next".equals(cmd)) {
            int index = favorites.getSelectedIndex() + 1;
            if (index > list.size() - 1) index = 0;
            favorites.setSelectedIndex(index);
        }
    }

    public JButton getDefault() { return nextButton; }

    // Return the (possibly cached) image having the given index.
    private ImageIcon getImage(int index) {
        ImageIcon image = cache.get(index);
        if (image != null) return image;
        String name = "/images/" + list.get(index);
        URL url = ImageSlider.class.getResource(name);
        if (url != null) {
            image = new ImageIcon(url);
        }
        cache.set(index, image);
        return image;
    }

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

How can I achieve my goal? 我怎样才能实现目标?

The easiest way to achieve this is to put the imageLabel into a JPanel with a FlowLayout . 实现此目的的最简单方法是将imageLabel放入带有FlowLayoutJPanel Then add that panel to the bigger BorderLayout . 然后将该面板添加到更大的BorderLayout

So change: 所以改变:

this.add(imageLabel, BorderLayout.CENTER);

To something like: 对于这样的事情:

JPanel imageConstrain = new JPanel(new FlowLayout(SwingConstants.LEFT));  
imageConstrain.add(imageLabel);
this.add(imageConstrain, BorderLayout.CENTER);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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