当前位置: 技术问答>java相关
关于JPanel滚动的问题
来源: 互联网 发布时间:2015-08-10
本文导语: 一个JPanel,Layout为null, 其中画了一个JLabel,当移动出panel边界时,希望出现滚动条. 如下程序,初始有滚动条,但当物件出界后,滚动条没有增加滚动范围. 问:如何在panel的范围扩大后,随之改变滚动条的范围?import java.awt.*; imp...
一个JPanel,Layout为null,
其中画了一个JLabel,当移动出panel边界时,希望出现滚动条.
如下程序,初始有滚动条,但当物件出界后,滚动条没有增加滚动范围.
问:如何在panel的范围扩大后,随之改变滚动条的范围?import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class Test extends Frame {
public JLabel bl = new JLabel(" b1 ");
JPanel editor;
Point lastLocation;
public Test() {
setLayout(new BorderLayout());
editor = new JPanel();
editor.setLayout(null);
editor.setPreferredSize(new Dimension(300,400));
JScrollPane scroller = new JScrollPane();
JViewport port = scroller.getViewport();
port.add(editor);
port.setScrollMode(1);
add("Center", scroller);
bl.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
if( lastLocation == null )
return;
JLabel ls = (JLabel) e.getComponent();
int incx = lastLocation.x - e.getX(),
incy = lastLocation.y - e.getY();
ls.setLocation(ls.getX()-incx,ls.getY()-incy);
}
});
bl.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e ) {
lastLocation = e.getPoint();
}
public void mouseReleased(MouseEvent e ) {
lastLocation = null;
}
public void mouseClicked(MouseEvent e ) {
System.out.println("c");
JLabel ls = (JLabel) e.getComponent();
ls.setForeground(Color.blue);
}
});
bl.setBounds(new Rectangle(100, 39, 55, 22));
bl.setBorder(new LineBorder(Color.BLUE, 1));
editor.add(bl);
}
public static void main(String[] args) {
Test frame = new Test();
frame.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setSize(300, 400);
frame.setVisible(true);
}
}
其中画了一个JLabel,当移动出panel边界时,希望出现滚动条.
如下程序,初始有滚动条,但当物件出界后,滚动条没有增加滚动范围.
问:如何在panel的范围扩大后,随之改变滚动条的范围?import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class Test extends Frame {
public JLabel bl = new JLabel(" b1 ");
JPanel editor;
Point lastLocation;
public Test() {
setLayout(new BorderLayout());
editor = new JPanel();
editor.setLayout(null);
editor.setPreferredSize(new Dimension(300,400));
JScrollPane scroller = new JScrollPane();
JViewport port = scroller.getViewport();
port.add(editor);
port.setScrollMode(1);
add("Center", scroller);
bl.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
if( lastLocation == null )
return;
JLabel ls = (JLabel) e.getComponent();
int incx = lastLocation.x - e.getX(),
incy = lastLocation.y - e.getY();
ls.setLocation(ls.getX()-incx,ls.getY()-incy);
}
});
bl.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e ) {
lastLocation = e.getPoint();
}
public void mouseReleased(MouseEvent e ) {
lastLocation = null;
}
public void mouseClicked(MouseEvent e ) {
System.out.println("c");
JLabel ls = (JLabel) e.getComponent();
ls.setForeground(Color.blue);
}
});
bl.setBounds(new Rectangle(100, 39, 55, 22));
bl.setBorder(new LineBorder(Color.BLUE, 1));
editor.add(bl);
}
public static void main(String[] args) {
Test frame = new Test();
frame.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setSize(300, 400);
frame.setVisible(true);
}
}
|
滚动条是根据JPanel的初始大小来确定出现与否。你要做的事是动态改变JPanel的大小。
|
用setPreferedSize()设置JPanel大小,不要用setSize()
|
建议你最好不要吧awt和swing的组件一起来用,这样有一些问题你想也想不到的。还有你的代码写的怎么这么难看呀,一句话的事写了这么多:JScrollPane scroller = new JScrollPane();
JViewport port = scroller.getViewport();
port.add(editor);
port.setScrollMode(1);
完全可以用
JScrollPane scroller = new JScrollPane(editor);
一句替换吗
JViewport port = scroller.getViewport();
port.add(editor);
port.setScrollMode(1);
完全可以用
JScrollPane scroller = new JScrollPane(editor);
一句替换吗