Swing JScrollPanel问题
我在JScrollPanel中加了个JPanel,为什么JPanel显示在后面???如图发不了图看这个帖子吧:http://bbs.
然后我把JScrollPanel的layout设置位null,JPanel就显示在前面了。但是滚动条就没了。
designScrollPanel.setLayout(null);
如图:
程序代码:
import java.awt.Color; import java.awt.Dimension; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.util.ArrayList; import java.util.List; import javax.swing.BorderFactory; 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.JScrollPane; import javax.swing.ScrollPaneConstants; import javax.swing.ScrollPaneLayout; import com.withiter.util.ResolutionUtils; public class Main extends JFrame { private static final long serialVersionUID = 3350060192892360223L; class Resolution { private String name; private String value; public Resolution(String name, String value) { super(); this.name = name; this.value = value; } public String toString(){ return name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } } class Template { private Resolution resolution; private int areas; public Template(Resolution resolution, int areas) { super(); this.resolution = resolution; this.areas = areas; } public Template(int areas){ super(); this.areas = areas; } public Resolution getResolution() { return resolution; } public void setResolution(Resolution resolution) { this.resolution = resolution; } public int getAreas() { return areas; } public void setAreas(int areas) { this.areas = areas; } @Override public String toString() { if(areas == 0){ return "请选择模板区域数目"; } return "区域数目:"+areas; } } private JLabel resolutionLabel; private JLabel templateLabel; private JComboBox jcbresolution; private JComboBox jcbTemplate; private JScrollPane designScrollPanel; private JPanel designPanel; private JLabel test1 = new JLabel("asdfkjashdkghasdlkjghalskjdghlakjshdglk"); private JLabel test2 = new JLabel("asdfkjashdkghasdlkjghalskjdghlakjshdglk"); private JLabel test3 = new JLabel("asdfkjashdkghasdlkjghalskjdghlakjshdglk"); private JLabel test4 = new JLabel("asdfkjashdkghasdlkjghalskjdghlakjshdglk"); private JLabel test5 = new JLabel("asdfkjashdkghasdlkjghalskjdghlakjshdglk"); private JButton btAdd; private JButton btReset; private JLabel programsListLabel; private JList programsList; private List<Resolution> listTimes = new ArrayList<Resolution>(); private List<Template> listTemplate = new ArrayList<Template>(); Main() { Toolkit tk = Toolkit.getDefaultToolkit(); Dimension screenSize = tk.getScreenSize(); int x = (int) screenSize.getWidth(); int y = (int) screenSize.getHeight(); this.setBounds((x - 1140) / 2, (y - 740) / 2, 1140, 740); this.setResizable(false); this.setLayout(null); this.setTitle("CPSMini 发布平台"); this.setBackground(Color.BLACK); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // add resolutions listTimes.add(new Resolution("请选择合适的LCD分辨率","0")); listTimes.add(new Resolution("1920*1080(16:9)","1920*1080_16:9")); listTimes.add(new Resolution("1600*1200(4:3)","1600*1200_4:3")); listTimes.add(new Resolution("1280*1024(5:4)","1280*1024_5:4")); listTimes.add(new Resolution("1280*960(4:3)","1280*960_4:3")); listTimes.add(new Resolution("1280*768(5:3)","1280*768_5:3")); listTimes.add(new Resolution("1024*768(4:3)","1024*768_4:3")); listTimes.add(new Resolution("800*640(5:4)","800*640_5:4")); listTimes.add(new Resolution("800*600(4:3)","800*600_4:3")); listTimes.add(new Resolution("800*450(16:9)","800*450_16:9")); listTimes.add(new Resolution("1080*1920(9:16)","1080*1920_9:16")); listTimes.add(new Resolution("1200*1600(3:4)","1200*1600_3:4")); listTimes.add(new Resolution("1024*1280(4:5)","1024*1280_4:5")); listTimes.add(new Resolution("960*1280(3:4)","960*1280_3:4")); listTimes.add(new Resolution("768*1280(3:5)","768*1280_3:5")); listTimes.add(new Resolution("768*1024(3:4)","768*1024_3:4")); listTimes.add(new Resolution("640*800(4:5)","640*800_4:5")); listTimes.add(new Resolution("600*800(3:4)","600*800_3:4")); listTimes.add(new Resolution("450*800(9:16)","450*800_9:16")); // add templates listTemplate.add(new Template(0)); listTemplate.add(new Template(2)); listTemplate.add(new Template(3)); resolutionLabel = new JLabel("请选择显示屏分辨率"); jcbresolution = new JComboBox(listTimes.toArray()); templateLabel = new JLabel("请选择节目模板"); jcbTemplate = new JComboBox(listTemplate.toArray()); designScrollPanel = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); designPanel = new JPanel(); btAdd = new JButton("添加到节目单"); btReset = new JButton("重置选择的素材"); programsListLabel = new JLabel("节目列表"); programsList = new JList(); programsListLabel.setBounds(850, 30, 220, 20); programsList.setBounds(850, 60, 260, 400); programsList.setBorder(BorderFactory.createLineBorder(Color.GRAY)); resolutionLabel.setBounds(30, 30, 120, 20); jcbresolution.setBounds(170, 30, 200, 20); templateLabel.setBounds(420, 30, 120, 20); jcbTemplate.setBounds(560, 30, 200, 20); btAdd.setBounds(850, 470, 120, 20); btReset.setBounds(980, 470, 130, 20); // 设计区域大小默认800*640 designPanel.setBackground(Color.red); designPanel.add(test1); designPanel.add(test2); designPanel.add(test3); designPanel.add(test4); designPanel.add(test5); designScrollPanel.add(designPanel); designPanel.setBounds(100, 100, 500, 500); designPanel.setPreferredSize(new Dimension(500, 500)); // designPanel.setBorder(BorderFactory.createLineBorder(Color.GRAY)); // designPanel.setAutoscrolls(false); designScrollPanel.setBounds(30, 60, 800, 640); // designScrollPanel.setBackground(Color.black); designScrollPanel.setLayout(new ScrollPaneLayout()); // designScrollPanel.setBorder(BorderFactory.createLineBorder(Color.GRAY)); // designScrollPanel.setAutoscrolls(true); jcbresolution.addItemListener(new ItemListener(){ @Override public void itemStateChanged(ItemEvent e) { // TODO Auto-generated method stub if(e.getStateChange() == ItemEvent.SELECTED){ String selected = ((Resolution)jcbresolution.getSelectedItem()).getValue(); System.out.println(selected); if(selected.equals("0")){ return; } String[] xy = selected.split("_"); int weidth = Integer.parseInt(xy[0].split("\\*")[0]); int height = Integer.parseInt(xy[0].split("\\*")[1]); System.out.println(xy[0]); int lcm = ResolutionUtils.getLcm(weidth, height); System.out.println(lcm); if(xy[1].equals("4:3")){ designPanel.setSize(800, 600); } if(xy[1].equals("5:3")){ designPanel.setSize(800, 480); } if(xy[1].equals("5:4")){ designPanel.setSize(800, 640); } if(xy[1].equals("16:9")){ designPanel.setSize(800, 450); } if(xy[1].equals("3:4")){ designPanel.setSize(600, 800); } if(xy[1].equals("3:5")){ designPanel.setSize(480, 800); } if(xy[1].equals("4:5")){ designPanel.setSize(640, 800); } if(xy[1].equals("9:16")){ designPanel.setSize(450, 800); } } } }); jcbTemplate.addItemListener(new ItemListener(){ @Override public void itemStateChanged(ItemEvent e) { // TODO Auto-generated method stub if(e.getStateChange() == ItemEvent.SELECTED){ int selected = ((Template)jcbTemplate.getSelectedItem()).getAreas(); System.out.println(selected); } } }); btAdd.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String desKey = String.valueOf(System.currentTimeMillis()); int index = jcbTemplate.getSelectedIndex(); String timeSelected = listTimes.get(index).getValue(); int length = timeSelected.length(); String firstLine = length + "" + timeSelected + "" + desKey; System.out.println(firstLine); } }); btReset.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { } }); this.add(programsListLabel); this.add(programsList); this.add(resolutionLabel); this.add(jcbresolution); this.add(templateLabel); this.add(jcbTemplate); this.add(btAdd); this.add(btReset); this.add(designScrollPanel); this.setVisible(true); } public static void main(String[] args) { new Main(); } }
代码如下: