0

today i try to do a example of a "Window" on Java. I try to Concat the Title but my "GetTitle()" don't work! Anyone can help me with this?

And why "public class MiVentana extends JFrame {" and "MiVentana Frame = new MiVentana("Titulo");" says warning?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MiVentana extends JFrame {

public MiVentana (String Titulo){
    this.setTitle(Titulo);
    this.setSize(300,400);
    this.setLocation(160,80);
    this.setLayout(new FlowLayout());
    this.ConfigurarVentana();
    this.setVisible(true);
}

public void ConfigurarVentana(){
    JPanel panel = new JPanel();
    JButton boton = new JButton ("OK");
    boton.addActionListener(new EscuchadorBoton());
    panel.add(boton);
    this.add(panel);
}

class EscuchadorBoton implements ActionListener{
    public void actionPerformed(ActionEvent a){
        this.setTitle(this.getTitle().concat(this.getTitle()));
    }

}
public static void main(String[] args) {
    MiVentana Frame = new MiVentana("Titulo");
    //frameTest.setVisible(true);
            }
    }

EDIT: I'm working on Ubuntu 14.04 IDE Eclipse 3.8

Dash95
  • 3
  • 3

1 Answers1

1

Using this inside the ActionListener refers to the EscuchadorBoton listener, not the instance of MiVentana - your JFrame.

Using MiVentana.this should refer to the window, not the listener and you'll be able to get and set the title with that.

This post describes what is happening a bit better - basically you want this from the enclosing class, not the enclosed class.

Basically instead of doing this:

this.setTitle(this.getTitle().concat(this.getTitle()));

You need to do this:

MiVentana.this.setTitle(MiVentana.this.getTitle().concat(MiVentana.this.getTitle()));
Community
  • 1
  • 1
Will Richardson
  • 7,780
  • 7
  • 42
  • 56