JavaSE项目中怎样让JPanel和JScrollPane结合出现自动滚动条,并运用到自己的项目中出现不能正常显示出滚动条的原因
2014-05-07 14:22
459 查看
创建一个java 项目测试,这个是ok的 :
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class InvokeTest extends JFrame {
public InvokeTest() {
super("TestJScrollPane");
this.setLayout(null);
this.setBounds(200, 200, 300, 300);
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(300,100));//主要是这句代码,设置panel的首选大小,同时保证宽高大于JScrollPane的宽高,这样下面的JScrollPane才会出现滚动条
JButton button1 = new JButton("1");
panel.add(button1);
JButton button2 = new JButton("2");
panel.add(button2);
JButton button3 = new JButton("3");
panel.add(button3);
JButton button4 = new JButton("4");
panel.add(button4);
JButton button5 = new JButton("5");
panel.add(button5);
JButton button6 = new JButton("6");
panel.add(button6);
JButton button7 = new JButton("7");
panel.add(button7);
JScrollPane scrollPane = new JScrollPane(panel);
scrollPane.setBounds(10, 10, 175, 70);
this.getContentPane().add(scrollPane);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new InvokeTest();
}
};
将上面的思路运用到自己项目中,来滚动显示自己的图片
比如我自定义一个绘图Panel继承JPanel ,起名DrawPanel 或IMagePanel :public class DrawPanel extends JPanel implements MouseMotionListener, MouseListener{
///////各种方法如绘制图片,我就不详细列出了!
};
然后定义一个继承JFrame的类public class MapEdit extends JFrame {
//比如这个地图编辑器
private JScrollPane getMainPanel() {
private JScrollPane mainPanel = null;
private JScrollPane scrollPane= null;
private JPanel getJContentPane() { //布局
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getJPanel(), BorderLayout.NORTH);
jContentPane.add(getMainPanel(), BorderLayout.CENTER);
}
return jContentPane;
}
if (mainPanel == null) {
this.setLayout(null);
JPanel panel = new DrawPanel(this,Res.tileWidth*Res.tilex, Res.tileHeight*Res.tiley );
panel.setPreferredSize(new Dimension(Res.tileWidth*Res.tilex, Res.tileHeight*Res.tiley));//主要是这句代码,设置panel的首选大小,
scrollPane = new JScrollPane(panel);
scrollPane.setBounds(0,0, 300, 300);
this.getContentPane().add(scrollPane);
this.setVisible(true);
mainPanel =scrollPane;//注意这就是你没有显示出滚动条原因:你返回的应该是你创建的滚动条并应用到布局中,而不是返回绘图的Jpanel,应该是包含了这个绘图的
// 绘图的滚动条:注意 如果返回 mainPanel =panel 就不能正常显示出滚动条了
}
return mainPanel;
}
}
注意:jpanel和jscrollpanel 滚动条在窗口人为拉小或在 DrawPanel中增加一个图片元素后窗口拉小,滚动条消失原因:你可能在DrawPanel中鼠标事件设置了小面这行
public void mouseClicked(MouseEvent arg0) {
this.setPreferredSize(new Dimension(100,300)); -----这不是是缩小了viewPort吗,怎么能显示完全呢,亲??
}
的事件注释或去掉下面这句
// this.setPreferredSize(new Dimension(100,300));
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class InvokeTest extends JFrame {
public InvokeTest() {
super("TestJScrollPane");
this.setLayout(null);
this.setBounds(200, 200, 300, 300);
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(300,100));//主要是这句代码,设置panel的首选大小,同时保证宽高大于JScrollPane的宽高,这样下面的JScrollPane才会出现滚动条
JButton button1 = new JButton("1");
panel.add(button1);
JButton button2 = new JButton("2");
panel.add(button2);
JButton button3 = new JButton("3");
panel.add(button3);
JButton button4 = new JButton("4");
panel.add(button4);
JButton button5 = new JButton("5");
panel.add(button5);
JButton button6 = new JButton("6");
panel.add(button6);
JButton button7 = new JButton("7");
panel.add(button7);
JScrollPane scrollPane = new JScrollPane(panel);
scrollPane.setBounds(10, 10, 175, 70);
this.getContentPane().add(scrollPane);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new InvokeTest();
}
};
将上面的思路运用到自己项目中,来滚动显示自己的图片
比如我自定义一个绘图Panel继承JPanel ,起名DrawPanel 或IMagePanel :public class DrawPanel extends JPanel implements MouseMotionListener, MouseListener{
///////各种方法如绘制图片,我就不详细列出了!
};
然后定义一个继承JFrame的类public class MapEdit extends JFrame {
//比如这个地图编辑器
private JScrollPane getMainPanel() {
private JScrollPane mainPanel = null;
private JScrollPane scrollPane= null;
private JPanel getJContentPane() { //布局
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getJPanel(), BorderLayout.NORTH);
jContentPane.add(getMainPanel(), BorderLayout.CENTER);
}
return jContentPane;
}
if (mainPanel == null) {
this.setLayout(null);
JPanel panel = new DrawPanel(this,Res.tileWidth*Res.tilex, Res.tileHeight*Res.tiley );
panel.setPreferredSize(new Dimension(Res.tileWidth*Res.tilex, Res.tileHeight*Res.tiley));//主要是这句代码,设置panel的首选大小,
scrollPane = new JScrollPane(panel);
scrollPane.setBounds(0,0, 300, 300);
this.getContentPane().add(scrollPane);
this.setVisible(true);
mainPanel =scrollPane;//注意这就是你没有显示出滚动条原因:你返回的应该是你创建的滚动条并应用到布局中,而不是返回绘图的Jpanel,应该是包含了这个绘图的
// 绘图的滚动条:注意 如果返回 mainPanel =panel 就不能正常显示出滚动条了
}
return mainPanel;
}
}
注意:jpanel和jscrollpanel 滚动条在窗口人为拉小或在 DrawPanel中增加一个图片元素后窗口拉小,滚动条消失原因:你可能在DrawPanel中鼠标事件设置了小面这行
public void mouseClicked(MouseEvent arg0) {
this.setPreferredSize(new Dimension(100,300)); -----这不是是缩小了viewPort吗,怎么能显示完全呢,亲??
}
的事件注释或去掉下面这句
// this.setPreferredSize(new Dimension(100,300));
相关文章推荐
- thinkphp结合KindEditor 上传图片的时候出现“\”导致图片不能正常显示,
- Studio中的项目接入百度地图sdk后,地图不能正常显示及出现 errorcode: 230 uid: -1 appid -1 msg: APP Scode码校验失败 问题的解决方法
- GB2312编码IE显示正常,java处理后出现问号的原因
- 关于JLable列表的自动滚动条的显示:当JLable列表的内容超出JPanel面板所规定的高度时,自动显示滚动条的方法
- 出现HP Loadrunner Virtual User Generator 已停止工作,原因可能是设置了工具 > 常规选项-->显示选项卡, 选择回放期间显示运行时查看器和自动排列窗
- DoModal返回-1,对话框不能显示,今天碰到项目在用unicode编码,和多字符编码时候出现的
- 图片路径中含有中文在jsp下不能正常显示的原因及解决
- CVS导入项目时某些文件不能正常导入的原因
- iOS适配 旧项目工程在iOS9下不能正常显示
- 怎样使可编辑的DIV固定大小,当内容多时,就自动出现滚动条
- 智能安全实验室-全能优化(Guardio) 3.8.0.491:修正谁在劫持我的计算机,双击项目时出现窗口不能显示的问题;
- JScrollPane控件中添加JPanel后滚动条显示问题
- win10操作系统下,TortoiseSVN(或TortoiseGit)客户端正常安装后,svn(或git)项目的文件/文件夹图标不能正常显示的解决
- eclipes创建一个web项目web.xml不能自动更新的原因(web.xml和@WebServlet的作用)
- 【博客项目笔记】ckeditor不能正常显示的问题
- win10操作系统下,TortoiseSVN(或TortoiseGit)客户端正常安装后,svn(或git)项目的文件/文件夹图标不能正常显示的解决
- SPS安装过程中配置服务器场帐户设置页面不能正常显示以及IIS中SPS管理站点未配置成功问题的原因
- 使用.Net 1.1的项目,TreeView控件不能正常显示
- BootStrap中小图标在web项目中不能正常显示
- notepad中文不能正常显示,出现小方格乱码