日期:2014-05-20  浏览次数:20664 次

The problem about JScrollPane
想在JScrollPane中实现滚动条的效果,其中添加了3个label,滚动条没反应,还需要添加什么代码不?
Java code

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(p_rslt_future);
    }
    
    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);
    }

}















------解决方案--------------------
label 可以添加在里面么。。不是很清楚。
最近在写一个迷你IE的作业,用到了这个。
Java code

JEditorPane jep = new JEditorPane();
JScrollPane jsp = new JScrollPane(jep);

------解决方案--------------------
Java code

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);
    }

}

------解决方案--------------------
Java code

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;
    }

}