创建一个java swing专辑

时间:2015-06-10 15:45:15

标签: java image swing arraylist jbutton

我想创建一个Java Swing相册,但我无法找到正确的方法。我认为应该创建两个ArrayList,一个用于存储照片对象,另一个用于存储按钮。

之后我应该找到一种方法将每个图像分配给按钮并将它们添加到面板中。

我的问题是:你认为这是正确的方法,如果是这样,你能给我一个暗示吗? (对于底部的最后一节课)

此处是我的代码:

主要:

import javax.swing.JFrame;

public class Main extends JFrame {
public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
           new AlbumFrame();
        }
    });
  }
}

AlbumFrame:

import java.awt.BorderLayout;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class AlbumFrame extends JFrame {

    private JPanel MenuPanel;
    private JPanel PhotoPanel;

    public AlbumFrame(){
        super("JPhone");

        setLayout(new BorderLayout());

        PhotoPanel = new PhotoPanel();
        add(PhotoPanel,BorderLayout.CENTER);

        MenuPanel = new MenuPanel();
        add(MenuPanel,BorderLayout.SOUTH);

        setSize(480,800);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }       
}

MenuPanel

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.filechooser.FileNameExtensionFilter;


public class MenuPanel extends JPanel implements ActionListener{

    private static final long serialVersionUID = 1L;
    private JButton backButton;
    private JButton homeButton;
    private JButton turnButton;
    private JButton addButton;

    final private JFileChooser fc;

    public MenuPanel(){

        fc = new JFileChooser();

        backButton = new JButton(new ImageIcon(getClass().getResource("/Images/back.png")));
        homeButton = new JButton(new ImageIcon(getClass().getResource("/Images/home.png")));
        turnButton = new JButton(new ImageIcon(getClass().getResource("/Images/turn.png")));
        addButton = new JButton(new ImageIcon(getClass().getResource("/Images/add.png")));

        backButton.setPreferredSize(new Dimension(55,55));
        homeButton.setPreferredSize(new Dimension(55,55));
        turnButton.setPreferredSize(new Dimension(55,55));
        addButton.setPreferredSize(new Dimension(55,55));

        backButton.addActionListener(this);
        homeButton.addActionListener(this);
        turnButton.addActionListener(this);
        addButton.addActionListener(this);

        setLayout(new FlowLayout(FlowLayout.CENTER));

        add(backButton);
        add(homeButton);
        add(turnButton);
        add(addButton);
    }

    public void actionPerformed(ActionEvent e) {
        JButton clicked = (JButton)e.getSource();

        //Test for the moment
        if(clicked == backButton){
            System.out.println("back");
        }else if(clicked == homeButton){
            System.out.println("home");
        }else if(clicked == turnButton){
            System.out.println("turn");
        }else if(clicked == addButton){
            int returnVal = fc.showOpenDialog(MenuPanel.this);
            if(returnVal == JFileChooser.APPROVE_OPTION){
                File file = fc.getSelectedFile();
            }
        }
    }
}

PhotoPanel

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JPanel;

public class PhotoPanel extends JPanel implements ActionListener {

    ArrayList<Photo> Album = new ArrayList<Photo>();
    ArrayList<JButton> Buttons = new ArrayList<JButton>();

    public PhotoPanel(){

        setLayout(new FlowLayout(FlowLayout.CENTER));

    }

    public void actionPerformed(ActionEvent e) {

    }
}

1 个答案:

答案 0 :(得分:1)

我会使用单独的类PhotoCard来避免列表:

class PhotoCard {
    public PhotoCard(Photo photo) {
        add(photo);
        // also add buttons, listeners, etc.
    }
}

保存必要的数据并初始化侦听器。 然后可以将课程添加到您的PhotoPanel

PhotoPanel.add(new PhotoCard(...));