JLabel控件使用方法

来源:互联网 发布:数据结构与算法分析pdf 编辑:程序博客网 时间:2024/06/03 05:51
JLabel 对象可以显示文本、图像或同时显示二者。可以通过设置垂直和水平对齐方式,指定标签显示区中标签内容在何处对齐。默认情况下,标签在其显示区内垂直居中对齐。默认情况下,只显示文本的标签是开始边对齐;而只显示图像的标签则水平居中对齐。

还可以指定文本相对于图像的位置。默认情况下,文本位于图像的结尾边上,文本和图像都垂直对齐。

根据标签的 ComponentOrientation 属性值确定其开始边和结尾边。目前,默认的 ComponentOrientation 设置将开始边映射到左边,将结尾边映射到右边。

最后,还可以使用 setIconTextGap 方法指定文本和图像之间应该出现多少像素。默认情况下为 4 个像素。
构造方法摘要
JLabel()
          创建无图像并且其标题为空字符串的 JLabel。
JLabel(Icon image)
          创建具有指定图像的 JLabel 实例。
JLabel(Icon image, int horizontalAlignment)
          创建具有指定图像和水平对齐方式的 JLabel 实例。
JLabel(String text)
          创建具有指定文本的 JLabel 实例。
JLabel(String text, Icon icon, int horizontalAlignment)
          创建具有指定文本、图像和水平对齐方式的 JLabel 实例。
JLabel(String text, int horizontalAlignment)
          创建具有指定文本和水平对齐方式的 JLabel 实例。
方法摘要


 String getText()
          返回该标签所显示的文本字符串。
 void setText(String text)
          定义此组件将要显示的单行文本。
 Icon getIcon()
          返回该标签显示的图形图像(字形、图标)。
 void setIcon(Icon icon)
          定义此组件将要显示的图标。
对于此控件的方法,在这里就我们就介绍这几种,其余的请大家参考JDK文档,下面我们就来通过一个范例来看一个JLabel的用法。
 代码如下:
import java.awt.*;
import javax.swing.*;
/*<html>
  <head>
   <title>JLable test</title>
  </head>
  <body>
    <applet height="400" width="650" code="Jlabel.class">
     
    </applet>
  </body>
</html>*/
public class Jlabel extends JApplet{
 public void init(){
  Container con=this.getContentPane();//增加一个内容窗格

  ImageIcon icon=new ImageIcon("h.jpg");

  JLabel label=new JLabel("hebe",icon,JLabel.CENTER);

     con.add(label);
 }
}
运行结果为:

 

JTextField的用法:
JTextField 是一个轻量级组件,它允许编辑单行文本。有关使用文本字段的信息和示例,请参阅《The Java Tutorial》中的 How to Use Text Fields 一节。

JTextField 应与 java.awt.TextField 具有源代码兼容性,理应如此。此组件具有 java.awt.TextField 类中没有的功能。有关其他功能,请参考超类。

JTextField 具有建立字符串的方法,此字符串用作针对被激发的操作事件的命令字符串。java.awt.TextField 把字段文本用作针对 ActionEvent 的命令字符串。如果通过 setActionCommand 方法设置的命令字符串不为 null,则 JTextField 将使用该字符串来保持与 java.awt.TextField 的兼容性,否则将使用字段文本来保持兼容性。

setEchoChar 和 getEchoChar 方法不是直接提供的,以避免可插入的外观的新实现意外公开密码字符。为了提供类似密码的服务,单独的类 JPasswordField 扩展了 JTextField,从而通过可插入外观独立地提供此服务。

通过添加 TextEvent 的 TextListener,可以监视 java.awt.TextField 的更改。在基于 JTextComponent 的组件中,通过 DocumentEvent 将更改从模型传播到 DocumentListeners。DocumentEvent 给出了更改的位置和更改种类(如果需要)。代码片段可能看起来如下所示:


     DocumentListener myListener = ??;
     JTextField myArea = ??;
     myArea.getDocument().addDocumentListener(myListener);
 JTextField 的水平对齐方式可以设置为左对齐、前端对齐、居中对齐、右对齐或尾部对齐。右对齐/尾部对齐在所需的字段文本尺寸小于为它分配的尺寸时使用。这是由 setHorizontalAlignment 和 getHorizontalAlignment 方法确定的。默认情况下为前端对齐。

