The problem about JScrollPane
想在JScrollPane中实现滚动条的效果,其中添加了3个label,滚动条没反应,还需要添加什么代码不?
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); }}JEditorPane jep = new JEditorPane();JScrollPane jsp = new JScrollPane(jep);
[解决办法]
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); }}
[解决办法]
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; }}
[解决办法]
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); }}
[解决办法]
再看個小例子吧:
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(); }}