Naum ta rodando direito!

1 resposta
M

esse e o meu programa…eu ja consegui fazer o botao funcionar e a janela de achar aquivos tambem,…mas quando eu seleciono um arquivo jpg ele naum abre nenhuma figura…como eu faço pra aparecer.

import <a href="http://java.io">java.io</a>.<em>;

import java.awt.</em>;

import java.awt.event.<em>;

import javax.swing.</em>;
public class Janela extends JFrame {

private JLabel label;

private JButton openButton;

private ObjectInputStream input;

private RandomAccessFile output;

private Image imagem;

private String names[]={"*.jpg"};

private Icon icons[] = {new ImageIcon (names[0])};
public Janela()
{
	super ("Visualizador de Imagens");
	
	Container container = getContentPane();
	
	label = new JLabel ();
	getContentPane().add(
		label,BorderLayout.CENTER);

JPanel northPanel = new JPanel();
northPanel.setLayout(new GridLayout(3,1,0,5));
		
container.add(northPanel,BorderLayout.NORTH);		

openButton = new JButton("Abrir Arquivo");
//openButton = label.getDoTask1Button();
openButton.setText("Abrir Arquivo");

openButton.addActionListener(
	new ActionListener() {
		public void actionPerformed (ActionEvent event)
		{
			openFile();
		}
	}
);

addWindowListener (
	new  WindowAdapter() {
		public void windowClosing (WindowEvent event)
		{
			if(input!=null)
			closeFile();
			
			System.exit(0);
		}
	}
);

	pack();
	setSize(500,450);
	setVisible(true);
	show();
	northPanel.add(openButton);
	
}



private void openFile()
{
	JFileChooser fileChooser = new JFileChooser();
	fileChooser.setFileSelectionMode(
		JFileChooser.FILES_ONLY);
		
		int result = fileChooser.showOpenDialog(this);
		
		
		
		if (result == JFileChooser.CANCEL_OPTION)
		return;
		
		File fileName = fileChooser.getSelectedFile();
		
		label = new JLabel(icons[0]);
		//container.add(label);
		
		setSize(400,300);
		setVisible(true);
		
		
		if(fileName == null || fileName.getName().equals(""))
		JOptionPane.showMessageDialog(this,"Nome de Arquivo Invalido","Arquivo Invalido",JOptionPane.ERROR_MESSAGE);
		
		
		
		else{
			
			try{
				output = new RandomAccessFile (fileName,"jpg");
				
				input = new ObjectInputStream( new FileInputStream(fileName));
				
				openButton.setEnabled(true);
			}
			
			catch (IOException ioException) {
				JOptionPane.showMessageDialog(this,"Erro ao Abrir o Arquivo","Erro",JOptionPane.ERROR_MESSAGE);
			}
		}
	}
	
	


private void closeFile()
{
	try{
		input.close();
		System.exit(0);
	}
	catch(IOException ioException) {
		JOptionPane.showMessageDialog(this,"Error closing file","Error",JOptionPane.ERROR_MESSAGE);
		
		System.exit(1);
	}
}

	


	public static void main (String args[])
	{
		Janela application = new Janela();
		application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		application.setVisible(true);

	}

}

obrigado!!!

1 Resposta

P

Olah!!! :o)

Um dos erro está na seguinte linha:

Aqui precisa ser:
output = new RandomAccessRile(fileName, “r”);

Veja a documentação em http://java.sun.com/j2se/1.3/docs/api/index.html

abs :wink:

[i]RandomAccessFile
public RandomAccessFile(File file,
String mode)
throws FileNotFoundExceptionCreates a random access file stream to read from, and optionally to write to, the file specified by the File argument. A new FileDescriptor object is created to represent this file connection.
The mode argument must either be equal to “r” or “rw”, indicating that the file is to be opened for input only or for both input and output, respectively. The write methods on this object will always throw an IOException if the file is opened with a mode of “r”. If the mode is “rw” and the file does not exist, then an attempt is made to create it. An IOException is thrown if the file argument refers to a directory.

If there is a security manager, its checkRead method is called with the pathname of the file argument as its argument to see if read access to the file is allowed. If the mode is “rw”, the security manager’s checkWrite method is also called with the path argument to see if write access to the file is allowed.

Parameters:
file - the file object.
mode - the access mode.
Throws:
IllegalArgumentException - if the mode argument is not equal to “r” or to “rw”.
FileNotFoundException - if the file exists but is a directory rather than a regular file, or cannot be opened or created for any other reason
SecurityException - if a security manager exists and its checkRead method denies read access to the file or the mode is “rw” and the security manager’s checkWrite method denies write access to the file.
See Also:
File.getPath(), SecurityManager.checkRead(java.lang.String), SecurityManager.checkWrite(java.lang.String)
[/i]

Criado 7 de abril de 2005
Ultima resposta 7 de abr. de 2005
Respostas 1
Participantes 2