Java app application

Asked

Viewed 68 times

1

This code when pressed on button definicoes, opens a new window with an image, a line with text and also generates radio Buttons, but the problem is that it creates another new window with radio Buttons and also doubles the JFrame that contains all the buttons, and I don’t want to duplicate the JFrame. What I wanted was to show the image, the text and the radio Buttons all in the same new window. Is there any way to do that? Keeping the JFrame initial that has this button definicoes and also has other buttons, etc.

 if (definicoesBtn == e.getSource()) // botao definicoes premido
 {
     window = new JFrame(" Jogo da Memoria - Definicoes:");
     window.setLayout(new BorderLayout());  

     JLabel label3 = new JLabel(" Definicoes: ");           
     window.add("Center", label3);
     window.resize(300,300);
     window.show();                         
     ImageIcon img = new ImageIcon("definicoes.jpg");
     int altura = img.getIconHeight();  
     int largura = img.getIconWidth(); 
     JLabel label = new JLabel(img);    
     window.add(label, BorderLayout.NORTH);

     MemoryGame formulario1=new MemoryGame(); // cria os radiobutton
     formulario1.setBounds(0,0,300,200);
     formulario1.setVisible(true);

     window.pack();  
     window.setVisible(true); 
 }

My main class is this one on the first lines of code:

public class MemoryGame extends JFrame implements ActionListener, ChangeListener
{
    private JCheckBox check1,check2,check3; // declaracao dos checkbutoes
    //...  
    private JFrame window = new JFrame("Jogo da Memoria");
    // Jframe com todos os botoes janela inicial declarei em MemoryGame()

    public MemoryGame() {  
        setLayout(null);
        check1=new JCheckBox("Inglés");
        check1.setBounds(10,10,150,30);
        check1.addChangeListener(this);
        add(check1);
        check2=new JCheckBox("Francés");
        check2.setBounds(10,50,150,30);
        check2.addChangeListener(this);        
        add(check2);
        check3=new JCheckBox("Alemán");
        check3.setBounds(10,90,150,30);
        check3.addChangeListener(this);        
        add(check3);

And also the function:

 public void stateChanged(ChangeEvent e){
    String cad="";
    if (check1.isSelected()==true) {
        cad=cad+"Inglés-";
    }
    if (check2.isSelected()==true) {
        cad=cad+"Francés-";
    }
    if (check3.isSelected()==true) {
        cad=cad+"Alemán-";
    }
    setTitle(cad);
}

Something must be wrong or incomplete, so I need help to solve or fix the code.

  • 1

    Post the full class code, including the Imports. Your error is easy to see, you are instantiating new JFrameInstead of using the same one. But in order to fix your problem, I’m going to need more things in the class to know what to put in place of creating a new window. At least one compileable class that shows the problem would help enough.

  • I’ve solved the problem.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.