Java 通过从文件选择器中选择图像,在数组中添加图像
在这个程序中,我尝试使用filechooser从用户中选择图像,然后显示这些图像。默认情况下,我添加了2个图像。当我添加更多图像时,它不会显示吗 下面是我的全部代码Java 通过从文件选择器中选择图像,在数组中添加图像,java,swing,Java,Swing,在这个程序中,我尝试使用filechooser从用户中选择图像,然后显示这些图像。默认情况下,我添加了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.*; im
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();
}
}
导入java.awt.CardLayout;
导入java.awt.Color;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.*;
导入java.awt.*;
导入java.awt.event.*;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.SwingUtilities;
导入javax.swing.JLabel;
导入javax.swing.JPasswordField;
导入javax.swing.JTextField;
导入java.awt.FlowLayout;
导入java.io.File;
导入javax.swing.JDialog;
导入javax.swing.JFileChooser;
公开课{
JFrame=新JFrame(“CardLayout演示”);
JPanel panelCont=新的JPanel();
LoginView日志=新建LoginView();
JPanel Img=新图像库();
CardLayout cl=新的CardLayout();
公共CLayout(){
面板控制设置布局(cl);
log.setLayout(新的BoxLayout(log,BoxLayout.PAGE_轴));
Img.setLayout(新的BoxLayout(Img,BoxLayout.PAGE_轴));
面板续加(记录,“1”);
面板续加(Img,“2”);
cl.show(面板续,“1”);
ActionListener loginListener=新建ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
字符串userName=log.getUserName();
char[]password=log.getPassword();
字符串传递=新字符串(密码);
if(LoginView.LOGIN.equals(e.getActionCommand())){
if(userName.equals(“imagegallery”)&pass.equals(“12345”))
cl.show(面板续,“2”);
}
}
};
log.addActionListener(loginListener);
框架。添加(面板续);
框架。设置尺寸(800600);
frame.setTitle(“图像库”);
frame.setLocationRelativeTo(空);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
公共静态void main(字符串[]args){
SwingUtilities.invokeLater(新的Runnable(){
@凌驾
公开募捐{
新的CLayout();
}
});
}
}
类ImageGallery扩展了JPanel{
private ImageIcon myImage1=新的ImageIcon(“jummy.jpg”);
私有ImageIcon myImage2=新的ImageIcon(“Desert.jpg”);
JPanel ImageGallery=新的JPanel();
私有图像图标[]myImages=新图像图标[10];
私有整数索引=0;
私有整数计数=0;
私人整数合计=1;
公共图像库(){
添加(新JLabel(myImage1));
myImages[0]=myImage1;
myImages[1]=myImage2;
添加(ImageGallery,BorderLayout.CENTER);
JButton-PREVIOUS=新JButton(“PREVIOUS”);
JButton NEXT=新JButton(“NEXT”);
JDialog.setDefaultLookAndFeelDecorated(true);
JButton按钮=新JButton(“选择文件”);
addActionListener(新建ActionListener()){
已执行的公共无效行动(行动事件ae){
JFileChooser fileChooser=新的JFileChooser();
int returnValue=fileChooser.showOpenDialog(null);
if(returnValue==JFileChooser.APPROVE\u选项){
File selectedFile=fileChooser.getSelectedFile();
System.out.println(selectedFile.getName().toString());
ImageIcon tImage=新的ImageIcon(“selectedFile.getName().toString()”;
System.out.println(myImages.length);
int n=2+计数;
myImages[n]=tImage;
总计=总计+计数+1;
计数++;
系统输出打印项次(总数+“”+计数);
}
}
});
JPanel菜单=新建JPanel();
设置布局(新的网格布局(1,3));
添加(菜单,BorderLayout.NORTH);
菜单。添加(上一个);
菜单。添加(下一步);
菜单。添加(按钮);
//寄存器侦听器
PreviousButtonListener PreviousButton=新的PreviousButtonListener();
NextButtonListener NextButton=新的NextButtonListener();
//将侦听器添加到相应的组件集中
PREVIOUS.addActionListener(PreviousButton);
NEXT.addActionListener(NextButton);
}
类PreviousButtonListener实现ActionListener{
已执行的公共无效操作(操作事件e){
如果(curImageIndex>0&&curImageIndex=0&&curImageIndex问题在这一行
ImageIcon tImage=new ImageIcon("selectedFile.getName().toString()");
您需要删除双引号才能读取实际的文件名
ImageIcon tImage=new ImageIcon(selectedFile.getName().toString());
希望这有帮助。问题就在这一行
ImageIcon tImage=new ImageIcon("selectedFile.getName().toString()");
您需要删除双引号才能读取实际的文件名
ImageIcon tImage=new ImageIcon(selectedFile.getName().toString());
希望这有帮助。问题就在这一行
ImageIcon tImage=new ImageIcon("selectedFile.getName().toString()");
您需要删除双引号才能读取实际的文件名
ImageIcon tImage=new ImageIcon(selectedFile.getName().toString());
希望这有帮助。问题就在这一行
ImageIcon tImage=new ImageIcon("selectedFile.getName().toString()");
您需要删除双引号才能读取实际的文件名
ImageIcon tImage=new ImageIcon(selectedFile.getName().toString());
希望这有帮助。你听说了吗?听说了吗?听说了吗?听说了吗?@user3363800如果有助于解决您的问题,请告诉我。很高兴这对您有所帮助。祝您编码愉快:)请别忘了接受@peeskilletI thinkselectedFile.getAbsolutePath()提到的答案
更有用。我怀疑selectedFile.getName()
只会返回文件名,而不会返回完整的文件名path@user3363800如果这有助于解决您的问题,请回答。很高兴这对您有所帮助。祝您编码愉快:)并且请不要忘记接受@peeskilletI thinkselectedFile.getAbsolutePath()提到的答案
更有用。我怀疑selectedFile.getName()
只会返回文件名,而不会返回完整的文件名path@user3363800如果这有助于解决你的问题,请告诉我。很高兴这对你有帮助。祝你玩得开心