想做一个滚动面板的示例。编译出错提示是:
at java.awt.Container.add(Container.java:410)
at Jscrollpane.(Jscrollpane.java:25)
at Jscrollpane.main(Jscrollpane.java:7)
下面是代码:
import javax.swing.*;
import java.awt.*;
import java.lang.String;
public class Jscrollpane extends JFrame{
public static void main(String[] args){
Jscrollpane frame = new Jscrollpane();
frame.setVisible(true);
}
public Jscrollpane(){
super();
setTitle("Jscrollpane面板");
setResizable(false);
setBounds(100,100,570,725);
final JScrollPane frameScrollPane = new JScrollPane();
frameScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
getContentPane().add(frameScrollPane);
final JPanel framePanel = new JPanel();
framePanel.setLayout(new BorderLayout());
frameScrollPane.setViewportView(framePanel);
final JPanel typePanel = new JPanel();
framePanel.add(typePanel,BorderLayout.NORTH);
final JLabel typeLabel = new JLabel();
typeLabel.setText("类别: ");
typeLabel.add(typeLabel);
JScrollPane typeScrollPane = new JScrollPane();
typeScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
typePanel.add(typeScrollPane);
String[] items = {"幽默短信类","新年祝福短信类","身日祝福短信类","新婚祝福短信类"};
JList list = new JList(items);
list.setVisibleRowCount(3);
typeScrollPane.setViewportView(list);
final JLabel label = new JLabel();
label.setPreferredSize(new Dimension(110,0));
typePanel.add(label);
final JPanel contentPanel = new JPanel();
framePanel.add(contentPanel);
final JLabel contentLabel = new JLabel();
contentLabel.setText("内容: ");
contentPanel.add(contentLabel);
JScrollPane contentScrollPane = new JScrollPane();
contentScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
contentPanel.add(contentScrollPane);
JTextArea textArea = new JTextArea();
textArea.setRows(3);
textArea.setColumns(20);
textArea.setLineWrap(true);
contentScrollPane.setViewportView(textArea);
}
}