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

时间:2014-03-10 08:10:00

标签: java swing jlist imageicon

enter image description here

我正在尝试在Java Swing中创建一个桌面应用程序。我试图在框架中创建图像滑块,我得到它。现在问题在于我想在该帧中设置imagelabel的特定区域。我怎样才能做到这一点?我想在左侧设置imagelabel。我正在发布我在运行程序后得到的快照。

这是我的代码

here i  am getting like this

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

我如何实现目标?

1 个答案:

答案 0 :(得分:2)

实现此目标的最简单方法是将imageLabel放入JPanel FlowLayout。然后将该面板添加到较大的BorderLayout

所以改变:

this.add(imageLabel, BorderLayout.CENTER);

类似于:

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