滚动条没有出现在 JPanel 中
Scrollbar is not appearing in a JPanel
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.*;
import java.awt.Dimension;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JScrollPane;
import javax.swing.border.LineBorder;
class Test
{
public Test()
{
JFrame f = new JFrame();
f.setSize(800,800);
f.setLayout(new FlowLayout());
JPanel s = new JPanel();
s.setLayout(new BoxLayout(s,BoxLayout.PAGE_AXIS));
JLabel m = new JLabel("D");
JLabel m1 = new JLabel("D1");
JLabel m2 = new JLabel("D1");
JLabel m3 = new JLabel("D1");
JLabel m4 = new JLabel("D1");
JLabel m5 = new JLabel("D1");
JLabel m6 = new JLabel("D1");
JLabel m7 = new JLabel("D1");
JLabel m8 = new JLabel("D1");
s.add(m);
s.add(m1);
s.add(m2);
s.add(m3);
s.add(m4);
s.add(m5);
s.add(m6);
s.add(m7);
s.add(m8);
JScrollPane scroll = new JScrollPane(s);
//scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setViewportBorder(new LineBorder(Color.RED));
f.add(scroll);
f.setVisible(true);
}
public static void main(String args[])
{
Test t = new Test();
}
}
一切正常,但是 Scroll 没有显示(出现了 JScollPane).. 有没有更好的方法来做到这一点?我的意思是 - 在面板中显示 JScrollPane 的正确方法......?
It's showing like this
当添加到滚动窗格的组件的首选大小大于滚动窗格的大小时,会出现滚动条。
JScrollPane scroll = new JScrollPane(s);
scroll.setPreferredSize(new Dimension(100, 50));
滚动窗格将在您向其中添加元素时扩展其高度,除非您设置了首选大小。
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.*;
import java.awt.Dimension;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JScrollPane;
import javax.swing.border.LineBorder;
class Test
{
public Test()
{
JFrame f = new JFrame();
f.setSize(800,800);
f.setLayout(new FlowLayout());
JPanel s = new JPanel();
s.setLayout(new BoxLayout(s,BoxLayout.PAGE_AXIS));
JLabel m = new JLabel("D");
JLabel m1 = new JLabel("D1");
JLabel m2 = new JLabel("D1");
JLabel m3 = new JLabel("D1");
JLabel m4 = new JLabel("D1");
JLabel m5 = new JLabel("D1");
JLabel m6 = new JLabel("D1");
JLabel m7 = new JLabel("D1");
JLabel m8 = new JLabel("D1");
s.add(m);
s.add(m1);
s.add(m2);
s.add(m3);
s.add(m4);
s.add(m5);
s.add(m6);
s.add(m7);
s.add(m8);
JScrollPane scroll = new JScrollPane(s);
//scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setViewportBorder(new LineBorder(Color.RED));
f.add(scroll);
f.setVisible(true);
}
public static void main(String args[])
{
Test t = new Test();
}
}
一切正常,但是 Scroll 没有显示(出现了 JScollPane).. 有没有更好的方法来做到这一点?我的意思是 - 在面板中显示 JScrollPane 的正确方法......?
It's showing like this
当添加到滚动窗格的组件的首选大小大于滚动窗格的大小时,会出现滚动条。
JScrollPane scroll = new JScrollPane(s);
scroll.setPreferredSize(new Dimension(100, 50));
滚动窗格将在您向其中添加元素时扩展其高度,除非您设置了首选大小。