gpt4 book ai didi

java - 创建 java swing 专辑

转载 作者:行者123 更新时间:2023-12-01 11:26:39 28 4
gpt4 key购买 nike

我想创建一个 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();
}
});
}
}

相册框架:

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

菜单面板

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

照片面板

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) {

}
}

最佳答案

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

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

保存必要的数据并初始化监听器。然后可以将类添加到您的 PhotoPanel 中:

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

关于java - 创建 java swing 专辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30761177/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com