当前位置: 技术问答>java相关
帮忙将这棵树加上滚动条!(付代码)
来源: 互联网 发布时间:2015-05-29
本文导语: 当节点超出applet的范围时,加上滚动条! import java.awt.*; import java.awt.event.*; import java.applet.*; import javax.swing.*; import javax.swing.tree.*; public class Applet1 extends Applet { public void init(){ try{ DefaultMutableTreeNode root...
当节点超出applet的范围时,加上滚动条!
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import javax.swing.tree.*;
public class Applet1 extends Applet {
public void init(){
try{
DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
DefaultMutableTreeNode one = new DefaultMutableTreeNode("one");
DefaultMutableTreeNode two = new DefaultMutableTreeNode("two");
DefaultMutableTreeNode three = new DefaultMutableTreeNode("three");
one.add(new DefaultMutableTreeNode("1"));
one.add(new DefaultMutableTreeNode("2"));
one.add(new DefaultMutableTreeNode("3"));
one.add(new DefaultMutableTreeNode("4"));
one.add(new DefaultMutableTreeNode("5"));
one.add(new DefaultMutableTreeNode("6"));
one.add(new DefaultMutableTreeNode("7"));
root.add(one);
root.add(two);
root.add(three);
root.add(new DefaultMutableTreeNode("4"));
root.add(new DefaultMutableTreeNode("5"));
root.add(new DefaultMutableTreeNode("6"));
root.add(new DefaultMutableTreeNode("7"));
root.add(new DefaultMutableTreeNode("8"));
root.add(new DefaultMutableTreeNode("9"));
two.add(new DefaultMutableTreeNode("4"));
two.add(new DefaultMutableTreeNode("5"));
two.add(new DefaultMutableTreeNode("6"));
two.add(new DefaultMutableTreeNode("7"));
two.add(new DefaultMutableTreeNode("8"));
two.add(new DefaultMutableTreeNode("9"));
setLayout(new BorderLayout());
JPanel jpanel=new JPanel();
jpanel.add(new JScrollPane().add(new JTree(new DefaultTreeModel(root))));
add(jpanel,BorderLayout.CENTER);
}
catch(Exception e) {
e.printStackTrace();
}
}
}
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import javax.swing.tree.*;
public class Applet1 extends Applet {
public void init(){
try{
DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
DefaultMutableTreeNode one = new DefaultMutableTreeNode("one");
DefaultMutableTreeNode two = new DefaultMutableTreeNode("two");
DefaultMutableTreeNode three = new DefaultMutableTreeNode("three");
one.add(new DefaultMutableTreeNode("1"));
one.add(new DefaultMutableTreeNode("2"));
one.add(new DefaultMutableTreeNode("3"));
one.add(new DefaultMutableTreeNode("4"));
one.add(new DefaultMutableTreeNode("5"));
one.add(new DefaultMutableTreeNode("6"));
one.add(new DefaultMutableTreeNode("7"));
root.add(one);
root.add(two);
root.add(three);
root.add(new DefaultMutableTreeNode("4"));
root.add(new DefaultMutableTreeNode("5"));
root.add(new DefaultMutableTreeNode("6"));
root.add(new DefaultMutableTreeNode("7"));
root.add(new DefaultMutableTreeNode("8"));
root.add(new DefaultMutableTreeNode("9"));
two.add(new DefaultMutableTreeNode("4"));
two.add(new DefaultMutableTreeNode("5"));
two.add(new DefaultMutableTreeNode("6"));
two.add(new DefaultMutableTreeNode("7"));
two.add(new DefaultMutableTreeNode("8"));
two.add(new DefaultMutableTreeNode("9"));
setLayout(new BorderLayout());
JPanel jpanel=new JPanel();
jpanel.add(new JScrollPane().add(new JTree(new DefaultTreeModel(root))));
add(jpanel,BorderLayout.CENTER);
}
catch(Exception e) {
e.printStackTrace();
}
}
}
|
你这个问题我正好遇到过,算你走运了! ^_^
若不显示,你想这样加入。
jScrollPane3.getViewport().add(jTextArea3, null);
试试看应该可以的。
good luck!
若不显示,你想这样加入。
jScrollPane3.getViewport().add(jTextArea3, null);
试试看应该可以的。
good luck!
|
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import javax.swing.tree.*;
public class Applet1 extends JFrame {
public Applet1(){
try{
DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
DefaultMutableTreeNode one = new DefaultMutableTreeNode("one");
DefaultMutableTreeNode two = new DefaultMutableTreeNode("two");
DefaultMutableTreeNode three = new DefaultMutableTreeNode("three");
one.add(new DefaultMutableTreeNode("1"));
one.add(new DefaultMutableTreeNode("2"));
one.add(new DefaultMutableTreeNode("3"));
one.add(new DefaultMutableTreeNode("4"));
one.add(new DefaultMutableTreeNode("5"));
one.add(new DefaultMutableTreeNode("6"));
one.add(new DefaultMutableTreeNode("7"));
root.add(one);
root.add(two);
root.add(three);
root.add(new DefaultMutableTreeNode("4"));
root.add(new DefaultMutableTreeNode("5"));
root.add(new DefaultMutableTreeNode("6"));
root.add(new DefaultMutableTreeNode("7"));
root.add(new DefaultMutableTreeNode("8"));
root.add(new DefaultMutableTreeNode("9"));
two.add(new DefaultMutableTreeNode("4"));
two.add(new DefaultMutableTreeNode("5"));
two.add(new DefaultMutableTreeNode("6"));
two.add(new DefaultMutableTreeNode("7"));
two.add(new DefaultMutableTreeNode("8"));
two.add(new DefaultMutableTreeNode("9"));
getContentPane().setLayout(new BorderLayout());
JPanel jpanel=new JPanel();
jpanel.setLayout(new BorderLayout());
jpanel.add(new JScrollPane(new JTree(new DefaultTreeModel(root))),BorderLayout.CENTER);
getContentPane().add(jpanel,BorderLayout.CENTER);
}
catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args)
{
Applet1 a = new Applet1();
a.setSize(400,300);
a.setVisible(true);
}
}
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import javax.swing.tree.*;
public class Applet1 extends JFrame {
public Applet1(){
try{
DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
DefaultMutableTreeNode one = new DefaultMutableTreeNode("one");
DefaultMutableTreeNode two = new DefaultMutableTreeNode("two");
DefaultMutableTreeNode three = new DefaultMutableTreeNode("three");
one.add(new DefaultMutableTreeNode("1"));
one.add(new DefaultMutableTreeNode("2"));
one.add(new DefaultMutableTreeNode("3"));
one.add(new DefaultMutableTreeNode("4"));
one.add(new DefaultMutableTreeNode("5"));
one.add(new DefaultMutableTreeNode("6"));
one.add(new DefaultMutableTreeNode("7"));
root.add(one);
root.add(two);
root.add(three);
root.add(new DefaultMutableTreeNode("4"));
root.add(new DefaultMutableTreeNode("5"));
root.add(new DefaultMutableTreeNode("6"));
root.add(new DefaultMutableTreeNode("7"));
root.add(new DefaultMutableTreeNode("8"));
root.add(new DefaultMutableTreeNode("9"));
two.add(new DefaultMutableTreeNode("4"));
two.add(new DefaultMutableTreeNode("5"));
two.add(new DefaultMutableTreeNode("6"));
two.add(new DefaultMutableTreeNode("7"));
two.add(new DefaultMutableTreeNode("8"));
two.add(new DefaultMutableTreeNode("9"));
getContentPane().setLayout(new BorderLayout());
JPanel jpanel=new JPanel();
jpanel.setLayout(new BorderLayout());
jpanel.add(new JScrollPane(new JTree(new DefaultTreeModel(root))),BorderLayout.CENTER);
getContentPane().add(jpanel,BorderLayout.CENTER);
}
catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args)
{
Applet1 a = new Applet1();
a.setSize(400,300);
a.setVisible(true);
}
}