文本字段如何使用 VK_ENTER 事件取决于文本字段是否具有任何操作侦听器。如果具有操作侦听器,则 VK_ENTER 导致侦听器获取一个 ActionEvent,并使用 VK_ENTER 事件。这与 AWT 文本字段处理 VK_ENTER 事件的方式是兼容的。如果文本字段没有操作侦听器,则从 1.3 版本开始不使用 VK_ENTER 事件。而是处理祖先组件的绑定,这将启用 JFC/Swing 的默认按钮特性。


构造方法摘要
JTextField()
          构造一个新的 TextField。
JTextField(Document doc, String text, int columns)
          构造一个新的 JTextField,它使用给定文本存储模型和给定的列数。
JTextField(int columns)
          构造一个具有指定列数的新的空 TextField。
JTextField(String text)
          构造一个用指定文本初始化的新 TextField。
JTextField(String text, int columns)
          构造一个用指定文本和列初始化的新 TextField。
方法摘要 
 void setFont(Font f)
          设置当前字体。
 void setHorizontalAlignment(int alignment)
          设置文本的水平对齐方式。
 void setScrollOffset(int scrollOffset)
          获取滚动偏移量(以像素为单位)。
对于其他的方法请大家参考JDK文档。下面我们就通过一个范例来看看JTextField的用法。
代码如下:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/*<html>
  <head>
   <title>textfield test</title>
  </head>
  <body>
   <applet height="400" width="400" code="Jtextfield.class">
    
   </applet>
  </body>
</html>*/
public class Jtextfield extends JApplet{
 JTextField text1,text2;
 JLabel label;
 public void init(){
  Container con=this.getContentPane();
  con.setLayout(new FlowLayout());
  label=new JLabel("please input some character,and pressed Enter");
  text1=new JTextField(20);
  text2=new JTextField(20);
  text2.setEditable(false);
  con.add(label);
  con.add(text1);
  con.add(text2);
  text1.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    text2.setText(text1.getText());
   }
  });
 }
}
运行结果为:

类 JButton

构造方法摘要
JButton()
          创建不带有设置文本或图标的按钮。
JButton(Action a)
          创建一个按钮,其属性从所提供的 Action 中获取。
JButton(Icon icon)
          创建一个带图标的按钮。
JButton(String text)
          创建一个带文本的按钮。
JButton(String text, Icon icon)
          创建一个带初始文本和图标的按钮。
方法摘要
protected  void configurePropertiesFromAction(Action a)
          该工厂方法根据 Action 实例中的值设置 AbstractButton 的属性。
 AccessibleContext getAccessibleContext()
          获得与此 JButton 关联的 AccessibleContext。
 String getUIClassID()
          返回指定呈现此组件的 L&F 类的类名,以字符串的形式。
 boolean isDefaultButton()
          获得 defaultButton 属性的值,如果为 true 则意味着此按钮是其 JRootPane 的当前默认按钮。
 boolean isDefaultCapable()
          获得 defaultCapable 属性的值。
protected  String paramString()
          返回此 JButton 的字符串表示形式。
 void removeNotify()
          重写 JComponent.removeNotify 来检查此按钮当前是否被设置为 RootPane 上的默认按钮,如果是,则将 RootPane 的默认按钮设置为 null,以确保 RootPane 不继续停留在无效的按钮引用上。
 void setDefaultCapable(boolean defaultCapable)
          设置 defaultCapable 属性,该属性确定此按钮是否可以是其根窗格的默认按钮。
 void updateUI()
          根据当前外观的值重置 UI 属性。
下面我们就来看一个范例:
代码如下:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*
 *<html>
 <head>
  <title>Jbutton test</title>
 </head>
 <body>
   <applet height="200" width="200" code="Jbutton.class">
    
   </applet>
 </body>
</html>
*/
public class Jbutton extends JApplet{
 JButton yes,no;
 JLabel l1;
 public void init(){
  yes=new JButton("yes");
  no=new JButton("no");
     l1=new JLabel("                                  ");
     Container con=this.getContentPane();
     con.setLayout(new FlowLayout());
     con.add(yes);
     con.add(no);
     con.add(l1);
     ActionListener action=new ActionListener(){
      public void actionPerformed(ActionEvent e){
       if(e.getActionCommand()=="yes"){
        l1.setText("you clicked the yes button");
       }else{
        l1.setText("you clicked the no button");
       }
      }
     };
     yes.addActionListener(action);
     no.addActionListener(action);
 }
}
运行结果为:

