我在这里缺少什么? (可能很明显)

时间:2014-01-23 13:25:41

标签: java swing jframe jpanel layout-manager

这个程序被用来创建电影列表。电影正在添加按钮添加按钮(JButton),还有Erease按钮女巫被禁止清除Jtextfield。代码编译但它只显示空白的JFrame。我错过了什么? (Java导入存在,但是它们会占用太多空间来粘贴它们)

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");
        }
    });
}



}

1 个答案:

答案 0 :(得分:5)

代码将组件添加到面板,但面板永远不会添加到框架中。

enter image description here

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

完整的,可编辑的来源。

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");
            }
        });
    }
}