gpt4 book ai didi

java - 通过从文件选择器中选择图像来添加图像到数组中

转载 作者:行者123 更新时间:2023-12-01 22:51:32 25 4
gpt4 key购买 nike

在此程序中,我尝试使用文件选择器从用户中选择图像,然后显示这些图像。默认情况下我添加了 2 张图像。当我添加更多图像时,它不显示?

下面是我的整个代码

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import java.awt.FlowLayout;
import java.io.File;
import javax.swing.JDialog;
import javax.swing.JFileChooser;

public class CLayout {
JFrame frame = new JFrame("CardLayout demo");
JPanel panelCont = new JPanel();
LoginView log = new LoginView();
JPanel Img = new ImageGallery();
CardLayout cl = new CardLayout();

public CLayout() {
panelCont.setLayout(cl);
log.setLayout(new BoxLayout(log, BoxLayout.PAGE_AXIS));
Img.setLayout(new BoxLayout(Img, BoxLayout.PAGE_AXIS));
panelCont.add(log, "1");
panelCont.add(Img, "2");
cl.show(panelCont, "1");
ActionListener loginListener = new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
String userName = log.getUserName();
char[] password = log.getPassword();
String pass=new String(password);
if (LoginView.LOGIN.equals(e.getActionCommand())) {
if(userName.equals("imagegallery")&& pass.equals("12345"))
cl.show(panelCont, "2");
}
}
};
log.addActionListener(loginListener);
frame.add(panelCont);
frame.setSize(800,600);
frame.setTitle(" Image Gallery ");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new CLayout();
}
});
}

}
class ImageGallery extends JPanel {
private ImageIcon myImage1 = new ImageIcon ("Chrysanthemum.jpg");
private ImageIcon myImage2 = new ImageIcon ("Desert.jpg");

JPanel ImageGallery = new JPanel();
private ImageIcon[] myImages =new ImageIcon[10];
private int curImageIndex=0;
private int count=0;
private int total=1;
public ImageGallery () {
ImageGallery.add(new JLabel (myImage1));
myImages[0]=myImage1;
myImages[1]=myImage2;
add(ImageGallery, BorderLayout.CENTER);

JButton PREVIOUS = new JButton ("Previous");
JButton NEXT = new JButton ("Next");
JDialog.setDefaultLookAndFeelDecorated(true);

JButton button = new JButton("Select File");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JFileChooser fileChooser = new JFileChooser();
int returnValue = fileChooser.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
System.out.println(selectedFile.getName().toString());
ImageIcon tImage=new ImageIcon("selectedFile.getName().toString()");
System.out.println(myImages.length);
int n=2+count;
myImages[n]=tImage;
total=total+count+1;
count++;
System.out.println(total+" "+count);
}
}
});
JPanel Menu = new JPanel();
Menu.setLayout(new GridLayout(1,3));
add(Menu, BorderLayout.NORTH);
Menu.add(PREVIOUS);
Menu.add(NEXT);
Menu.add(button);

//register listener
PreviousButtonListener PreviousButton = new PreviousButtonListener ();
NextButtonListener NextButton = new NextButtonListener ();

//add listeners to corresponding componenets
PREVIOUS.addActionListener(PreviousButton);
NEXT.addActionListener(NextButton);
}


class PreviousButtonListener implements ActionListener {

public void actionPerformed(ActionEvent e) {
if(curImageIndex>0 && curImageIndex <=total) {
ImageGallery.remove(0);
curImageIndex=curImageIndex-1;
ImageIcon TheImage= myImages[curImageIndex];
ImageGallery.add(new JLabel (TheImage));
ImageGallery.validate();
ImageGallery.repaint();
} else {
ImageGallery.remove(0);
ImageGallery.add(new JLabel (myImage1));
curImageIndex=0;
ImageGallery.validate();
ImageGallery.repaint();
}
}
}


class NextButtonListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

if(curImageIndex>=0 && curImageIndex <total){
ImageGallery.remove(0);
curImageIndex = curImageIndex + 1;
ImageIcon TheImage= myImages[curImageIndex];
ImageGallery.add(new JLabel (TheImage));
ImageGallery.validate();
ImageGallery.repaint();
} else {
ImageGallery.remove(0);
ImageGallery.add(new JLabel (myImages[total]));
curImageIndex=total ;
ImageGallery.validate();
ImageGallery.repaint();
}
}
}
}
class LoginView extends JPanel {

JLabel userLabel = new JLabel("User");
JTextField userText = new JTextField(20);
JLabel passwordLabel = new JLabel("Password");
JPasswordField passwordText = new JPasswordField(20);
private final JButton loginButton;
private final JButton registerButton;

public static final String LOGIN = "Login";
public static final String REGISTER = "Regster";

public LoginView() {
setLayout(new GridLayout(0, 2));
userLabel.setBounds(10, 10, 80, 25);
add(userLabel);
userText.setBounds(10, 10, 60, 25);
add(userText);
passwordLabel.setBounds(10, 40, 80, 25);
add(passwordLabel);
passwordText.setBounds(100, 40, 160, 25);
add(passwordText);

loginButton = new JButton("login");
loginButton.setActionCommand(LOGIN);
registerButton = new JButton("register");
registerButton.setActionCommand(REGISTER);
add(loginButton);
}

public void addActionListener(ActionListener listener) {
loginButton.addActionListener(listener);
registerButton.addActionListener(listener);
}

public String getUserName() {
return userText.getText();
}

public char[] getPassword() {
return passwordText.getPassword();
}
}

最佳答案

问题出在这一行

ImageIcon tImage=new ImageIcon("selectedFile.getName().toString()");

您需要删除双引号才能读取实际的文件名。

ImageIcon tImage=new ImageIcon(selectedFile.getName().toString());

希望这有帮助。

关于java - 通过从文件选择器中选择图像来添加图像到数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24672154/

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