Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java GUI 设计  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 各位帮帮忙,有问题,滚屏的
showme





发贴: 1
积分: 0
于 2007-05-30 15:43 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
有个问题向大家请教一下,关于Swing的:我有一个JPanel,里面有N多JLabel,JPanel的布局为NULL,JLabel的大小与位置都是随机生成的,我希望有个滚动条能让我的程序看到所有JLabel,这个滚动条怎么加啊?
JscrollPanel不行啊,我试了
代码基本如下:
public void paint(){
JLayeredPane drawPanel=new JLayeredPane();
drawPanel.setLayout(null);
JLabel l=new JLabel();
l.setOpaque(true);
l.setBackground(Color.YElloW);
l.setForeground(Color.black);
l.setBounds(0,0,2000,3000);
drawPanel.add(l,new IntegerMusical Note,0);

JLabel l1=new JLabel();
l1.setOpaque(true);
l.setBackground(Color.BLUE);
l.setForeground(Color.black);
l1.setBound(100,200,40,50);
drawPanel.add(l1,new Integer(9),0);
}
大概是这样,各位高手帮我,少写这句了
我试过:
JScrollPane sc=new JScrollPane(drawPanel);
可是好像不行啊,谁能帮我写个例程,包括上面俩label的,主要是滚屏功能



作者 Re:各位帮帮忙,有问题,滚屏的 [Re:showme]
snowbug



CJSDN高级会员


发贴: 418
积分: 130
于 2007-05-31 03:38 user profilesend a private message to usersend email to snowbugsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
不应该 overwrite 那个 paint() 方法。

大致应该是这样子:

import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class SwingTest {

/**
* @param args
*/
public static void main(String[] args) {
JPanel labels = new JPanel(null);
labels.setPreferredSize(new Dimension(400,400));
JScrollPane sp = new JScrollPane(labels, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

JLabel lb = new JLabel("Label 1");
lb.setBounds(20, 20, 200,100);
labels.add(lb);

lb = new JLabel("Label 2");
lb.setBounds(50, 40,200,100);
labels.add(lb);

JFrame f = new JFrame("Multi-Labels");
f.getContentPane().add(sp);

f.pack();
f.setSize(200,200);
f.setVisible(true);

}

}



另外, 除了很特殊的情况,都应该用 layout manager 。




flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923