setBounds(0, 0, 400, 300); setDefaultCloseOperation(DISPOSE_ON_CLOSE); addWindowListener(new WindowHandler()); // Create labels for input fields JLabel dbURLLabel = new JLabel( "Database URL: "); dbURLLabel.setDisplayedMnemonic('D'); // Set accelerator dbURLLabel.setLabelFor(database); // Connect label to field database.setFocusAccelerator('D'); // Identify accelerator to field JLabel userIDLabel = new JLabel( "User ID:", JLabel.RIGHT); userIDLabel.setDisplayedMnemonic('U'); // Set accelerator userIDLabel.setLabelFor(userIDInput); // Connect label to field userIDInput.setFocusAccelerator('U'); // Identify accelerator to field userIDLabel.setPreferredSize(dbURLLabel.getPreferredSize()); // Set same size JLabel passwordLabel = new JLabel("Password: "); passwordLabel.setDisplayedMnemonic('P'); // Set accelerator passwordLabel.setLabelFor(passwordInput); // Connect label to field passwordInput.setFocusAccelerator('P'); // Identify accelerator to field // Box for database URL input Box dbPane = Box.createHorizontalBox(); dbPane.add(dbURLLabel); dbPane.add(database); // Box for user ID and password input fields Box loginPane = Box.createHorizontalBox(); loginPane.add(userIDLabel); loginPane.add(userIDInput); loginPane.add(passwordLabel); loginPane.add(passwordInput);
Box inputPane = Box.createVerticalBox(); inputPane.add(dbPane); inputPane.add(loginPane); getContentPane().add(inputPane, BorderLayout.NORTH); // Add message area status.setText("Enter a database URL and/or press Enter"); status.setEditable(false); // No user input status.setLineWrap(true); // Lines wrap status.setWrapStyleWord(true); // on word boundaries status.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED)); getContentPane().add(status, BorderLayout.SOUTH); // Create tree to go in left split pane dbNode = new DefaultMutableTreeNode("No database"); dbTreeModel = new DefaultTreeModel(dbNode); dbTree = new JTree(dbTreeModel); treePane = new JScrollPane(dbTree); treePane.setBorder(BorderFactory.createLineBorder(Color.darkGray)); // Create table to go in right split pane tableModel = new ResultsModel(); JTable table = new JTable(tableModel); table.setPreferredSize(new Dimension(800,400)); table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); tablePane = new JScrollPane(table); tablePane.setBorder(BorderFactory.createLineBorder(Color.darkGray)); JSplitPane splitpane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, // Continuous relayout treePane, // Left pane content tablePane); // Right pane content getContentPane().add(splitpane, BorderLayout.CENTER); splitpane.setDividerLocation(150); // Left pane 150 pixels wide
wrox还出了一本《java专业编程指南》,里面讲的树的例子也不少,你可以去下载源码看看。
www.wrox.com
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
import javax.swing.tree.*;
import javax.swing.table.*;
import java.sql.*;class DatabaseBrowse extends JFrame implements ActionListener, TreeSelectionListener
{
public static void main(String[] args)
{
DatabaseBrowse theApp = new DatabaseBrowse(); // Create application object
} public DatabaseBrowse()
{
super("Database Browser");
setBounds(0, 0, 400, 300);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
addWindowListener(new WindowHandler()); // Create labels for input fields
JLabel dbURLLabel = new JLabel( "Database URL: ");
dbURLLabel.setDisplayedMnemonic('D'); // Set accelerator
dbURLLabel.setLabelFor(database); // Connect label to field
database.setFocusAccelerator('D'); // Identify accelerator to field JLabel userIDLabel = new JLabel( "User ID:", JLabel.RIGHT);
userIDLabel.setDisplayedMnemonic('U'); // Set accelerator
userIDLabel.setLabelFor(userIDInput); // Connect label to field
userIDInput.setFocusAccelerator('U'); // Identify accelerator to field userIDLabel.setPreferredSize(dbURLLabel.getPreferredSize()); // Set same size
JLabel passwordLabel = new JLabel("Password: ");
passwordLabel.setDisplayedMnemonic('P'); // Set accelerator
passwordLabel.setLabelFor(passwordInput); // Connect label to field
passwordInput.setFocusAccelerator('P'); // Identify accelerator to field
// Box for database URL input
Box dbPane = Box.createHorizontalBox();
dbPane.add(dbURLLabel);
dbPane.add(database); // Box for user ID and password input fields
Box loginPane = Box.createHorizontalBox();
loginPane.add(userIDLabel);
loginPane.add(userIDInput);
loginPane.add(passwordLabel);
loginPane.add(passwordInput);
Box inputPane = Box.createVerticalBox();
inputPane.add(dbPane);
inputPane.add(loginPane);
getContentPane().add(inputPane, BorderLayout.NORTH); // Add message area
status.setText("Enter a database URL and/or press Enter");
status.setEditable(false); // No user input
status.setLineWrap(true); // Lines wrap
status.setWrapStyleWord(true); // on word boundaries
status.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
getContentPane().add(status, BorderLayout.SOUTH); // Create tree to go in left split pane
dbNode = new DefaultMutableTreeNode("No database");
dbTreeModel = new DefaultTreeModel(dbNode);
dbTree = new JTree(dbTreeModel);
treePane = new JScrollPane(dbTree);
treePane.setBorder(BorderFactory.createLineBorder(Color.darkGray));
// Create table to go in right split pane
tableModel = new ResultsModel();
JTable table = new JTable(tableModel);
table.setPreferredSize(new Dimension(800,400));
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
tablePane = new JScrollPane(table);
tablePane.setBorder(BorderFactory.createLineBorder(Color.darkGray));
JSplitPane splitpane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
true, // Continuous relayout
treePane, // Left pane content
tablePane); // Right pane content
getContentPane().add(splitpane, BorderLayout.CENTER);
splitpane.setDividerLocation(150); // Left pane 150 pixels wide
// Add event listeners
database.addActionListener(this);
userIDInput.addActionListener(this);
passwordInput.addActionListener(this);
dbTree.addTreeSelectionListener(this);
pack();
setVisible(true); // Set window visible
show(); // Display the window
database.requestFocus(); // Focus to the url input field
for(int i = 0 ; i < drivers.length ; i++)
try
{
Class.forName(drivers[i]);
}
catch(ClassNotFoundException cnfe)
{
System.err.println(cnfe);
status.setText("Driver load failed: " + cnfe.getMessage());
} } private String userID = "guest";
private String password = "guest";
private String url = "jdbc:odbc:technical_library"; Connection connection;
Statement statement;
private JTextField database = new JTextField(url);
private JTextField userIDInput = new JTextField(userID);
private JPasswordField passwordInput = new JPasswordField(password);
private JTextArea status = new JTextArea(3,30); private DefaultMutableTreeNode dbNode; // Root node for the database tree
private DefaultTreeModel dbTreeModel; // Model for the database metadata
private JTree dbTree; // Tree to display the metadata
private JScrollPane treePane; // Scroll pane holding the tree ResultsModel tableModel; // Model for table
private JTable table; // Table holding table data
JScrollPane tablePane; // Scroll pane holding the table private String[] drivers = {
"sun.jdbc.odbc.JdbcOdbcDriver", // ODBC bridge
"com.imaginary.sql.msql.MsqlDriver" // mSQL driver
};
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource(); // Get source of the event
if(source == database || // If its URL input,
source == userIDInput || // or userID input,
source == passwordInput) // or password input,
{ // we will try for a connection
url = database.getText(); // Get database URL
userID = userIDInput.getText(); // Get user ID char[] pw = passwordInput.getPassword(); // Get password
if(pw != null)
password = new String(pw); if(url == null || url.length()==0)
{
status.setText("Please specify a database URL ");
return;
}
openConnection();
password = null; // For security
}
}
去www.wrox.com找一找示便源码下载。
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;class OidNode
{
public int m_id;
private String m_name;
public OidNode(int id,String name)
{
m_id=id;
m_name=name;
}
public int getId()
{
return m_id;
}
public String getName()
{
return m_name;
}
public String toString()
{
return m_name;
}
}public class swingtree
{
JFrame frmMain=new JFrame("JTree");
JTree treeTree1;
JScrollPane pane;
Container c=frmMain.getContentPane();
public swingtree()
{
DefaultMutableTreeNode parentcopy;
DefaultMutableTreeNode top=new DefaultMutableTreeNode(new OidNode(1,"C:\\"));
DefaultMutableTreeNode parent=top;
DefaultMutableTreeNode node=new DefaultMutableTreeNode(new OidNode(0,"Winnt"));
parent.add(node);
parentcopy=parent;
parent=node;
node=new DefaultMutableTreeNode(new OidNode(13,"System32"));
parent.add(node);
node=new DefaultMutableTreeNode(new OidNode(14,"Config"));
parent.add(node);
node=new DefaultMutableTreeNode(new OidNode(15,"Fonts"));
parent.add(node);
node=new DefaultMutableTreeNode(new OidNode(16,"Help"));
parent.add(node);
parent=node;
node=new DefaultMutableTreeNode(new OidNode(17,"News"));
parent.add(node);
node=new DefaultMutableTreeNode(new OidNode(18,"Mail"));
parent.add(node);
parent=parentcopy;
node=new DefaultMutableTreeNode(new OidNode(2,"Program File"));
parent.add(node);
parentcopy=parent;
parent=node;
node=new DefaultMutableTreeNode(new OidNode(6,"MicroSoft Office XP"));
parent.add(node);
node=new DefaultMutableTreeNode(new OidNode(7,"Visual Studio.net"));
parent.add(node);
node=new DefaultMutableTreeNode(new OidNode(8,"Winamp"));
parent.add(node);
node=new DefaultMutableTreeNode(new OidNode(9,"ACD See 3.0"));
parent.add(node);
node=new DefaultMutableTreeNode(new OidNode(10,"Windows Media"));
parent.add(node);
parent=parentcopy;
node=new DefaultMutableTreeNode(new OidNode(3,"My Document"));
parent.add(node);
parentcopy=parent;
parent=node;
node=new DefaultMutableTreeNode(new OidNode(4,"My Picture"));
parent.add(node);
node=new DefaultMutableTreeNode(new OidNode(5,"My Picuture"));
parent.add(node);
parent=parentcopy;
node=new DefaultMutableTreeNode(new OidNode(11,"Inetpub"));
parent.add(node);
parentcopy=parent;
parent=node;
node=new DefaultMutableTreeNode(new OidNode(19,"AdminScrip"));
parent.add(node);
node=new DefaultMutableTreeNode(new OidNode(20,"ftproot"));
parent.add(node);
node=new DefaultMutableTreeNode(new OidNode(21,"wwwroot"));
parent.add(node);
parent=parentcopy; node=new DefaultMutableTreeNode(new OidNode(12,"JDK"));
parent.add(node);
parentcopy=parent;
parent=node;
node=new DefaultMutableTreeNode(new OidNode(22,"bin"));
parent.add(node);
node=new DefaultMutableTreeNode(new OidNode(23,"demo"));
parent.add(node);
node=new DefaultMutableTreeNode(new OidNode(24,"include"));
parent.add(node);
node=new DefaultMutableTreeNode(new OidNode(25,"jre"));
parent.add(node);
node=new DefaultMutableTreeNode(new OidNode(26,"lib"));
parent.add(node);
parent=parentcopy;
node=new DefaultMutableTreeNode(new OidNode(27,"Autoexec.bat"));
parent.add(node);
node=new DefaultMutableTreeNode(new OidNode(28,"Config.sys"));
parent.add(node);
node=new DefaultMutableTreeNode(new OidNode(29,"File0001"));
parent.add(node);
node=new DefaultMutableTreeNode(new OidNode(30,"File0002"));
parent.add(node);
treeTree1=new JTree(top);
treeTree1.setShowsRootHandles(true);
treeTree1.putClientProperty("JTree.lineStyle","Angled");
treeTree1.setEditable(false);
pane=new JScrollPane(treeTree1);
c.add("Center",pane);
/* frmMain.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
*/
frmMain.setSize(300,200);
frmMain.setVisible(true);
}
public static void main(String[] args)
{
new swingtree();
}
}