简体   繁体   中英

What i am missing here? ( probably something obvious)

this program is supposted to create list of movies. Movies are beeing add by tha Add button (JButton) , there is also Erease button witch is supposted to clear Jtextfield. Code compiles but it shows nothing but blank JFrame. What i am missing? (Java imports are there, but they would take too much space to paste them)

public class SamodzielnaListaOsob extends JFrame  implements ActionListener {

JButton add, erease;
JTextField film;
DefaultListModel<String> listFilm;

public SamodzielnaListaOsob(String title){
    super(title);

        setDefaultCloseOperation(EXIT_ON_CLOSE);


        final JTextField film = new JTextField("Tutaj wpisz tytul filmu", 10);
        film.setBorder(BorderFactory.createTitledBorder(null, "Film"));

        JPanel p1 = new JPanel();
            p1.add(film);


        JButton add = new JButton("Dodaj do listy");
            add.addActionListener(new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent e) {
                            String nowyFilm = film.getText();
                                if (nowyFilm !=""){
                                    listFilm.addElement(nowyFilm);
                                    film.setText("");
                                }
                }
            });
            JButton erease = new JButton("Wyczysc pole");
                erease.addActionListener(new ActionListener(){
                    @Override
                    public void actionPerformed(ActionEvent e) {
                         film.setText("");

                    }
                });

                JPanel p2 = new JPanel();
                p2.add(add);
                p2.add(erease);

                listFilm = new DefaultListModel<String>();
                listFilm.addElement("Film0");
                listFilm.addElement("Film1");
                listFilm.addElement("Film2");





             setPreferredSize(new Dimension(900, 900));      
             pack();
             setVisible(true);
}




public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new SamodzielnaListaOsob("List of movies");
        }
    });
}



}

The code adds components to panels, but the panels are never added to the frame.

在此处输入图片说明

 //setPreferredSize(new Dimension(900, 900));
 p1.add(p2);
 setContentPane(p1);

The full, compilable source.

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

public class SamodzielnaListaOsob extends JFrame {

    JButton add, erease;
    JTextField film;
    DefaultListModel<String> listFilm;

    public SamodzielnaListaOsob(String title){
        super(title);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        final JTextField film = new JTextField("Tutaj wpisz tytul filmu", 10);
        film.setBorder(BorderFactory.createTitledBorder(null, "Film"));

        JPanel p1 = new JPanel();
        p1.add(film);
        JButton add = new JButton("Dodaj do listy");
        JButton erease = new JButton("Wyczysc pole");

        JPanel p2 = new JPanel();
        p2.add(add);
        p2.add(erease);

        listFilm = new DefaultListModel<String>();
        listFilm.addElement("Film0");
        listFilm.addElement("Film1");
        listFilm.addElement("Film2");

        //setPreferredSize(new Dimension(900, 900));
        p1.add(p2);
        setContentPane(p1);
        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new SamodzielnaListaOsob("List of movies");
            }
        });
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM