JLayeredPanel内的Jpanels

时间:2015-06-01 22:32:26

标签: java swing jpanel jfilechooser

我正在使用JLayeredPanel将两个面板放在一起。第一个面板就像一个背景,第二个面板使用JFileChooser来获取用户的图像在面板上打印,然后该面板将点击按钮放置在第一个面板的顶部。

我成功地将图像打印到JFileChooser的第二个面板上,但无法将第二个面板放在第一个面板的顶部。

这是大纲。 http://s8.postimg.org/ofblrk179/outline.png

这是我的代码。 ...

    public class DesignerUI extends JFrame {
    public DesignerUI() {


   public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                DesignerUI frame = new DesignerUI();
                frame.setVisible(true);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}
    card2 = new JPanel();
    cards.add(card2, "Design");
    card2.setLayout(null);

    JButton btnGraphic = new JButton("Add Graphic");
    btnGraphic.setBounds(0, 0, 400, 53);
    panel.add(btnGraphic);
    btnGraphic.addActionListener(new chooseGraphic());

    JLayeredPane lp = new JLayeredPane();
    lp.setPreferredSize(new Dimension(300,400));
    lp.setBounds(400,0,382,406);
    card2.add(lp);

    panel_1 = new JPanel();
    panel_1.setBounds(0, 0, 383, 406);

    picPanel = new JPanel();
    picPanel.setBounds(105, 115, 213, 200);

    lp.add(picPanel, new Integer(2)); 
    lp.add(panel_1, new Integer(3)); 

private class chooseGraphic implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        JFrame graphicInputWindow = new JFrame();
        new graphicInputWindow();
    }
}

public class graphicInputWindow extends JFrame {
    JButton button;
    JButton select;
    JLabel label;
    JPanel picPanel;
    Image image;

    public graphicInputWindow() {
        super("Select Your Image");
        setResizable(false);
        button = new JButton("Browse");
        button.setBounds(300, 300, 100, 40);
        select = new JButton("Select");
        select.setBounds(400, 300, 100, 40);
        picPanel = new JPanel();
        label = new JLabel();

        label.setBounds(10, 10, 670, 250);
        add(button);
        add(select);
        add(picPanel);
        add(label);

        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                JFileChooser file = new JFileChooser();
                file.setCurrentDirectory(new File(System
                        .getProperty("user.home")));

                // filter the files
                FileNameExtensionFilter filter = new FileNameExtensionFilter(
                        "*.Images", "jpg", "gif", "png");
                file.addChoosableFileFilter(filter);
                int result = file.showSaveDialog(null);

                // if the user click on save
                if (result == JFileChooser.APPROVE_OPTION) {
                    File selectedFile = file.getSelectedFile();
                    String path = selectedFile.getAbsolutePath();
                    label.setIcon(ResizeImage(path));

                }

                // if the user click on cancel
                else if (result == JFileChooser.CANCEL_OPTION) {
                    System.out.println("No File Select");

                }
            }
        });

        select.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                // if user click select
                JPanel picPanel = new JPanel(new BorderLayout());
                picPanel.add(label);
                lp.add(picPanel);
                lp.setVisible(true);
                //close window returns to card2
                setVisible(false);

            }
        });

        setSize(700, 400);
        setVisible(true);
        revalidate();
        contentPane.revalidate();
        contentPane.repaint();
    }

    public ImageIcon ResizeImage(String ImagePath) {
        ImageIcon MyImage = new ImageIcon(ImagePath);
        Image img = MyImage.getImage();
        Image newImg = img.getScaledInstance(panel_1.getWidth(),
                panel_1.getHeight(), Image.SCALE_SMOOTH);
        ImageIcon image = new ImageIcon(newImg);
        return image;
    }

    /*protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.drawImage(image, 0, 0, getWidth(), getHeight(), this);
    }*/
}
}

0 个答案:

没有答案