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 a netbeans thing or if it was really the compiler and 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 compile:
I'mnotsurewhythecompilercreatedtheTela$DialogTela.class
andtheTela$1.class
,inadditiontotheTela$2.class
itself.
Forthesakeofconscience,IcreatedanotherclasscalledTela.class
,whereIonlymakeacalltoaTeste
(whichalsobelongstotheswingpackage):
importjavax.swing.JOptionPane;publicclassTeste{publicstaticvoidmain(String[]args){JOptionPane.showMessageDialog(null,"teste", "Teste", JOptionPane.INFORMATION_MESSAGE);
}
}
and only JOptionPane
was generated.
What would be those repeated classes that the compiler created from the same class? Is there any particularity of the swing API?