Erro interface = minimizando quando clico no botão

0 respostas
L

Olá pessoal, sou novo no forúm e se vcs poderem me ajudar agradeceria muito. A situação é o seguinte tenho esse codigo que é uma classe extends Jframe, é toda vez que vou clicar no botão abrir para abrir uma imagem, ele minimiza a aplicação do nada, embora quando eu maximize a mesma a imagem ja esteja carregada e a classe esta toda certa, queria que ela não tivesse esse bug. de ante mão obrigado !

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;




public class Interface extends JFrame {
	
	

	private JButton BtnAbrir1;
	private JButton BtnAbrir2;
	private JButton BtnAplicar;
	
	
	private JPanel panel1;
	private JPanel panel2;
	private JPanel panel3;
	private JPanel panel4;
	private JPanel panel5;
	
	private ImagePGM imgPGM1;
	private ImagePanel imageA;
	private ImagePGM imgPGM2;
	private ImagePanel imageB;
	private ImagePGM imgPGM3;
	private ImagePanel imageResultado;
	
	private JLabel labelOperacoes;
	
	private TitledBorder borda1;
	private TitledBorder borda2;
	private TitledBorder borda3;
	
	private JRadioButton opSoma;
	private JRadioButton opSubt;
	private JRadioButton opMulti;
	private JRadioButton opDivi;
	private JRadioButton opOr;
	private JRadioButton opAnd;
	private JRadioButton opXor;
	
	private ButtonGroup bGOperacoes;
	
	
	public static int a;
	
	public Interface() {
		initComponents();
	}
	
	private void initComponents() {
	
		setLayout(null);
		
	BtnAbrir1 = new JButton("Abrir");
	BtnAbrir2 = new JButton("Abrir");
	BtnAplicar = new JButton("Aplicar");
	
	labelOperacoes = new JLabel("Operações:       ");
	
	
	opSoma = new JRadioButton("Soma");
	opSubt = new JRadioButton("Subtração");
	opMulti = new JRadioButton("Multiplicação");
	opDivi= new JRadioButton("Divisão");
	opOr = new JRadioButton("Or");
	opAnd = new JRadioButton("And");
	opXor = new JRadioButton("Xor");
		
	bGOperacoes = new ButtonGroup();
	
	borda1 = new TitledBorder("");
	borda2 = new TitledBorder("");
	borda3 = new TitledBorder("");
	
	panel1 = new JPanel();
	panel2 = new JPanel();
	panel3 = new JPanel();
	panel4 = new JPanel();
	panel5 = new JPanel();
	
	panel1.setBounds(120,10,400,38);
	panel1.setBorder(BorderFactory.createLineBorder(Color.blue));
	panel5.setBounds(550,10,250,38);
	panel5.setBorder(BorderFactory.createLineBorder(Color.green));
	
	setBounds(0,0 ,860,390);
	BtnAbrir1.setBounds(95,320,70,25);
	add(BtnAbrir1);
	BtnAbrir1.setActionCommand("Abrir");
	BtnAbrir1.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent evt) {
			butCarregaAActionPerformed(evt);
		}

