简体   繁体   English

JList不会出现在JPanel内部

[英]JList does not appear inside JPanel

I have made some changes to a program I am working on. 我对正在开发的程序进行了一些更改。 Upon great suggestion, I have changed a JTextArea within a JScrollPanel to a JList. 根据很好的建议,我将JScrollPanel中的JTextArea更改为JList。 For some reason, even though I have coded the contents of the JList, the area appears blank and about 1 row high. 出于某种原因,即使我已经编码了JList的内容,该区域仍显示为空白并且高约1行。 I have the default size for each JList set to (200, 200). 我将每个JList的默认大小设置为(200,200)。 Any ideas? 有任何想法吗?

Code: 码:

package SwingAssign2;

import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;

import java.awt.BorderLayout;

import javax.swing.JComboBox;
import javax.swing.JTextField;

import java.awt.FlowLayout;

import javax.swing.SwingConstants;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JList;
import javax.swing.AbstractListModel;

public class SwingAssign2 {

    private JFrame frame;
    private JTextField textField;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    SwingAssign2 window = new SwingAssign2();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public SwingAssign2() {
        initialize();
        frame.pack();
    }

    /**
     * Initialize the contents of the frame.
     */
    @SuppressWarnings("unchecked")
    private void initialize() {
        frame = new JFrame();
        frame.setSize(600, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panelTop = new JPanel();
        FlowLayout fl_panelTop = (FlowLayout) panelTop.getLayout();
        fl_panelTop.setAlignment(FlowLayout.LEFT);
        frame.getContentPane().add(panelTop, BorderLayout.NORTH);
        //panel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));

        final JComboBox comboBox = new JComboBox();
        comboBox.setModel(new DefaultComboBoxModel(new String[] {"Select Something", "Selection 1", "Selection 2", "Selection 3", "Selection 4"}));
        panelTop.add(comboBox);
        comboBox.setPreferredSize(new Dimension(160, 20));

        textField = new JTextField();
        panelTop.add(textField);
        textField.setColumns(40);
        textField.setBorder(BorderFactory.createLoweredBevelBorder());

        JPanel panelBottom = new JPanel();
        frame.getContentPane().add(panelBottom, BorderLayout.SOUTH);
        panelBottom.setLayout(new BorderLayout(0, 0));

        final JLabel status = new JLabel("Status");
        status.setHorizontalAlignment(SwingConstants.CENTER);
        panelBottom.add(status, BorderLayout.SOUTH);
        status.setBorder(BorderFactory.createLoweredBevelBorder());

        JScrollPane scrollPaneB = new JScrollPane();  
        scrollPaneB.setSize(200, 200);
        String[] listB = { "Some content on the right panel", "More content", "Some more content", "More and more content", "More and more content", "More and more content",
                "More and more content", "More and more content", "More and more content", "More and more content", "More and more content", "More and more content", "More and more content", "More and more content", 
                "More and more content" }; 
        JList listBottom = new JList(listB);
        listBottom.setVisibleRowCount(12);
        scrollPaneB = new JScrollPane(listBottom);
        panelBottom.add(scrollPaneB);
        scrollPaneB.add(listBottom);
        scrollPaneB.setBorder(BorderFactory.createTitledBorder("Bottom Panel"));
        scrollPaneB.setVisible(true);
        listBottom.setVisible(true);

        JPanel panelCenter = new JPanel();
        frame.getContentPane().add(panelCenter, BorderLayout.CENTER);
        panelCenter.setLayout(new GridLayout(0, 2, 0, 0));

        JScrollPane scrollPaneL = new JScrollPane();  
        scrollPaneL.setSize(200, 200);
        String[] listL = { "Some content on the right panel", "More content", "Some more content", "More and more content", "More and more content", "More and more content",
                "More and more content", "More and more content", "More and more content", "More and more content", "More and more content", "More and more content", "More and more content", "More and more content", 
                "More and more content" }; 
        JList listLeft = new JList(listL);
        listLeft.setVisibleRowCount(12);
        scrollPaneL = new JScrollPane(listLeft);
        panelCenter.add(scrollPaneL);
        scrollPaneL.add(listLeft);
        scrollPaneL.setBorder(BorderFactory.createTitledBorder("Top Left Panel"));
        scrollPaneL.setVisible(true);
        listLeft.setVisible(true);

        JScrollPane scrollPaneR = new JScrollPane();  
        scrollPaneR.setSize(200, 200);
        String[] listR = { "Some content on the right panel", "More content", "Some more content", "More and more content", "More and more content", "More and more content",
                "More and more content", "More and more content", "More and more content", "More and more content", "More and more content", "More and more content", "More and more content", "More and more content", 
                "More and more content" }; 
        JList listRight = new JList(listR);
        listRight.setVisibleRowCount(12);
        scrollPaneR = new JScrollPane(listRight);
        panelCenter.add(scrollPaneR);
        scrollPaneR.add(listRight);
        scrollPaneR.setBorder(BorderFactory.createTitledBorder("Top Right Panel"));
        scrollPaneR.setVisible(true);
        listRight.setVisible(true);






        comboBox.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if(e.getStateChange() == ItemEvent.SELECTED) {
                    textField.setText((String) comboBox.getSelectedItem());
                    status.setText("comboBox Action Listener Fired");
                }
            }
        });


    }

}

