帮帮忙! 怎么给JPanel 加上 JScrollPane!!!!

dds11one 2011-01-02 04:09:21


public class test extends JFrame{
private static final long serialVersionUID = 5699328341463955028L;
private JScrollPane jsp = null;
private JPanel jp = null;
private JPanel jpMainList2 = null;
private JTextArea jta = null;


private JScrollPane getJsp(){
if(jsp == null){
jsp = new JScrollPane();
jsp.setLayout(null);
jsp.setAutoscrolls(true);
jsp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
jsp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
jsp.add(getjp());
jsp.setSize(500,600);
jsp.setBackground(Color.red);
}
return jsp;
}

private JPanel getjp(){
if(jp == null){
jp = new JPanel();
jp.setLayout(null);
jp.add(getjta());
jp.setLocation(50, 50);
jp.setSize(1000,1000);
jp.setBackground(Color.white);

}
return jp;
}

private JTextArea getjta(){
if(jta == null){
jta = new JTextArea();
jta.setSize(1500, 1500);
jta.setBackground(Color.CYAN);
}
return jta;
}

public test(){
this.setLayout(null);
this.add(getJsp());
this.setExtendedState(Frame.MAXIMIZED_BOTH);
this.setVisible(true);
}

public static void main(String[] args){
new test();
}
}





Jpanel 的大小 已经超出 JScrollPane 的大小了 ,怎么滚动条没有用?
应该怎么做?帮帮忙!
...全文
181 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
dds11one 2011-01-02
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 inhibitory 的回复:]
Java code
import java.awt.Dimension;
import java.awt.Toolkit;

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

@SuppressWarn……
[/Quote]

搞定了,谢谢 还是布局的问题!
Inhibitory 2011-01-02
  • 打赏
  • 举报
回复
import java.awt.Dimension;
import java.awt.Toolkit;

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

@SuppressWarnings("serial")
public class Test extends JPanel {
@Override
public Dimension getPreferredSize() {
return new Dimension(600, 600); // 这个值只是推荐值,并不一定会使用到
}

private static void createGuiAndShow() {
JFrame frame = new JFrame("");

JPanel panel = new JPanel();
panel.add(new JButton("....Button...."));
panel.setPreferredSize(new Dimension(600, 600)); // 这里设置才有用
JScrollPane scroller = new JScrollPane(panel);
frame.getContentPane().add(scroller);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
int sw = Toolkit.getDefaultToolkit().getScreenSize().width;
int sh = Toolkit.getDefaultToolkit().getScreenSize().height;
int w = 400;
int h = 400;
int x = (sw - w) / 2;
int y = (sh - h) / 2 - 40;
x = x > 0 ? x : 0;
y = y > 0 ? y : 0;
frame.setBounds(x, y, w, h);
frame.setVisible(true);
}

public static void main(String[] args) {
createGuiAndShow();
}
}
huntor 2011-01-02
  • 打赏
  • 举报
回复
JTextArea textcomponent = new JTextArea();
textcomponent.set...;
JScrollPane scollpane = new JScrollPane(textcomponent,22,32);
dds11one 2011-01-02
  • 打赏
  • 举报
回复
还是不行啊 ,麻烦写个例子看看啊 ! 谢谢
dds11one 2011-01-02
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 inhibitory 的回复:]
出错是因为你使用了 setLayout(null);

Java code
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Frame;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSc……
[/Quote]



还是你行啊 ,麻烦写个例子看看啊 ! 谢谢
Inhibitory 2011-01-02
  • 打赏
  • 举报
回复
出错是因为你使用了 setLayout(null);
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Frame;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;

public class test extends JFrame {
private static final long serialVersionUID = 5699328341463955028L;
private JScrollPane jsp = null;
private JPanel jp = null;
private JPanel jpMainList2 = null;
private JTextArea jta = null;

private JScrollPane getJsp() {
if (jsp == null) {
jsp = new JScrollPane(getjp());
//jsp.setLayout(null);
jsp.setAutoscrolls(true);
jsp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
jsp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
//jsp.add(getjp());
jsp.setSize(500, 600);
jsp.setBackground(Color.red);
}
return jsp;
}

private JPanel getjp() {
if (jp == null) {
jp = new JPanel();
//jp.setLayout(null);
jp.setLayout(new BorderLayout());
jp.add(getjta());
jp.setLocation(50, 50);
jp.setSize(1000, 1000);
jp.setBackground(Color.white);

}
return jp;
}

private JTextArea getjta() {
if (jta == null) {
jta = new JTextArea();
jta.setSize(1500, 1500);
jta.setBackground(Color.CYAN);
}
return jta;
}

public test() {
this.setLayout(null);
this.add(getJsp());
this.setExtendedState(Frame.MAXIMIZED_BOTH);
this.setVisible(true);
}

public static void main(String[] args) {
new test();
}
}

dds11one 2011-01-02
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 inhibitory 的回复:]
Java code
private JScrollPane getJsp(){
if(jsp == null){
jsp = new JScrollPane(getjp());
jsp.setLayout(null);
jsp.setAutoscrolls(true);
j……
[/Quote]

没有用啊 请说清楚点
Inhibitory 2011-01-02
  • 打赏
  • 举报
回复
 private JScrollPane getJsp(){
if(jsp == null){
jsp = new JScrollPane(getjp());
jsp.setLayout(null);
jsp.setAutoscrolls(true);
jsp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
jsp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
//jsp.add(getjp()); 或者是setViewport(getjp());
jsp.setSize(500,600);
jsp.setBackground(Color.red);
}
return jsp;
}

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