JList不会出现在JPanel中



我对正在开发的一个程序做了一些修改。根据大家的建议,我将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");
                }
            }
        });

    }
}

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


同样,您永远不会将JList添加到JScrollPane本身,而是希望将列表添加到scrollpane的视图中。这通常通过以下两种方式之一完成:

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

  • 我看到你在代码中调用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);

最新更新