为什么不能获得焦点后,再单击就出来。package my.jfcbook.chapter11;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;public class Frame1 extends JFrame {
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
JScrollPane jScrollPane1 = new JScrollPane();
JTree tree = new TreeExample1(); //Construct the frame
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
} //Component initialization
private void jbInit() throws Exception {
String[] people={"汪峰","窦唯","黑豹","绿洲","子孙"};
JComboBox combo=new JComboBox(people);
tree.setCellEditor(new DefaultTreeCellEditor(
tree,null,new DefaultCellEditor(combo)
));
tree.setEditable(true);
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");
contentPane.add(jScrollPane1, BorderLayout.CENTER);
jScrollPane1.getViewport().add(tree, null);
} //Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
public static void main(String[] args) {
Frame1 frame=new Frame1();
frame.setSize(400,250);
frame.show();
}
}class TreeExample1 extends JTree {
public TreeExample1() {
DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode();
DefaultMutableTreeNode apolloNode =
new DefaultMutableTreeNode("Apollo");
rootNode.add(apolloNode);
DefaultMutableTreeNode skylabNode =
new DefaultMutableTreeNode("Skylab");
rootNode.add(skylabNode); DefaultMutableTreeNode n =
new DefaultMutableTreeNode("11");
apolloNode.add(n);
n.add(new DefaultMutableTreeNode("Neil Armstrong"));
n.add(new DefaultMutableTreeNode("Buzz Aldrin"));
n.add(new DefaultMutableTreeNode("Michael Collins")); n = new DefaultMutableTreeNode("12");
apolloNode.add(n);
n.add(new DefaultMutableTreeNode("Pete Conrad"));
n.add(new DefaultMutableTreeNode("Alan Bean"));
n.add(new DefaultMutableTreeNode("Richard Gordon")); n = new DefaultMutableTreeNode("13");
apolloNode.add(n);
n.add(new DefaultMutableTreeNode("James Lovell"));
n.add(new DefaultMutableTreeNode("Fred Haise"));
n.add(new DefaultMutableTreeNode("Jack Swigert")); n = new DefaultMutableTreeNode("14");
apolloNode.add(n);
n.add(new DefaultMutableTreeNode("Alan Shephard"));
n.add(new DefaultMutableTreeNode("Edgar Mitchell"));
n.add(new DefaultMutableTreeNode("Stuart Roosa"));
n = new DefaultMutableTreeNode("15");
apolloNode.add(n);
n.add(new DefaultMutableTreeNode("Dave Scott"));
n.add(new DefaultMutableTreeNode("Jim Irwin"));
n.add(new DefaultMutableTreeNode("Al Worden")); n = new DefaultMutableTreeNode("16");
apolloNode.add(n);
n.add(new DefaultMutableTreeNode("John Young"));
n.add(new DefaultMutableTreeNode("Charlie Duke"));
n.add(new DefaultMutableTreeNode("Ken Mattingly")); n = new DefaultMutableTreeNode("17");
apolloNode.add(n);
n.add(new DefaultMutableTreeNode("Eugene Cernan"));
n.add(new DefaultMutableTreeNode("Harrison Schmidt"));
n.add(new DefaultMutableTreeNode("Ron Evans")); n = new DefaultMutableTreeNode("2");
skylabNode.add(n);
n.add(new DefaultMutableTreeNode("Pete Conrad"));
n.add(new DefaultMutableTreeNode("Joseph Kerwin"));
n.add(new DefaultMutableTreeNode("Paul Weitz")); n = new DefaultMutableTreeNode("3");
skylabNode.add(n);
n.add(new DefaultMutableTreeNode("Alan Bean"));
n.add(new DefaultMutableTreeNode("Owen Garriott"));
n.add(new DefaultMutableTreeNode("Jack Lousma"));
n = new DefaultMutableTreeNode("4");
skylabNode.add(n);
n.add(new DefaultMutableTreeNode("Gerald Carr"));
n.add(new DefaultMutableTreeNode("Edward Gibson"));
n.add(new DefaultMutableTreeNode("William Pogue")); this.setModel(new DefaultTreeModel(rootNode));
} public static void main(String[] args) {
JFrame f = new JFrame("Tree Example 1");
TreeExample1 t = new TreeExample1();
t.putClientProperty("JTree.lineStyle", "Angled");
t.expandRow(0); f.getContentPane().add(new JScrollPane(t));
f.setSize(300, 300);
f.setVisible(true);
}}
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;public class Frame1 extends JFrame {
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
JScrollPane jScrollPane1 = new JScrollPane();
JTree tree = new TreeExample1(); //Construct the frame
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
} //Component initialization
private void jbInit() throws Exception {
String[] people={"汪峰","窦唯","黑豹","绿洲","子孙"};
JComboBox combo=new JComboBox(people);
tree.setCellEditor(new DefaultTreeCellEditor(
tree,null,new DefaultCellEditor(combo)
));
tree.setEditable(true);
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");
contentPane.add(jScrollPane1, BorderLayout.CENTER);
jScrollPane1.getViewport().add(tree, null);
} //Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
public static void main(String[] args) {
Frame1 frame=new Frame1();
frame.setSize(400,250);
frame.show();
}
}class TreeExample1 extends JTree {
public TreeExample1() {
DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode();
DefaultMutableTreeNode apolloNode =
new DefaultMutableTreeNode("Apollo");
rootNode.add(apolloNode);
DefaultMutableTreeNode skylabNode =
new DefaultMutableTreeNode("Skylab");
rootNode.add(skylabNode); DefaultMutableTreeNode n =
new DefaultMutableTreeNode("11");
apolloNode.add(n);
n.add(new DefaultMutableTreeNode("Neil Armstrong"));
n.add(new DefaultMutableTreeNode("Buzz Aldrin"));
n.add(new DefaultMutableTreeNode("Michael Collins")); n = new DefaultMutableTreeNode("12");
apolloNode.add(n);
n.add(new DefaultMutableTreeNode("Pete Conrad"));
n.add(new DefaultMutableTreeNode("Alan Bean"));
n.add(new DefaultMutableTreeNode("Richard Gordon")); n = new DefaultMutableTreeNode("13");
apolloNode.add(n);
n.add(new DefaultMutableTreeNode("James Lovell"));
n.add(new DefaultMutableTreeNode("Fred Haise"));
n.add(new DefaultMutableTreeNode("Jack Swigert")); n = new DefaultMutableTreeNode("14");
apolloNode.add(n);
n.add(new DefaultMutableTreeNode("Alan Shephard"));
n.add(new DefaultMutableTreeNode("Edgar Mitchell"));
n.add(new DefaultMutableTreeNode("Stuart Roosa"));
n = new DefaultMutableTreeNode("15");
apolloNode.add(n);
n.add(new DefaultMutableTreeNode("Dave Scott"));
n.add(new DefaultMutableTreeNode("Jim Irwin"));
n.add(new DefaultMutableTreeNode("Al Worden")); n = new DefaultMutableTreeNode("16");
apolloNode.add(n);
n.add(new DefaultMutableTreeNode("John Young"));
n.add(new DefaultMutableTreeNode("Charlie Duke"));
n.add(new DefaultMutableTreeNode("Ken Mattingly")); n = new DefaultMutableTreeNode("17");
apolloNode.add(n);
n.add(new DefaultMutableTreeNode("Eugene Cernan"));
n.add(new DefaultMutableTreeNode("Harrison Schmidt"));
n.add(new DefaultMutableTreeNode("Ron Evans")); n = new DefaultMutableTreeNode("2");
skylabNode.add(n);
n.add(new DefaultMutableTreeNode("Pete Conrad"));
n.add(new DefaultMutableTreeNode("Joseph Kerwin"));
n.add(new DefaultMutableTreeNode("Paul Weitz")); n = new DefaultMutableTreeNode("3");
skylabNode.add(n);
n.add(new DefaultMutableTreeNode("Alan Bean"));
n.add(new DefaultMutableTreeNode("Owen Garriott"));
n.add(new DefaultMutableTreeNode("Jack Lousma"));
n = new DefaultMutableTreeNode("4");
skylabNode.add(n);
n.add(new DefaultMutableTreeNode("Gerald Carr"));
n.add(new DefaultMutableTreeNode("Edward Gibson"));
n.add(new DefaultMutableTreeNode("William Pogue")); this.setModel(new DefaultTreeModel(rootNode));
} public static void main(String[] args) {
JFrame f = new JFrame("Tree Example 1");
TreeExample1 t = new TreeExample1();
t.putClientProperty("JTree.lineStyle", "Angled");
t.expandRow(0); f.getContentPane().add(new JScrollPane(t));
f.setSize(300, 300);
f.setVisible(true);
}}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货