左边的为点“yes”按扭运行的结果,右边的为点“no”按扭的运行结果。

 

JCheckBox的用法

public class JCheckBoxextends JToggleButtonimplements Accessible复选框的实现,复选框是一个可以被选定和取消选定的项,它将其状态显示给用户。按照惯例,可以选定组中任意数量的复选框。
构造方法摘要
JCheckBox()
          创建一个没有文本、没有图标并且最初未被选定的复选框。
JCheckBox(Action a)
          创建一个复选框,其属性从所提供的 Action 获取。
JCheckBox(Icon icon)
          创建有一个图标、最初未被选定的复选框。
JCheckBox(Icon icon, boolean selected)
          创建一个带图标的复选框,并指定其最初是否处于选定状态。
JCheckBox(String text)
          创建一个带文本的、最初未被选定的复选框。
JCheckBox(String text, boolean selected)
          创建一个带文本的复选框,并指定其最初是否处于选定状态。
JCheckBox(String text, Icon icon)
          创建带有指定文本和图标的、最初未选定的复选框。
JCheckBox(String text, Icon icon, boolean selected)
          创建一个带文本和图标的复选框,并指定其最初是否处于选定状态。
方法摘要
protected  void configurePropertiesFromAction(Action a)
          该工厂方法根据 Action 实例的值设置 ActionEvent 源的属性。
protected  PropertyChangeListener createActionPropertyChangeListener(Action a)
          该工厂方法创建 PropertyChangeListener,在 Action 实例上进行属性更改时,用于更新 ActionEvent 源。
 AccessibleContext getAccessibleContext()
          获得与此 JCheckBox 关联的 AccessibleContext。
 String getUIClassID()
          返回指定呈现此组件的 L&F 类名的字符串。
 boolean isBorderPaintedFlat()
          获得 borderPaintedFlat 属性的值。
protected  String paramString()
          返回此 JCheckBox 的字符串表示形式。
 void setBorderPaintedFlat(boolean b)
          设置 borderPaintedFlat 属性,该属性为外观提供了关于复选框边框外观的提示。
 void updateUI()
          根据当前外观重置 UI 属性值。
下面我们就来看一个范例:
代码如下:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*
 *<html>
  <head>
    <title>
     Jcheckbox test
    </title>
  </head>
  <body>
   <applet height="300" width="300" code="Jcheckbox.class">
    
   </applet>
  </body>
</html>
*/
public class Jcheckbox extends JApplet{
 JCheckBox one,two,three;
 JPanel pane;
 public void init(){
  Container con=this.getContentPane();
        con.setLayout(new BorderLayout());
  one=new JCheckBox("music");
  two=new JCheckBox("sports");
  three=new JCheckBox("sleep");
       
        pane=new JPanel();
        pane.add(one);
        pane.add(two);
        pane.add(three);
       
        con.add(pane,BorderLayout.CENTER);

 }
}
运行结构为:

 

JRadioButton的用法:
public class JRadioButtonextends JToggleButtonimplements Accessible实现一个单选按钮,此按钮项可被选择或取消选择,并可为用户显示其状态。与 ButtonGroup 对象配合使用可创建一组按钮,一次只能选择其中的一个按钮。(创建一个 ButtonGroup 对象并用其 add 方法将 JRadioButton 对象包含在此组中。)

注:ButtonGroup 对象为逻辑分组,不是物理分组。要创建按钮面板,仍需要创建一个 JPanel 或类似的容器对象并将 Border 添加到其中以便将面板与周围的组件分开。
构造方法摘要
JRadioButton()
          创建一个初始化为未选择的单选按钮,其文本未设定。
JRadioButton(Action a)
          创建一个单选按钮,其属性来自提供的 Action。
JRadioButton(Icon icon)
          创建一个初始化为未选择的单选按钮,其具有指定的图像但无文本。
JRadioButton(Icon icon, boolean selected)
          创建一个具有指定图像和选择状态的单选按钮,但无文本。
JRadioButton(String text)
          创建一个具有指定文本的状态为未选择的单选按钮。
JRadioButton(String text, boolean selected)
          创建一个具有指定文本和选择状态的单选按钮。
JRadioButton(String text, Icon icon)
          创建一个具有指定的文本和图像并初始化为未选择的单选按钮。
JRadioButton(String text, Icon icon, boolean selected)
          创建一个具有指定的文本、图像和选择状态的单选按钮。
  方法摘要
