本帖最后由 lamar511 于 2012-04-05 16:13:47 编辑

解决方案 »

  1.   

    label 可以添加在里面么。。不是很清楚。
    最近在写一个迷你IE的作业,用到了这个。JEditorPane jep = new JEditorPane();
    JScrollPane jsp = new JScrollPane(jep);然后要JEditorPane 里面大于实际值就会有滚轴。。
    我只是路过。。
      

  2.   

    anybody here?
    any comment?
      

  3.   


    import java.awt.*;
    import javax.swing.*;public class TestFrame extends JFrame{
        
        public TestFrame() {
            Container mainc = this.getContentPane();
            JPanel    p_rslt_future = new JPanel();
    //        p_rslt_future.setLayout(null);
            JPanel pscr = new JPanel();
    //        pscr.setLayout(null);
            JLabel    day1 = new JLabel("DAY1"),
                    day2 = new JLabel("DAY2"),
                    day3 = new JLabel("DAY3");
            day1.setBounds(10, 10, 50, 20);
            day2.setBounds(10, 100, 50, 20);
            day3.setBounds(50, 230, 50, 20);
            pscr.add(day1);pscr.add(day2);pscr.add(day3);
            JScrollPane sp = new JScrollPane(pscr);
            
            sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
            sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
            sp.setBounds(3, 3, 438, 200);
            p_rslt_future.add(sp);
            mainc.add(sp);
        }
        
        public static void main(String args[]) {
            TestFrame myframe = new TestFrame();
            myframe.setSize(50, 60);
            myframe.setTitle("TestFrame");
            myframe.setVisible(true);
            myframe.setLocationRelativeTo(null);
            myframe.setDefaultCloseOperation(EXIT_ON_CLOSE);
        }}一是no layout二是p_rslt_future的存在二者都可能導致內容不會超出視界(viewport)
    或者允許no layout,嘗試setViewport(JViewport viewport) 
      

  4.   


    import java.awt.BorderLayout;
    import java.awt.Component;
    import java.awt.Container;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowListener;import javax.swing.ImageIcon;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.border.TitledBorder;public class RulsFrame extends BaseFrame {
    public RulsFrame() {
    init();
    } private void init() {
    setTitle("Online Exam Ruls");
    setSize(321, 450);
    setContentPane(createContentPanel());
    setDefaultCloseOperation(2); } private JPanel createContentPanel() {
    JPanel p = new JPanel(new BorderLayout());
    ImageIcon img = new ImageIcon(getClass().getResource("dsgsdg.png"));
    JLabel label = new JLabel(img);
    p.add(BorderLayout.NORTH, label);
    p.add(BorderLayout.CENTER, createCenterPanel());
    return p;
    } private JScrollPane createCenterPanel() {
    JScrollPane p = new JScrollPane();
    p.setBorder(new TitledBorder("Ruls"));
    JTextArea rules = new JTextArea();
    rules.setText("请同学严格遵守考试以下规则,否则视为主动放弃考试:\n1.不准带任何和考试有关的物品\n2.不准在考试期间交头接耳\n3.不准在考试期间泡妹子\n4.不准在考试期间看A片\n5.不准在考试期间吟诗\n\n\nPeople laugh and People cry\nSome give up Some always try\nSome say hi while some say bye\nWe all are walking dead!\n");
    rules.setLineWrap(true);
    rules.setEditable(false);
    p.getViewport().add(rules);
    return p;
    }}
    你加sp.getViewport().add();试试,就写过这一次,不熟
      

  5.   

    JScrollPane sp = new JScrollPane();-
    -
    -
    --
    -
    sp.getViewport().add(pscr);
      

  6.   


    不行呀,你那个例子是JTextArea的,我是想ScrollPane里面放各种不同的组建,例如图片、文字、button、等等。 用这个 sp.getViewport().add(pscr);了也不行。
      

  7.   


    import java.awt.*;
    import javax.swing.*;public class TestFrame extends JFrame{
        
        public TestFrame() {
            Container mainc = this.getContentPane();
            JPanel pscr = new JPanel();
            pscr.setPreferredSize(new Dimension(480, 300));
            //设置panel的首选大小,同时保证宽高大于JScrollPane的宽高,这样下面的JScrollPane才能滚动
            pscr.setLayout(null);
            JLabel    day1 = new JLabel("DAY1"),
                    day2 = new JLabel("DAY2"),
                    day3 = new JLabel("DAY3");
            day1.setBounds(10, 10, 50, 20);
            day2.setBounds(10, 100, 50, 20);
            day3.setBounds(50, 230, 50, 20);
            pscr.add(day1);
            pscr.add(day2);
            pscr.add(day3);
            JScrollPane sp = new JScrollPane(pscr);
            
            sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
            sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
            sp.setBounds(3, 3, 438, 200);
            mainc.add(sp);
        }
        
        public static void main(String args[]) {
            TestFrame myframe = new TestFrame();
            myframe.setSize(450, 235);
            myframe.setTitle("TestFrame");
            myframe.setVisible(true);
            myframe.setResizable(false);
            myframe.setLocationRelativeTo(null);
            myframe.setDefaultCloseOperation(EXIT_ON_CLOSE);
        }}
    改了下可以滚动了,你看下注释就明白了
      

  8.   

    再看個小例子吧:
    import java.awt.BorderLayout;
    import java.awt.GridLayout;import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;public class JScrollPaneRowHeaderView extends JFrame {
      public JScrollPaneRowHeaderView() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel(new GridLayout(5, 10));
        for (int i = 0; i < 50; i++) {
          panel.add(new JLabel("  Label " + i));
        }    JScrollPane scrolled = new JScrollPane(panel);
        scrolled.setRowHeaderView(new JLabel("Labels: "));    getContentPane().add(scrolled, BorderLayout.CENTER);    pack();
        setSize(300, 100);
        setVisible(true);
      }  public static void main(String[] args) {
        new JScrollPaneRowHeaderView();
      }
    }
      

  9.   


    运行成功, thanks a lot.
      

  10.   


    恩,这个例子可以,我想应该跟layout的类型有一定的关系吧。
    谢谢啦。