java如何在文本框上加滚动条

java如何在文本框上加滚动条,第1张

需要javaxswing里面的JScrollPane组件
给你个例子吧
import javaxswing;
public class TestScroll {
public TestScroll(){
JFrame jf=new JFrame("test");
JPanel jp=new JPanel();
JTextArea jta=new JTextArea(8,20);
JScrollPane jsp=new JScrollPane(jta);//新建一个滚动条界面,将文本框传入
jpadd(jsp);//注意:将滚动条界面添加到组建中,而不是添加文本框了
jfadd(jp);

jfpack();
jfsetLocation(300,300);
jfsetVisible(true);
jfsetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestScroll();
}
}

我调试了,下面修改可以。 不过FlowLayout布局效果不好,再多的,它也
是一子排开。 数量达到上百时,机器像蜗牛一样……
import javaawt;
import javaawtevent;
import javaxswing;
import javaio;
public class demotest extends JFrame{
JScrollPane scroll;
JPanel pan; //用此pan来添加,然后把它放入主窗口。
public demotest(String s){
pan = new JPanel();
pansetLayout(new FlowLayout());
scroll = new JScrollPane(pan);
scrollsetHorizontalScrollBarPolicy(ScrollPaneConstantsHORIZONTAL_SCROLLBAR_AS_NEEDED);//设置何时显示滚动条
scrollsetVerticalScrollBarPolicy(ScrollPaneConstantsVERTICAL_SCROLLBAR_ALWAYS); //设置何时显示滚动条
Container container = getContentPane();
containeradd(scroll);
File file = new File(s);
String[] picture = filelist();
int j,k;
int[] flag = new int[picturelength];
for(k = 0,j = 0;j < picturelength;j++) {
if(picture[j]toLowerCase()endsWith("jpg") || picture[j]toLowerCase()endsWith("jpeg") || picture[j]toLowerCase()endsWith("gif")
|| picture[j]toLowerCase()endsWith("png")){
k++;
//Systemoutprintln(picture[j]);
}
}
String[] pic = new String[k];
for(k = 0,j = 0;j < picturelength;j++) {
if(picture[j]toLowerCase()endsWith("jpg") || picture[j]toLowerCase()endsWith("jpeg") || picture[j]toLowerCase()endsWith("gif")
|| picture[j]toLowerCase()endsWith("png")){
pic[k] = s + "\\" + picture[j];
flag[k] = j;
k++;
}
}
for(int i = 0;i < piclength;i++){
ImageIcon a = new ImageIcon(pic[i]);
Image b = agetImage();
JLabel label = new JLabel(new ImageIcon(bgetScaledInstance(250,170,ImageSCALE_DEFAULT)));
JLabel label2 = new JLabel(picture[flag[i]],JLabelCENTER);
JPanel jpanel = new JPanel();
jpanelsetLayout(new BorderLayout());
jpaneladd(label,BorderLayoutCENTER);
jpaneladd(label2,BorderLayoutSOUTH);
JScrollPane c = new JScrollPane();
csetViewportView(jpanel);
panadd(c);
}
containeradd(scroll);
thissetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
thissetSize(800,660);
thissetVisible(true);
}
public static void main(String[] args){
demotest demo = new demotest("D:\\media\\photos");//(自己添加路径试试程序)
}
}

使用了布局管理器之后,用setPreferredSize(new Dimension(width, height))设置组件大小。
你可以试试button1setPreferredSize(new Dimension(width, height));

您好,我来为您解答:
方法一:(注:此方法只对ScrollView中有单个控件时有用,如只有widget2)
使用ScrollView的方法:scrollViewsmoothScrollTo(0, 0); 或 scrollViewscrollTo(int,int);
方法二:
一开始的时候让上面的控件获得焦点,这样滚动条就自然停在顶部了。
希望我的回答对你有帮助。

JScrollPanel本来就是动态出现的,如果超出长度就会出现。
你这样试试:
JScrollPanel j = new JScrollPanel();
jadd(jLabel1);
jadd(jLabel2);
jadd(jLabel3);
这样应该没问题吧。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/10373723.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-08
下一篇 2023-05-08

发表评论

登录后才能评论

评论列表(0条)

保存