protected  void configurePropertiesFromAction(Action a)
          该工厂方法根据 Action 实例中的值设置 ActionEvent 源的属性。
protected  PropertyChangeListener createActionPropertyChangeListener(Action a)
          该工厂方法创建 PropertyChangeListener,当属性在其 Action 实例中更改时,用于更新 ActionEvent 源。
 AccessibleContext getAccessibleContext()
          获取与此 JRadioButton 相关联的 AccessibleContext。
 String getUIClassID()
          返回呈现此组件的 L&F 类的名称。
protected  String paramString()
          返回此 JRadioButton 的字符串表示形式。
 void updateUI()
          将 UI 属性重置为当前外观对应的值。
下面我们就通过一个范例来看一下JRadioButton的用法:
代码如下:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*
 <html>
  <head>
     <title>
       JRadioButton test
     </title>
  </head>
  <body>
    <applet height="200" width="200" code="Jradiobutton.class"></applet>
  </body>
</html>
*/
public class Jradiobutton extends JApplet{
 JRadioButton one,two,three;
 JPanel pane=new JPanel();
 JLabel label;
 ButtonGroup group;
 public void init(){
  Container con=this.getContentPane();
  con.setLayout(new BorderLayout());
  one=new JRadioButton("sports");
  two=new JRadioButton("music");
  three=new JRadioButton("sleep");
  pane=new JPanel();
        group=new ButtonGroup();
        group.add(one);
        group.add(two);
        group.add(three);
        pane.add(one);
        pane.add(two);
        pane.add(three);
  label=new JLabel("                                      ");
   ActionListener listener=new ActionListener(){
      public void actionPerformed(ActionEvent e){
       label.setText(e.getActionCommand());
      }
     };
  
  pane.add(one);
  pane.add(two);
  pane.add(three);
  
  one.addActionListener(listener);
  two.addActionListener(listener);
  three.addActionListener(listener);
  con.add(pane,BorderLayout.SOUTH);
  con.add(label,BorderLayout.CENTER);
  
 }
}
运行结果如下:当我们点不同的Radio,就会显示不同的Radio内容。

 

类 JComboBox

 

public class JComboBoxextends JComponentimplements ItemSelectable, ListDataListener, ActionListener, Accessible将按钮或可编辑字段与下拉列表组合的组件。用户可以从下拉列表中选择值,下拉列表在用户请求时显示。如果使组合框处于可编辑状态,则组合框将包括用户可在其中键入值的可编辑字段。
构造方法摘要
JComboBox()
          创建具有默认数据模型的 JComboBox。
JComboBox(ComboBoxModel aModel)
          创建一个 JComboBox,其项取自现有的 ComboBoxModel 中。
JComboBox(Object[] items)
          创建包含指定数组中的元素的 JComboBox。
JComboBox(Vector<?> items)
          创建包含指定 Vector 中的元素的 JComboBox。

方法摘要
 void actionPerformed(ActionEvent e)
          此方法作为实现的副作用存在的公共方法。
 void addActionListener(ActionListener l)
          添加 ActionListener。
 void addItem(Object anObject)
          为项列表添加项。
 void addItemListener(ItemListener aListener)
          添加 ItemListener。
对与此控件的方法在这里我们就介绍这些,请大家参考JDK文档。下面我们就来看一个范例 :
代码如下:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*
 *<html>
  <head>
    <title>
     JComboBox test
    </title>
  </head>
  <body>
   <applet height="300" width="300" code="Jcombobox.class">
    
   </applet>
  </body>
</html>
*/
public class Jcombobox extends JApplet{
 JLabel label;
 JComboBox combox;
 public void init(){
  Container con=this.getContentPane();
  con.setLayout(new BorderLayout());
  label=new JLabel("                                  ");
  combox=new JComboBox();
  combox.addItem("sports");
  combox.addItem("sleep");
  combox.addItem("music");
  
  ItemListener listener=new ItemListener(){
   public void itemStateChanged(ItemEvent e){
      label.setText("you selected-->"+e.getItem()); 
   }
  };
  combox.addItemListener(listener);
  con.add(combox,BorderLayout.SOUTH);
  con.add(label,BorderLayout.CENTER);
  
 }
}
运行结果:

类 JTabbedPane

一个组件,它允许用户通过单击具有给定标题和/或图标的选项卡,在一组组件之间进行切换。有关使用选项卡窗格的例子和信息,请参阅《The Java Tutorial》 中的 How to Use Tabbed Panes 一节。

