Jframe中没有显示两个Jpanel

时间:2013-03-07 19:59:38

标签: java swing jframe jpanel

我不明白为什么mediaPanelselectedMediaPanel未显示在JFrame中。是什么原因?

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.List;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel;


public class MediaSelectionGUI {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        new MediaSelectionGUI();

    }

    public MediaSelectionGUI(){

        final Situation situation = new Situation();

        JFrame guiFrame = new JFrame();

        //make sure the program exits when the frame closes
        guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        guiFrame.setTitle("Communications Support Tool version 0.1");
        guiFrame.setSize(1000,500);
        guiFrame.setLayout(new FlowLayout());


      //This will center the JFrame in the middle of the screen
        guiFrame.setLocationRelativeTo(null);

        Vector<Media> media_option = new Vector<Media>(Arrays.asList(new Media[]{Media.Chat,Media.E_mail,Media.Intranet,Media.Meeting,Media.Social_Media, Media.Telefon, Media.Video}));
        Vector<Media> selected_media_option = new Vector<Media>();

        Zweck[] zweck_option = {Zweck.Anweisung, Zweck.Austausch, Zweck.Informieren, Zweck.Koordination};
        Ort[] ort_option = {Ort.Gleicher_Ort, Ort.Unterschiedlicher_Ort, Ort.Andere_Zeitzone};
        Anzahl[] anzahl_option = {Anzahl.Zwei_Personen, Anzahl.Klein_Gruppe, Anzahl.Publikum};

      //Create the second JPanel. Add a JLabel and JList and
        final JPanel mediaPanel = new JPanel(null);
        JLabel mediaLabel = new JLabel("All Media:");
        final JList<Media> media = new JList<Media>(media_option);
        media.setLayoutOrientation(JList.HORIZONTAL_WRAP);
        media.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

        mediaPanel.add(mediaLabel);
        mediaPanel.add(media);
        mediaPanel.setVisible(true);

        final JPanel selectedMediaPanel = new JPanel(null);
        JLabel selectedmediaLabel = new JLabel("Selected Media:");
        final JList<Media> selected_media = new JList<Media>(selected_media_option);
        selected_media.setLayoutOrientation(JList.HORIZONTAL_WRAP);
        selected_media.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

        selectedMediaPanel.add(selectedmediaLabel);
        selectedMediaPanel.add(selected_media);
        selectedMediaPanel.setVisible(true);

        JButton select_button = new JButton("select");
        JButton deselect_button = new JButton("deselect");

        select_button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                // TODO Auto-generated method stub
                List<Media> selected_media_option = media.getSelectedValuesList();
                for (Media medium: selected_media_option){
                    selected_media.getSelectedValuesList().add(medium);
                }
            }
        });


        deselect_button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                // TODO Auto-generated method stub
                List<Media> selected_media_option = selected_media.getSelectedValuesList();
                for (Media medium: selected_media_option){
                    media.getSelectedValuesList().add(medium);
                }
            }
        });

        select_button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                // TODO Auto-generated method stub
                List<Media> selected_media_option = media.getSelectedValuesList();
                for (Media medium: selected_media_option){
                    situation.media_collection.add(medium);
                    System.out.println(medium);
                }
            }
        });


      // Zweck Panel
        final JPanel zweckPanel = new JPanel();
        JLabel zweckLabel = new JLabel("Zweck:");
        JComboBox<Zweck> zweck = new JComboBox<Zweck>(zweck_option);
        zweckPanel.add(zweckLabel);
        zweckPanel.add(zweck);

     // Ort Panel
        final JPanel ortPanel = new JPanel();
        JLabel ortLabel = new JLabel("Ort:");
        JComboBox<Ort> ort = new JComboBox<Ort>(ort_option);    
        ortPanel.add(ortLabel);
        ortPanel.add(ort);

     // Anzahl Panel
        final JPanel anzahlPanel = new JPanel();
        JLabel anzahlLabel = new JLabel("Anzahl:");
        JComboBox<Anzahl> anzahl = new JComboBox<Anzahl>(anzahl_option);
        ortPanel.add(anzahlLabel);
        ortPanel.add(anzahl);


        guiFrame.add(mediaPanel);
        guiFrame.add(select_button);
        guiFrame.add(deselect_button);
        guiFrame.add(selectedMediaPanel);
        guiFrame.add(zweckPanel);
        guiFrame.add(ortPanel);
        guiFrame.add(anzahlPanel);

        guiFrame.setVisible(true);
    }

}

2 个答案:

答案 0 :(得分:2)

您遇到问题的主要原因是您在使用布局管理器的框架中添加了两个带有空布局的面板。

帧布局管理器正在检查这些面板的首选大小,它们返回0x0。所以你的面板显示,他们只是大小为0x0

尝试为面板使用适当的布局管理器

答案 1 :(得分:1)

在JPanel构造函数中,您设置了一个null LayoutManager,然后组件将没有大小。

只需从JPanel构造函数中删除null。