9
I have noticed that when the java compiler compiles some class that has some swing/awt component, it creates another or even several classes of the same name, with a $
followed by a numbering.
I made a simple code to check if it was netbeans or if it really was the compiler and when compiling the code below:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Tela {
public Tela(){
final JFrame frame = new JFrame("Tela 1");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel("teste");
JButton button = new JButton("botao");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
new DialogTela(frame, true);
}
});
frame.setPreferredSize(new Dimension(175, 100));
JPanel p = new JPanel(new FlowLayout());
p.add(label);
p.add(button);
frame.getContentPane().add(p);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Tela();
}
});
}
class DialogTela{
public DialogTela(Frame f, boolean modal){
JDialog dialog = new JDialog(f,modal);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
JLabel label = new JLabel("teste");
dialog.setPreferredSize(new Dimension(175, 100));
JPanel p = new JPanel(new FlowLayout());
p.add(label);
dialog.getContentPane().add(p);
dialog.pack();
dialog.setVisible(true);
}
}
}
Result after compiling:
Until I understand he created the Tela$DialogTela.class
, I believe because it is an inner class, but I don’t understand why the compiler created the Tela$1.class
and the Tela$2.class
, beyond the very Tela.class
.
For consciousness-raising, I created another class called Teste
, where I just make a call to a JOptionPane
(which is also from the swing package):
import javax.swing.JOptionPane;
public class Teste{
public static void main(String[] args) {
JOptionPane.showMessageDialog(null, "teste", "Teste", JOptionPane.INFORMATION_MESSAGE);
}
}
and was generated only the Teste.class
.
What are those repeat classes that the compiler created from the same class for? It’s some particularity of the swing API?
So the answer to the question was already in it :D I was so disappointed with myself when I read your answer that I did a test based on the answer, using Thread, without swing and without anything, and the result was exactly this.
– user28595
@diegofm, at the time you were commenting I edited my answer exactly saying that this is Java in general. Pure coincidence. : -) Do not be disappointed, your question is very well written and will serve many people.
– cantoni