JList没有出现在JPanel中

时间:2014-06-30 21:46:37

标签: java swing jpanel jlist

我对正在处理的程序进行了一些更改。很好的建议,我已经将JScrollPanel中的JTextArea更改为JList。出于某种原因,即使我已经编写了JList的内容,该区域显示为空白,大约1行高。我将每个JList的默认大小设置为(200,200)。有什么想法吗?

代码:

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


    }

}

1 个答案:

答案 0 :(得分:6)

您正在将JLists添加到JScrollPane JPanel,并且您不能这样做,因为您只能将组件添加到一个容器中。在这里,您需要将JLists添加到各自的JScrollPanes,然后将JScrollPane添加到JPanel。


此外,您永远不会将JList添加到JScrollPane本身,而是希望将列表添加到滚动窗格的视口中。这通常以两种方式之一完成:

  1. 通过JScrollPane的构造函数:new JScrollPane(myList)
  2. 或通过方法:myScrollPane.setViewportView(myList)
  3. 另外

    • 我看到您在代码中调用setSize(...),并且您希望避免这种情况。
    • 您似乎正在重新构建JScrollPane等组件,但没有明显的用途。同样,你也不想这样做。

    修改
    例如:

      //!! 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);