RSS

Java Applet2

12 11月

Java Applet2  /My Document/Develop/Java/Applet2.zip

Java Applet 2

import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JApplet;
import javax.swing.JSplitPane;
import javax.swing.JTree;
import javax.swing.tree.*;
import javax.swing.JLabel;

/*
 <applet code="JApplet2.class" width="500" height="200">
 </applet>
*/

public class JApplet2 extends JApplet {
 private static final long serialVersionUID = 1L;
 private JPanel jContentPane = null;
 private JSplitPane jSplitPane = null;
 private JPanel jPanel = null;
 private JPanel jPanel1 = null;
 private JTree jTree = null;
 private JLabel jLabel1 = null;
 /**
  * This is the default constructor
  */
 public JApplet2() {
  super();
 }

 /**
  * This method initializes this
  *
  * @return void
  */
 public void init() {
  this.setSize(300, 200);
  this.setContentPane(getJContentPane());
 }

 /**
  * This method initializes jContentPane
  *
  * @return javax.swing.JPanel
  */
 private JPanel getJContentPane() {
  if (jContentPane == null) {
   jContentPane = new JPanel();
   jContentPane.setLayout(new BorderLayout());
   jContentPane.add(getJSplitPane(), java.awt.BorderLayout.CENTER);
  }
  return jContentPane;
 }

 /**
  * This method initializes jSplitPane 
  *  
  * @return javax.swing.JSplitPane 
  */
 private JSplitPane getJSplitPane() {
  if (jSplitPane == null) {
   jSplitPane = new JSplitPane();
   jSplitPane.setLeftComponent(getJPanel());
   jSplitPane.setRightComponent(getJPanel1());
  }
  return jSplitPane;
 }

 /**
  * This method initializes jPanel 
  *  
  * @return javax.swing.JPanel 
  */
 private JPanel getJPanel() {
  if (jPanel == null) {
   jPanel = new JPanel();
   jPanel.add(getJTree(), null);
  }
  return jPanel;
 }

 /**
  * This method initializes jPanel1 
  *  
  * @return javax.swing.JPanel 
  */
 private JPanel getJPanel1() {
  if (jPanel1 == null) {
   jLabel1 = new JLabel();
   jLabel1.setText("Not selected");
   jPanel1 = new JPanel();
   jPanel1.add(jLabel1, null);
  }
  return jPanel1;
 }

 /**
  * This method initializes jTree 
  *  
  * @return javax.swing.JTree 
  */
 private JTree getJTree() {
  DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
  DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("node1");
  DefaultMutableTreeNode sub1 = new DefaultMutableTreeNode("sub1");
  DefaultMutableTreeNode sub2 = new DefaultMutableTreeNode("sub2");
  node1.add(sub1);
  node1.add(sub2);
  DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("node2");
  root.add(node1);
  root.add(node2);
  if (jTree == null) {
   jTree = new JTree(root);
   jTree.setRootVisible(true);
   jTree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
    public void valueChanged(javax.swing.event.TreeSelectionEvent e) {
     nodeSelected();
    }
   });
  }
  return jTree;
 }

 private void nodeSelected() {
  String s = "Selected = " + jTree.getSelectionPath().getLastPathComponent().toString();
  jLabel1.setText(s);
 }
}

実行

 appletviewer JApplet2.java
 
Java Applet2 はコメントを受け付けていません。

投稿者: : 2010/11/12 投稿先 Java

 

タグ:

コメントは受け付けていません。