import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.List;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
public class PanelTest extends JFrame {
private JPanel listPanel;
private JScrollPane leftJScrollPanel;
private JScrollPane playJScrollPanel;
private List playList;
private List leftList;
private JSplitPane split;
private Container cp;
public PanelTest(){
init();
}
public void init(){
this.setSize(new Dimension(300, 379));
this.setTitle("PLAYER");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setResizable(false);
this.setLocation(200, 100);
this.setVisible(true);
}
public void lunchFrame(){
cp = getContentPane();
listPanel = new JPanel();
listPanel.setLayout(new BorderLayout());
//listPanel.setLayout(null);
//listPanel.setBounds(new Rectangle(4,150,289,148));
listPanel.setLocation(4, 150);
listPanel.setSize(new Dimension(289,148));
listPanel.setBorder(BorderFactory.createEtchedBorder());
leftList = new List();
leftJScrollPanel = new JScrollPane(leftList);
//leftJScrollPanel.setBounds(new Rectangle(4,150,100,148));
listPanel.add(leftJScrollPanel,BorderLayout.WEST);
playList = new List();
playJScrollPanel = new JScrollPane(playList);
//playJScrollPanel.setBounds(new Rectangle(100, 150, 189, 148));
listPanel.add(playJScrollPanel,BorderLayout.EAST);
split = new JSplitPane(SwingConstants.VERTICAL,true,leftJScrollPanel,playJScrollPanel );
split.setBorder(new EmptyBorder(0,0,0,0));
listPanel.add(split);
cp.add(listPanel);
}
public static void main(String args[]){
new PanelTest().lunchFrame();
}
}
------解决方案--------------------
List 换成 JList 就行了
AWT的组件是重量级的,Swing的组件是轻量级的,如果把重量级的和轻量级的用在一起,那么重量级的将覆盖掉轻量级的,List是重量级的
你的代码问题多多,帮你改了一下,list内容是空的,所以滚动条没有显示出来,当list有内容了切需要滚动条的时候它自然就出来了
- Java code
import java.awt.Dimension;import javax.swing.JFrame;import javax.swing.JList;import javax.swing.JScrollPane;import javax.swing.JSplitPane;import javax.swing.SwingConstants;import javax.swing.border.EmptyBorder;public class PanelTest extends JFrame{ private static final long serialVersionUID = 7699880680422202533L; private JScrollPane leftJScrollPanel; private JScrollPane playJScrollPanel; private JList playList; private JList leftList; private JSplitPane split; public PanelTest() { init(); } public void init() { lunchFrame(); this.setSize(new Dimension(300, 379)); this.setTitle("PLAYER"); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setResizable(false); this.setLocation(200, 100); this.setVisible(true); } public void lunchFrame() { leftList = new JList(); leftJScrollPanel = new JScrollPane(leftList); playList = new JList(); playJScrollPanel = new JScrollPane(playList); split = new JSplitPane(SwingConstants.VERTICAL, true, leftJScrollPanel, playJScrollPanel); split.setBorder(new EmptyBorder(0, 0, 0, 0)); //左边的宽度 split.setDividerLocation(120); getContentPane().add(split); } public static void main(String args[]) { new PanelTest(); }}