其实说显示不出来也不是完全显示不出来,需要把鼠标移到button的上面,button才会显示出来
而且当button显示出来后,你改变窗口的大小,触发了repaint()之后,所有的button又没了,你需要再把鼠标
放在button上面,才会再次显示,不知道这个是什么问题下面我每次回复都有一个java源文件代码,总共5个文件,放在一个叫aaa的package里就可以运行了,我的jdk是1.6
主要的方法是
1.TreeView.java 的 private JButton getAddBtn()
2.Node.java     的 public void draw(Graphics g, MyPanel myPanel)
3.MyPanel.java  的 public void paint(Graphics g)

解决方案 »

  1.   

    主程序TreeView.javapackage aaa;import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.Rectangle;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import java.util.Random;import javax.swing.BorderFactory;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;public class TreeView extends JFrame {
    private JPanel bigPanel = null;
    private JPanel northPanel = null;
    private MyPanel southPanel = null; private JTextField jTextField = null;
    private JButton addBtn = null;
    private JButton clearBtn = null; Random random = new Random();

    /**
     * This method initializes this
     * 
     */
    private void initialize() {
    this.setSize(871, 757);
    this.setContentPane(getBigPanel()); this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    } /**
     * This method initializes jPanel
     * 
     * @return javax.swing.JPanel
     */
    private JPanel getBigPanel() {
    if (bigPanel == null) {
    bigPanel = new JPanel();
    bigPanel.setLayout(null);
    bigPanel.add(getNorthPanel(), null);
    bigPanel.add(getSouthPanel(), null);
    }
    return bigPanel;
    } /**
     * This method initializes northPanel
     * 
     * @return javax.swing.JPanel
     */
    private JPanel getNorthPanel() {
    if (northPanel == null) {
    northPanel = new JPanel();
    northPanel.setLayout(null);
    northPanel.setBounds(new Rectangle(0, 1, 492, 40));
    northPanel.setBorder(BorderFactory.createMatteBorder(5, 5, 5, 5, Color.blue));
    northPanel.add(getAddBtn(), null);
    northPanel.add(getJTextField(), null);
    northPanel.add(getClearBtn(), null);
    }
    return northPanel;
    } /**
     * This method initializes jButton
     * 
     * @return javax.swing.JButton
     */
    private JButton getAddBtn() {
    if (addBtn == null) {
    addBtn = new JButton();
    addBtn.setBounds(new Rectangle(161, 7, 90, 26));
    addBtn.setText("Add");
    }
    addBtn.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    try {
    southPanel.bsTree.add(Integer.parseInt(jTextField.getText()));
    } catch (Exception e1) {
    } finally {
    jTextField.setText(new Integer(random.nextInt(999)).toString());
    jTextField.requestFocus();
    jTextField.selectAll();

    repaint();
    }
    }
    });
    return addBtn;
    } /**
     * This method initializes jTextField
     * 
     * @return javax.swing.JTextField
     */
    private JTextField getJTextField() {
    if (jTextField == null) {
    jTextField = new JTextField();
    jTextField.setText(new Integer(random.nextInt(999)).toString());
    jTextField.selectAll();
    jTextField.setBounds(new Rectangle(7, 7, 143, 26));
    }
    jTextField.addKeyListener(new KeyAdapter() {
    public void keyPressed(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_ENTER) {
    addBtn.doClick();
    }
    }
    });
    return jTextField;
    } /**
     * This method initializes clearBtn
     * 
     * @return javax.swing.JButton
     */
    private JButton getClearBtn() {
    if (clearBtn == null) {
    clearBtn = new JButton();
    clearBtn.setBounds(new Rectangle(393, 9, 90, 23));
    clearBtn.setMnemonic(KeyEvent.VK_UNDEFINED);
    clearBtn.setText("Clear");
    }
    clearBtn.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    southPanel.bsTree.clear();
    repaint();
    }
    });
    return clearBtn;
    } private MyPanel getSouthPanel() {
    if (southPanel == null) {
    southPanel = new MyPanel(this);
    southPanel.setLayout(null);
    southPanel.setBounds(new Rectangle(18, 49, 745, 604));
    southPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
    }
    return southPanel;
    } public static void main(String[] args) {
    TreeView tv = new TreeView();
    } public TreeView() {
    super("JFrame1");
    initialize();
    }
    } // @jve:decl-index=0:visual-constraint="10,10"
      

  2.   

    Node.javapackage aaa;import java.awt.Color;
    import java.awt.Graphics;import javax.swing.JButton;public class Node extends JButton {
    public int x = 0;
    public int y = 0;
    public static int width = 80;
    public static int height = 20;
    public Node parent;
    public Node left;
    public Node right;
    public int key; public void draw(Graphics g, MyPanel myPanel) {
    if (this.parent != null) {
    g.setColor(Color.black);
    g.drawLine(this.x + this.width / 2, this.y + this.height / 2, this.parent.x + this.width / 2, this.parent.y + this.height / 2);
    }
    this.setBounds(this.x, this.y, this.width, this.height);
    this.setText(new Integer(this.key).toString());
    myPanel.add(this);
    } public void setXY(int x, int y) {
    this.x = x;
    this.y = y;
    } public Node(Node parent, Node left, Node right, int key) {
    this.parent = parent;
    this.left = left;
    this.right = right;
    this.key = key;
    } public String toString() {
    StringBuffer str = new StringBuffer();
    str.append("            parent:" + (parent == null ? "null" : parent.key) + "\n");
    str.append("            key:   " + key + "\n");
    str.append("left:  " + (left == null ? "null" : left.key) + "         right: " + (right == null ? "null" : right.key) + "\n");
    return str.toString();
    }
    }
      

  3.   

    MyPanel.javapackage aaa;import java.awt.Color;
    import java.awt.Graphics;
    import java.util.ArrayList;import javax.swing.JPanel;public class MyPanel extends JPanel {
    /**
     * 平衡二叉树
     */
    public BinarySearchTree bsTree;
    /**
     * x += xOffset;
     */
    private int x;
    /**
     * JFrame的引用
     */
    private TreeView tv; int xOffset = Node.width / 2;
    int yOffset = Node.height * 2; public MyPanel(TreeView tv) {
    this.tv = tv;
    this.bsTree = new BinarySearchTree();
    this.x = 0;
    } public void paint(Graphics g) {
    // Color c = g.getColor(); ArrayList<Node> list = new ArrayList<Node>(); treeMidWalk(bsTree.root, 0, list); for (int i = 0; i < list.size(); i++) {
    Node n = list.get(i);
    //  g.setColor(Color.magenta);
    //  g.fillOval(n.x, n.y, DIAMETER, DIAMETER);
    //  g.setColor(Color.black);
    //  if (n.parent != null) {
    //  g.drawLine(n.x + RADIUS, n.y + RADIUS, n.parent.x + RADIUS,
    //  n.parent.y + RADIUS);
    //  }
    //  g.drawString(new Integer(n.key).toString(), n.x + RADIUS, n.y +
    //  RADIUS);

    n.draw(g, this);

    } this.x = 0;
    this.setBounds(this.getX(), this.getY(), tv.getWidth(), tv.getHeight());
    // g.setColor(c);
    } public void treeMidWalk(Node root, int level, ArrayList<Node> list) {
    int y = level * yOffset;
    if (root != null) {
    treeMidWalk(root.left, level + 1, list);
    list.add(root);
    root.setXY(this.x, y);
    this.x += xOffset;
    treeMidWalk(root.right, level + 1, list);
    }
    }
    }
      

  4.   

    MyPanel.javapackage aaa;import java.awt.Color;
    import java.awt.Graphics;
    import java.util.ArrayList;import javax.swing.JPanel;public class MyPanel extends JPanel {
    /**
     * 平衡二叉树
     */
    public BinarySearchTree bsTree;
    /**
     * x += xOffset;
     */
    private int x;
    /**
     * JFrame的引用
     */
    private TreeView tv; int xOffset = Node.width / 2;
    int yOffset = Node.height * 2; public MyPanel(TreeView tv) {
    this.tv = tv;
    this.bsTree = new BinarySearchTree();
    this.x = 0;
    } public void paint(Graphics g) {
    // Color c = g.getColor(); ArrayList<Node> list = new ArrayList<Node>(); treeMidWalk(bsTree.root, 0, list); for (int i = 0; i < list.size(); i++) {
    Node n = list.get(i);
    //  g.setColor(Color.magenta);
    //  g.fillOval(n.x, n.y, DIAMETER, DIAMETER);
    //  g.setColor(Color.black);
    //  if (n.parent != null) {
    //  g.drawLine(n.x + RADIUS, n.y + RADIUS, n.parent.x + RADIUS,
    //  n.parent.y + RADIUS);
    //  }
    //  g.drawString(new Integer(n.key).toString(), n.x + RADIUS, n.y +
    //  RADIUS);

    n.draw(g, this);

    } this.x = 0;
    this.setBounds(this.getX(), this.getY(), tv.getWidth(), tv.getHeight());
    // g.setColor(c);
    } public void treeMidWalk(Node root, int level, ArrayList<Node> list) {
    int y = level * yOffset;
    if (root != null) {
    treeMidWalk(root.left, level + 1, list);
    list.add(root);
    root.setXY(this.x, y);
    this.x += xOffset;
    treeMidWalk(root.right, level + 1, list);
    }
    }
    }
      

  5.   

    BinarySearchTree.javapackage aaa;import java.util.ArrayList;public class BinarySearchTree extends Base {// public static void main(String[] args) {
    // int[] a = new int[] { 4, 5, 6, 7, 3, 1, 2, 9, 8 };
    // BinarySearchTree tree = new BinarySearchTree();
    // for (int i = 0; i < a.length; i++) {
    // tree.add(a[i]);
    // } 
    // tree.treeMidWalk(tree.root);
    // tree.clear();
    // System.out.println();
    // } public Node root; public BinarySearchTree() {
    this.root = null;
    } public void clear() {
    root = null;
    } public void treeMidWalk(Node root) {
    if (root != null) {
    treeMidWalk(root.left);
    System.out.print(" " + root.key);
    treeMidWalk(root.right);
    }
    } public Node search(Node root, int key) {
    if (root == null || root.key == key) {
    return root;
    }
    if (key < root.key) {
    return search(root.left, key);
    } else {
    return search(root.right, key);
    }
    } public Node searchFor(Node root, int key) {
    while (root != null) {
    if (key == root.key) {
    return root;
    } else if (key < root.key) {
    root = root.left;
    } else {
    root = root.right;
    }
    }
    return null;
    } public void add(int key) {
    Node n = this.root;
    Node p = this.root;
    while (n != null) {
    p = n;
    if (key <= n.key) {
    n = n.left;
    } else {
    n = n.right;
    }
    }
    Node newNode = new Node(null, null, null, key);
    if (p == null) {
    this.root = newNode;
    } else {
    newNode.parent = p;
    if (key <= p.key) {
    p.left = newNode;
    } else {
    p.right = newNode;
    }
    }
    } public void stepToRight(ArrayList<Node> nodes, int x) {
    for (int i = 0; i < nodes.size(); i++) {
    if (nodes.get(i).x >= x) {
    nodes.get(i).x += 50;
    }
    }
    } public Node successor(Node n) {
    if (n == null) {
    return null;
    } if (n.right != null) {
    return treeMin(n.right);
    }
    Node p = n.parent;
    while (n.parent != null && p.right == n) {
    n = p;
    p = n.parent;
    } return p;
    } public Node predecessor(Node n) {
    if (n == null) {
    return null;
    } if (n.left != null) {
    return treeMax(n.left);
    } Node p = n.parent; while (n.parent != null && n == p.left) {
    n = p;
    p = n.parent;
    } return p;
    } public Node treeMin(Node root) {
    if (root == null) {
    return root;
    }
    while (root.left != null) {
    root = root.left;
    }
    return root;
    } public Node treeMax(Node root) {
    if (root == null) {
    return root;
    }
    while (root.right != null) {
    root = root.right;
    }
    return root;
    }
    }
      

  6.   

    Base.java
    package aaa;public class Base {
    public static void swap(int[] a, int x1, int x2) {
    int temp;
    temp = a[x1];
    a[x1] = a[x2];
    a[x2] = temp;
    } public static double log2(double val) {
    return Math.log(val) / Math.log(2);
    }

    public static void printArr(int[] a) {
    for (int i = 0; i < a.length; i++) {
    System.out.print(a[i] + " ");
    }
    System.out.println();
    } public static String printNum(int number, int spacePad) {
    String s = String.valueOf(number);
    StringBuilder sb = new StringBuilder();
    int length = s.length();
    if (length < spacePad) {
    for (int i = 1; i <= spacePad - length; i++) {
    sb.append("_");
    }
    sb.append(s);
    return sb.toString();
    } else if (length == spacePad) {
    return s;
    } else {
    throw new RuntimeException("spacePad cannot smaller than the length of number");
    }
    } public static void printTree(int[] a, int spacePad) {
    int length = a.length;
    int height = (int) (log2(length) + 1);
    for (int h = 1; h <= height; h++) {
    int firstSpace = (int) (spacePad * (Math.pow(2, height - h) - 1));
    StringBuilder sbFirst = new StringBuilder();
    for (int i = 0; i < firstSpace; i++) {
    sbFirst.append(" ");
    }
    int intervalSpace = (int) (spacePad * (Math.pow(2, height - h + 1) - 1));
    StringBuilder sbInterval = new StringBuilder();
    for (int i = 0; i < intervalSpace; i++) {
    sbInterval.append(" ");
    }

    System.out.print(sbFirst);

    int start = (int) Math.pow(2, h - 1) - 1;
    int end = (int) Math.pow(2, h) - 1;
    if (end > length) {
    end = length;
    }
    for (int i = start; i < end; i++) {
    System.out.print(printNum(a[i], 2) + sbInterval);
    }
    System.out.println();
    }
    }
    }
      

  7.   

    虽然还没解决楼主的问题,我有一点发现,希望对楼主有用。
    在node.java的
    public void draw(Graphics g, MyPanel myPanel)方法中,如果把myPanel.add(this);放到方法的第一句,当添加的结点比前面的都大时,就会显示该结点。
    还有当按下clear按钮后面板并没立即清空。
      

  8.   

    终于解决问题了
    只要将MyPanel.java  的 
    public void paint(Graphics g)
    改为
    public void paintComponent(Graphics g)就行。
      

  9.   

    我找到你程序的问题了,但不我改不了你的代码,你的代码看上去挺乱的,所以问题是你没有把Node加到southPanel中,所以southPanel没法刷新Node,
    你必须把每次新增的Node添加到southPanel(southPanel.add(node))中
    这样就不会出现你说的那问题哦!!!小弟出道不到一年,请大家多关照,指教,谢谢哦!!!!
      

  10.   

    package aaa;import java.util.ArrayList;public class BinarySearchTree extends Base {//    public static void main(String[] args) {
    //        int[] a = new int[] { 4, 5, 6, 7, 3, 1, 2, 9, 8 };
    //        BinarySearchTree tree = new BinarySearchTree();
    //        for (int i = 0; i < a.length; i++) {
    //            tree.add(a[i]);
    //        } 
    //        tree.treeMidWalk(tree.root);
    //        tree.clear();
    //        System.out.println();
    //    }    public Node root;////////////////////////////////////////////////////
        private Node newNode;
    /////////////////////////////////////////////////////    public BinarySearchTree() {
            this.root = null;
        }    public void clear() {
            root = null;
        }    public void treeMidWalk(Node root) {
            if (root != null) {
                treeMidWalk(root.left);
                System.out.print(" " + root.key);
                treeMidWalk(root.right);
            }
        }    public Node search(Node root, int key) {
            if (root == null || root.key == key) {
                return root;
            }
            if (key < root.key) {
                return search(root.left, key);
            } else {
                return search(root.right, key);
            }
        }    public Node searchFor(Node root, int key) {
            while (root != null) {
                if (key == root.key) {
                    return root;
                } else if (key < root.key) {
                    root = root.left;
                } else {
                    root = root.right;
                }
            }
            return null;
        }    public void add(int key) {
            Node n = this.root;
            Node p = this.root;
            while (n != null) {
                p = n;
                if (key <= n.key) {
                    n = n.left;
                } else {
                    n = n.right;
                }
            }
    /////////////////////////////////////////////////////////
           newNode = new Node(null, null, null, key);
    ////////////////////////////////////////////////////////
            if (p == null) {
                this.root = newNode;
            } else {
                newNode.parent = p;
                if (key <= p.key) {
                    p.left = newNode;
                } else {
                    p.right = newNode;
                }
            }
        }    public void stepToRight(ArrayList<Node> nodes, int x) {
            for (int i = 0; i < nodes.size(); i++) {
                if (nodes.get(i).x >= x) {
                    nodes.get(i).x += 50;
                }
            }
        }    public Node successor(Node n) {
            if (n == null) {
                return null;
            }        if (n.right != null) {
                return treeMin(n.right);
            }
            Node p = n.parent;
            while (n.parent != null && p.right == n) {
                n = p;
                p = n.parent;
            }        return p;
        }    public Node predecessor(Node n) {
            if (n == null) {
                return null;
            }        if (n.left != null) {
                return treeMax(n.left);
            }        Node p = n.parent;        while (n.parent != null && n == p.left) {
                n = p;
                p = n.parent;
            }        return p;
        }    public Node treeMin(Node root) {
            if (root == null) {
                return root;
            }
            while (root.left != null) {
                root = root.left;
            }
            return root;
        }    public Node treeMax(Node root) {
            if (root == null) {
                return root;
            }
            while (root.right != null) {
                root = root.right;
            }
            return root;
        }
    }
      

  11.   

    我曾写过一个程序,我用一个JLabel加一个图片做背景,结果把其它的组件给遮住了,只要鼠标移到某个组件上,则那个组件就显示出来,
    你这个问题也应该是这类问题哦??!!!