			private void butCarregaAActionPerformed(ActionEvent evt) {

		JFileChooser fc = new JFileChooser();
		int returnVal = fc.showOpenDialog(Interface.this);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			try {
				File file = fc.getSelectedFile();
				// This is where a real application would open the file.
				String filepath = file.getAbsolutePath();
				imgPGM1 = new ImagePGM(filepath);
				imageA = new ImagePanel(imgPGM1);
				panel2.removeAll();
				panel2.add(imageA);
				pack();

			} catch (Exception ex) {
				Logger.getLogger(Interface.class.getName()).log(
						Level.SEVERE, null, ex);
			}
		}
	}

	
	});
	

  
	BtnAbrir2.setBounds(390,320,70,25);
	add(BtnAbrir2);
	BtnAbrir2.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent evt) {
			butCarregaBActionPerformed(evt);
		}

			private void butCarregaBActionPerformed(ActionEvent evt) {

		JFileChooser fc = new JFileChooser();
		

		int returnVal = fc.showOpenDialog(Interface.this);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			try {
				File file = fc.getSelectedFile();
				// This is where a real application would open the file.
				String filepath = file.getAbsolutePath();
				imgPGM2 = new ImagePGM(filepath);
				imageB = new ImagePanel(imgPGM2);
				panel3.removeAll();
				panel3.add(imageB);
				pack();
				initComponents();
			} catch (Exception ex) {
				Logger.getLogger(Interface.class.getName()).log(
						Level.SEVERE, null, ex);
			}
		}

	}
	});
	BtnAplicar.setBounds(680,320,75,25);
	add(BtnAplicar);
	BtnAplicar.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent evt) {
			butAplicaActionPerformed(evt);
		}

			private void butAplicaActionPerformed(ActionEvent evt) {
		// TODO add your handling code here:

		if ((imgPGM1 != null) && (imgPGM2 != null)) {
			if (a == 0) {
				// Operador de soma selecionado
				try {
					ImageProcess imageProcess = new ImageProcess();
					imgPGM3 = imageProcess.addImage(imgPGM1, imgPGM2);
					imageResultado = new ImagePanel(imgPGM3);
					panel4.removeAll();
					panel4.add(imageResultado);
					pack();
					
				} catch (Exception ex) {
					Logger.getLogger(Interface.class.getName()).log(
							Level.SEVERE, null, ex);
				}
			}

			if (a == 1) {
				// Operador de subtração selecionado
				try {
					ImageProcess imageProcess = new ImageProcess();
					imgPGM3 = imageProcess.subImage(imgPGM1, imgPGM2);
					imageResultado = new ImagePanel(imgPGM3);
					panel4.removeAll();
					panel4.add(imageResultado);
					pack();
				} catch (Exception ex) {
					Logger.getLogger(Interface.class.getName()).log(
							Level.SEVERE, null, ex);
				}
			}

			if (a == 2) {
				// Operador de multiplicação selecionado
				try {
					ImageProcess imageProcess = new ImageProcess();
					imgPGM3 = imageProcess.mulImage(imgPGM1, imgPGM2);
					imageResultado = new ImagePanel(imgPGM3);
					panel4.removeAll();
					panel4.add(imageResultado);
					pack();
				} catch (Exception ex) {
					Logger.getLogger(Interface.class.getName()).log(
							Level.SEVERE, null, ex);
				}
			}

			if (a == 3) {
				// Operador de divisão selecionado
				try {
					ImageProcess imageProcess = new ImageProcess();
					imgPGM3 = imageProcess.divImage(imgPGM1, imgPGM2);
					imageResultado = new ImagePanel(imgPGM3);
					panel4.removeAll();
					panel4.add(imageResultado);
					pack();
				} catch (Exception ex) {
					Logger.getLogger(Interface.class.getName()).log(
							Level.SEVERE, null, ex);
				}
			}

			if (a == 4) {
				// Operador OR selecionado
				try {
					ImageProcess imageProcess = new ImageProcess();
					imgPGM3 = imageProcess.orImage(imgPGM1, imgPGM2);
					imageResultado = new ImagePanel(imgPGM3);
					panel4.removeAll();
					panel4.add(imageResultado);
					pack();
				} catch (Exception ex) {
					Logger.getLogger(Interface.class.getName()).log(
							Level.SEVERE, null, ex);
				}
			}

			if (a == 5) {
				// Operador AND selecionado
				try {
					ImageProcess imageProcess = new ImageProcess();
					imgPGM3 = imageProcess.andImage(imgPGM1, imgPGM2);
					imageResultado = new ImagePanel(imgPGM3);
					panel4.removeAll();
					panel4.add(imageResultado);
					pack();
				} catch (Exception ex) {
					Logger.getLogger(Interface.class.getName()).log(
							Level.SEVERE, null, ex);
				}
			}

			if (a == 6) {
				// Operador Xor selecionado
				try {
					ImageProcess imageProcess = new ImageProcess();
					imgPGM3 = imageProcess.xorImage(imgPGM1, imgPGM2);
					imageResultado = new ImagePanel(imgPGM3);
					panel4.removeAll();
					panel4.add(imageResultado);
					pack();
				} catch (Exception ex) {
					Logger.getLogger(Interface.class.getName()).log(
							Level.SEVERE, null, ex);
				}
			}

		} else {
			JOptionPane.showMessageDialog(null, "Selecione duas imagens");
		}
	}
	});
	
	labelOperacoes.setBounds(20,20,90,20);
	add(labelOperacoes);

	panel2.setBounds(5,60,256,256);
	panel2.setBorder(borda1);
	
	
	panel3.setBounds(300,60,256,256);
	panel3.setBorder(borda2);
	
	
	panel4.setBounds(590,60,256,256);
	panel4.setBorder(borda3);
	
	
	
	bGOperacoes.add(opSoma);
	panel1.add(opSoma);
	
	
	bGOperacoes.add(opSubt);
	panel1.add(opSubt);
	
	
	bGOperacoes.add(opMulti);
	panel1.add(opMulti);
	

	bGOperacoes.add(opDivi);
	panel1.add(opDivi);
	
	
	bGOperacoes.add(opOr);
	panel5.add(opOr);
	
	
	bGOperacoes.add(opAnd);
	panel5.add(opAnd);
	
	
	bGOperacoes.add(opXor);
	panel5.add(opXor);
	
	
	
	
	
	opSoma.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			a = 0;
			JOptionPane.showMessageDialog(null, "Operador da Soma ativado");
		}
	});
	opSubt.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			a = 1;
			JOptionPane.showMessageDialog(null,
					"Operador da Subtração ativado");
		}
	});
	opMulti.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			a = 2;
			JOptionPane.showMessageDialog(null,
					"Operador da Multiplicação ativado");
		}
	});
	opDivi.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			a = 3;
			JOptionPane.showMessageDialog(null,
					"Operador da Divisão ativado");
		}
	});
	opOr.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			a = 4;
			JOptionPane.showMessageDialog(null,
					"Operador Lógico OR ativado");
		}
	});
	opAnd.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			a = 5;
			JOptionPane.showMessageDialog(null,
					"Operador Lógico AND ativado");
		}
	});
	opXor.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			a = 6;
			JOptionPane.showMessageDialog(null,
					"Operador Lógico XOR ativado");
		}
	});
	
	
	panel2.setLayout(new BorderLayout());
	panel3.setLayout(new BorderLayout());
	panel4.setLayout(new BorderLayout());
	
	add(panel1);
	add(panel2);
	add(panel3);
	add(panel4);
	add(panel5);
	setBounds(0,0 ,860,390);
	setTitle("Processamento de Imagens = Operações Lógicas e Aritiméticas");
	setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	setVisible(true);
	
	
	
	
	}
	public static void main(String args[]) {
		  Interface t1 = new Interface();
		    t1.setVisible(true);
			}
	
}
Criado 28 de novembro de 2010
Respostas 0
Participantes 1