简体   繁体   English

Jframe中未显示两个Jpanel

[英]Two Jpanels are not showing in the Jframe

I don't understand why mediaPanel and selectedMediaPanel are not shown in the JFrame . 我不明白为什么mediaPanelselectedMediaPanel没有显示在JFrame What is the reason? 是什么原因?

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

}

The main reason your having problems is your adding two panels with null layouts to the frame that is using a layout manager. 您遇到问题的主要原因是使用布局管理器向框架中添加了两个具有空布局的面板。

The frames layout manager is checking these panels preferred sizes and they are returning 0x0. 框架布局管理器正在检查这些面板的首选大小,并且它们返回0x0。 So you panels are showing, they've just be sized to 0x0 所以您的面板正在显示,它们的大小仅为0x0

Try using appropriate layout managers for the panels as well 也尝试为面板使用适当的布局管理器

In the JPanel constructor, you are setting a null LayoutManager, then the components will be without size. 在JPanel构造函数中,要设置一个空LayoutManager,则组件将没有大小。

Just remove the null from JPanel constructor. 只需从JPanel构造函数中删除null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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