0

我不明白为什么mediaPanel并且selectedMediaPanel没有显示在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);
    }

}
4

2 回答 2

2

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

框架布局管理器正在检查这些面板的首选尺寸,它们返回 0x0。因此,您的面板正在显示,它们的大小刚刚调整为 0x0

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

于 2013-03-07T20:22:15.467 回答
1

在 JPanel 构造函数中,您设置了一个空的 LayoutManager,那么组件将没有大小。

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

于 2013-03-07T20:28:32.620 回答