通过使用 addTab 和 insertTab 方法将选项卡/组件添加到 TabbedPane 对象中。选项卡通过对应于添加位置的索引来表示,其中第一个选项卡的索引为 0,最后一个选项卡的索引为选项卡数减 1。

TabbedPane 使用 SingleSelectionModel 来表示选项卡索引集和当前所选择的索引。如果选项卡数大于 0,则总会有一个被选定的索引,此索引默认被初始化为第一个选项卡。如果选项卡数为 0,则所选择的索引为 -1。

构造方法摘要
JTabbedPane()
          创建一个具有默认的 JTabbedPane.TOP 选项卡布局的空 TabbedPane。
JTabbedPane(int tabPlacement)
          创建一个空的 TabbedPane,使其具有以下指定选项卡布局中的一种:JTabbedPane.TOP、JTabbedPane.BOTTOM、JTabbedPane.LEFT 或 JTabbedPane.RIGHT。
JTabbedPane(int tabPlacement, int tabLayoutPolicy)
          创建一个空的 TabbedPane,使其具有指定的选项卡布局和选项卡布局策略。
  方法摘要

 void addTab(String title, Component component)
          添加一个由 title 表示,且没有图标的 component。
 void addTab(String title, Icon icon, Component component)
          添加一个由 title 和/或 icon 表示的 component,其任意一个都可以为 null。
 void addTab(String title, Icon icon, Component component, String tip)
          添加由 title 和/或 icon 表示的 component 和 tip,其中任意一个都可以为 null。
 void setModel(SingleSelectionModel model)
          设置要用于此选项卡窗格的模型。
在小应用程序中使用选项窗格的一般过程为:
1。创建JTabbedPane对象。
2。调用addTab()方法在窗格中增加一个标签,(Component add(String title, Component component) 添加具有指定选项卡标题的 component。)
3。重复步骤2,增加标签
4。将选项窗格加入小应用程序的内容窗格。
下面我们就来通过一个范例来说明此控件的用法。
代码如下:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*
 *<html>
  <head>
    <title>JTabbedPane test</title>
  </head>
  <body>
   <applet height="300" width="300" code="JtabbedPane.class">
    
   </applet>
  </body>
</html>
*/
public class JtabbedPane extends JApplet{
 public void init(){
  JTabbedPane tab=new JTabbedPane();
  tab.addTab("colors",new ColorPanel());
  tab.addTab("sports",new SportPanel());
  
  this.getContentPane().add(tab);
 }
}
class ColorPanel extends JPanel{
 public ColorPanel(){
  JButton b1=new JButton("Red");
  JButton b2=new JButton("Blue");
  JButton b3=new JButton("Black");
  
  this.add(b1);
  this.add(b2);
  this.add(b3);
 }
}

class SportPanel extends JPanel{
 public SportPanel(){
  JRadioButton rb1=new JRadioButton("basketball");
  JRadioButton rb2=new JRadioButton("football");
  JRadioButton rb3=new JRadioButton("baseball");
  
  ButtonGroup group=new ButtonGroup();
  group.add(rb1);
  group.add(rb2);
  group.add(rb3);
  
  this.add(rb1);
  this.add(rb2);
  this.add(rb3);
  
 }
}
运行结果:

类 JTree


树中特定的节点可以由 TreePath(封装节点及其所有祖先的对象)标识,或由其显示行(其中显示区域中的每一行都显示一个节点)标识。展开 节点是一个非叶节点(由返回 false 的 TreeModel.isLeaf(node) 标识),当展开 其所有祖先时,该节点将显示其子节点。折叠 节点是隐藏它们的节点。隐藏 节点是位于折叠祖先下面的节点。所有可查看 节点的父节点都是可以展开的,但是可以显示它们,也可以不显示它们。显示 节点是可查看的并且位于可以看到它的显示区域。

以下 JTree 方法使用 "visible" 来表示 "displayed":

isRootVisible()
setRootVisible()
scrollPathToVisible()
scrollRowToVisible()
getVisibleRowCount()
setVisibleRowCount()
下一组 JTree 方法使用 "visible" 表示 "viewable"(在展开的父节点下面):

isVisible()
makeVisible()
如果您有兴趣了解选择何时更改,请实现 TreeSelectionListener 接口,并使用方法 addTreeSelectionListener 添加实例。当选择更改时,将调用 valueChanged,即如果用户在同一节点上单击两次,则仅调用 valueChanged 一次。