You're adding your JLists to both a JScrollPane and the JPanel, and you're not allowed to do that as you can add a component to only one container. 您正在将JLists添加到JScrollPane JPanel中,并且您不能这样做,因为您只能将组件添加到一个容器中。 Here you will want to add your JLists to their respective JScrollPanes and then add the JScrollPane to your JPanel. 在这里,您将需要将JLists添加到它们各自的JScrollPanes,然后将JScrollPane添加到您的JPanel。


Also, you never add a JList to a JScrollPane per se, but rather you want to add the list to the scrollpane's viewport. 同样,您永远不会将JList本身添加到JScrollPane中,而是希望将列表添加到滚动窗格的视口中。 This is usually done in one of two ways: 通常通过以下两种方式之一完成此操作:

  1. Via the JScrollPane's constructor: new JScrollPane(myList) 通过JScrollPane的构造函数: new JScrollPane(myList)
  2. Or via a method: myScrollPane.setViewportView(myList) 或通过一种方法: myScrollPane.setViewportView(myList)

Also

  • I see you calling setSize(...) in your code, and you'll want to avoid that. 我看到您在代码中调用setSize(...) ,并且您想要避免这种情况。
  • You seem to be re-constructing components such as the JScrollPane for no apparent purpose. 您似乎在重新构造JScrollPane之类的组件没有明显目的。 Again, you don't want to do this either. 同样,您也不想这样做。

Edit 编辑
For example: 例如:

  //!! JScrollPane scrollPaneB = new JScrollPane();
  //!! scrollPaneB.setSize(200, 200);
  String[] listB = { "Some content on the right panel", "More content",
        "Some more content", "More and more content",
        "More and more content", "More and more content",
        "More and more content", "More and more content",
        "More and more content", "More and more content",
        "More and more content", "More and more content",
        "More and more content", "More and more content",
        "More and more content" };
  JList listBottom = new JList(listB);
  listBottom.setVisibleRowCount(12);
  JScrollPane scrollPaneB = new JScrollPane(listBottom);
  panelBottom.add(scrollPaneB);
  //!! scrollPaneB.add(listBottom);
  scrollPaneB.setBorder(BorderFactory.createTitledBorder("Bottom Panel"));
  scrollPaneB.setVisible(true);
  // !! listBottom.setVisible(true);

  JPanel panelCenter = new JPanel();
  frame.getContentPane().add(panelCenter, BorderLayout.CENTER);
  panelCenter.setLayout(new GridLayout(0, 2, 0, 0));

  //!! JScrollPane scrollPaneL = new JScrollPane();
  // !! scrollPaneL.setSize(200, 200);
  String[] listL = { "Some content on the right panel", "More content",
        "Some more content", "More and more content",
        "More and more content", "More and more content",
        "More and more content", "More and more content",
        "More and more content", "More and more content",
        "More and more content", "More and more content",
        "More and more content", "More and more content",
        "More and more content" };
  JList listLeft = new JList(listL);
  listLeft.setVisibleRowCount(12);
  JScrollPane scrollPaneL = new JScrollPane(listLeft);
  panelCenter.add(scrollPaneL);
  // !! scrollPaneL.add(listLeft);
  scrollPaneL.setBorder(BorderFactory.createTitledBorder("Top Left Panel"));
  // !! scrollPaneL.setVisible(true);
  // !! listLeft.setVisible(true);

  //!! JScrollPane scrollPaneR = new JScrollPane();
  //!! scrollPaneR.setSize(200, 200);
  String[] listR = { "Some content on the right panel", "More content",
        "Some more content", "More and more content",
        "More and more content", "More and more content",
        "More and more content", "More and more content",
        "More and more content", "More and more content",
        "More and more content", "More and more content",
        "More and more content", "More and more content",
        "More and more content" };
  JList listRight = new JList(listR);
  listRight.setVisibleRowCount(12);
  JScrollPane scrollPaneR = new JScrollPane(listRight); //!! 
  panelCenter.add(scrollPaneR);
  //!! scrollPaneR.add(listRight);
  scrollPaneR.setBorder(BorderFactory.createTitledBorder("Top Right Panel"));
  //!! scrollPaneR.setVisible(true);
  //!! listRight.setVisible(true);

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

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