0

I have a problem with compiling. If I compile my code, it creates a GUI.class and GUI$x.class, wehere x stands for the amount of buttons I put in ( that means, if i have two buttons it creates a GUI$1.class and a GUI$2.class). The shown GUI is correct and works fine. Why does it create those $.class files and how can i correct it? Here is a code snippet:

public class GUI implements ActionListener,ListSelectionListener,FocusListener {
    static DefaultListModel<String> inputHistory;
    static DefaultListModel<String> resultHistory;
    static JTextField input;
    JList<String> listInput;
    JList<String> listResult;
    static JLabel status;
    JButton enter;
    JButton delete;
    JButton copy;
    JButton paste;
    static JButton numpad;
    boolean keyboardActivated = true;

    public GUI() {

        JFrame frame = new JFrame("calc");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setSize(705, 400);
        frame.getContentPane().setLayout(null);

        input = new JTextField();
        input.setBounds(10,10,560,50);
        input.setActionCommand("input");
        input.addActionListener(this);

        enter = new JButton("=");
        enter.setBounds(580,10,100,50);
        enter.addActionListener(this);  

        delete = new JButton("C");                                                  
        delete.setBounds(580,90,100,50);
        delete.addActionListener(new ActionListener(){  
            public void actionPerformed(ActionEvent e){  
                input.setText("");
            }
        });

        copy = new JButton("copy");
        copy.setBounds(580,150,100,50);
        copy.addActionListener(new ActionListener(){  
            public void actionPerformed(ActionEvent e){  
                Toolkit.getDefaultToolkit().getSystemClipboard().setContents (              
                           new StringSelection(input.getText()), null
                 );
            }
        });

        paste = new JButton("paste");
        paste.setBounds(580,210,100,50);
        paste.addActionListener(new ActionListener(){  
            public void actionPerformed(ActionEvent e){                                     
                Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
                DataFlavor flavor = DataFlavor.stringFlavor;
                if (clipboard.isDataFlavorAvailable(flavor)) {
                  try {
                    String text = (String) clipboard.getData(flavor);
                    input.setText(text);
                  } catch (UnsupportedFlavorException f) {
                    System.out.println(f);
                  } catch (IOException f) {
                    System.out.println(f);
                  }
                }
            }
        });

        JLabel label1 = new JLabel("input history");
        label1.setBounds(10, 70, 200, 20);
        inputHistory = new DefaultListModel<>();
        listInput = new JList<>(inputHistory);
        listInput.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        listInput.addListSelectionListener(this);
        listInput.addFocusListener(this);
        JScrollPane sp = new JScrollPane();
        sp.setViewportView(listInput);
        sp.setBounds(10,90,280,250);

        JLabel label2 = new JLabel("result history");
        label2.setBounds(290,70,280,20);
        resultHistory = new DefaultListModel<>();
        listResult = new JList<>(resultHistory);
        listResult.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        listResult.addListSelectionListener(this);
        listResult.addFocusListener(this);
        JScrollPane sp1 = new JScrollPane();
        sp1.setViewportView(listResult);
        sp1.setBounds(290,90,280,250);

        status = new JLabel("");
        status.setForeground(Color.red);
        status.setBounds(10,340,650,20);

        numpad = new JButton("Numpad");
        numpad.setBounds(580,290,100,50);
        numpad.addActionListener(new ActionListener(){  
            public void actionPerformed(ActionEvent e){  
                new Numpad();                                                   
                numpad.setEnabled(false);                                           
            }
        });

        frame.getContentPane().add(input);
        frame.getContentPane().add(enter);
        frame.getContentPane().add(label1);
        frame.getContentPane().add(label2);
        frame.getContentPane().add(sp);
        frame.getContentPane().add(sp1);
        frame.getContentPane().add(status);
        frame.getContentPane().add(numpad);
        frame.getContentPane().add(delete);
        frame.getContentPane().add(copy);
        frame.getContentPane().add(paste);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new GUI();
    }

    public void actionPerformed(ActionEvent e) {                                    
        String result = null;

        try {
            result = Postfix.eval(input.getText());
        } catch (Exception ex) {
            status.setText(ex.getMessage());
        }

        if (result != null) {                                                       
            inputHistory.insertElementAt(input.getText(), 0);                       
            resultHistory.insertElementAt(result , 0);                          
            input.setText(result);
            input.setCaretPosition(0);
            status.setText("");
        }
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Is this your whole code? It's not, right? – Sweeper Jun 07 '20 at 13:01
  • Please post the full code. There is not much in this one. Those classes are usually created when you add anonymous `ActionListeners` on each button, which is not the case in the code you showed. – Matthieu Jun 07 '20 at 13:01
  • I edited it, now it contains the full code. How can I make them not anonymous? – CodeLearner2020 Jun 07 '20 at 14:49
  • You can make them not anonymous by creating a separate class for every single `ActionListener`. You will have a lot more source files, the same number of class files, and have gained... nothing. – Robby Cornelissen Jun 08 '20 at 10:17
  • @Robby-Coernelissen Ok, then i will keep it as it is. Thank you! – CodeLearner2020 Jun 08 '20 at 12:15
  • Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Sep 19 '20 at 05:26

0 Answers0