如果有兴趣检测双击事件或用户何时单击节点(不管是否选中它),建议您执行以下操作:

 final JTree tree = ...;

 MouseListener ml = new MouseAdapter() {
     public void mousePressed(MouseEvent e) {
         int selRow = tree.getRowForLocation(e.getX(), e.getY());
         TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
         if(selRow != -1) {
             if(e.getClickCount() == 1) {
                 mySingleClick(selRow, selPath);
             }
             else if(e.getClickCount() == 2) {
                 myDoubleClick(selRow, selPath);
             }
         }
     }
 };
 tree.addMouseListener(ml);
 注:上述示例同时获取路径和行,但是您只需要获取感兴趣的内容。
要使用 JTree 显示复合节点(例如,同时包含图形图标和文本的节点),请为 TreeCellRenderer 创建子类,并使用 setCellRenderer(javax.swing.tree.TreeCellRenderer) 通知树使用它。要编辑此类节点,请为 TreeCellEditor 创建子类,并使用 setCellEditor(javax.swing.tree.TreeCellEditor)。

与所有 JComponent 类一样,可以使用 InputMap 和 ActionMap 将 Action 对象与 KeyStroke 关联,并在指定条件下执行操作。

构造方法摘要
JTree()
          返回带有示例模型的 JTree。
JTree(Hashtable<?,?> value)
          返回从 Hashtable 创建的 JTree,它不显示根。
JTree(Object[] value)
          返回 JTree,指定数组的每个元素作为不被显示的新根节点的子节点。
JTree(TreeModel newModel)
          返回 JTree 的一个实例,它显示根节点 - 使用指定的数据模型创建树。
JTree(TreeNode root)
          返回一个 JTree,指定的 TreeNode 作为其根,它显示根节点。
JTree(TreeNode root, boolean asksAllowsChildren)
          返回一个 JTree,指定的 TreeNode 作为其根,它用指定的方式显示根节点,并确定节点是否为叶节点。
方法摘要

 int getSelectionCount()
          返回选择的节点数。
 int getRowForLocation(int x, int y)
          返回指定位置的行。
 TreePath getPathForLocation(int x, int y)
          返回指定位置处的节点路径。
对于此控件的方法我们在这里就说这几个,请大家自己参考JDK文档,下面我们就来看一个范例。
代码如下
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
/*
 *<html>
  <head>
   <title>
    JTree test
   </title>
  </head>
  <body>
    <applet height="300" width="300" code="Jtree.class">
     
    </applet>
  </body>
</html>
*/
public class Jtree extends JApplet{
 JTree tree;
 JTextField text;
 JScrollPane jsp;
 public void init(){
  Container con=this.getContentPane();
  con.setLayout(new BorderLayout());
  
  DefaultMutableTreeNode top=new DefaultMutableTreeNode("top");
     
  DefaultMutableTreeNode A=new DefaultMutableTreeNode("A");
  top.add(A);
  DefaultMutableTreeNode a1=new DefaultMutableTreeNode("a1");
     A.add(a1);
    
     DefaultMutableTreeNode a2=new DefaultMutableTreeNode("a2");
     A.add(a2);
    
     DefaultMutableTreeNode a3=new DefaultMutableTreeNode("a3");
     A.add(a3);
    
     DefaultMutableTreeNode B=new DefaultMutableTreeNode("B");
     top.add(B);
    
     DefaultMutableTreeNode b1=new DefaultMutableTreeNode("b1");
     B.add(b1);
    
     DefaultMutableTreeNode b2=new DefaultMutableTreeNode("b2");
     B.add(b2);
    
     DefaultMutableTreeNode b3=new DefaultMutableTreeNode("b3");
     B.add(b3);
    
     tree=new JTree(top);
    
     jsp=new JScrollPane(tree,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
     con.add(jsp,BorderLayout.CENTER);
    
     text=new JTextField(20);
     con.add(text,BorderLayout.SOUTH);
    
     tree.addMouseListener(new MouseAdapter(){
      public void mouseClicked(MouseEvent e){
       domouseClicked(e);
      }
     });
 }
 void domouseClicked(MouseEvent e){
  TreePath path=tree.getPathForLocation(e.getX(),e.getY());
  if(path!=null)
    text.setText(path.toString());
    else
      text.setText("");
 }
}
运行结果:
0 0
原创粉丝点击