日期:2014-05-20  浏览次数:20751 次

java swing的一个简单问题
代码如下:(请大家帮忙解决一个问题:就是当我双击右边的tree的时候,就会出现排版偏移,恳请大家帮忙看看这个怎么解决)

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.*;

public class Work
{
public static void main(String[] args)
{
JFrame frame = new JFrame ("成绩统计系统");
  frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

  JukeBoxControls controlPanel = new JukeBoxControls();

  frame.getContentPane().add(controlPanel);
  frame.pack();
  frame.setVisible(true);
}
}
class JukeBoxControls extends JPanel
{
  private JComboBox Combo;
  private JPanel panel;
  private JPanel[] d;
   
  public JukeBoxControls()
  {
  JLabel titleLabel = new JLabel ("系别");
  titleLabel.setAlignmentX (Component.CENTER_ALIGNMENT);

  String[] Names = {"Make A Selection...", "计算机与通信系",
  "机械系", "土木工程系", "电气系",
  "外国语系","人文社科系"};

  Combo = new JComboBox (Names);
  Combo.setAlignmentX (Component.CENTER_ALIGNMENT);
  Combo.setBounds(20,20,50,10);
   
  JPanel p=new JPanel();
  p.setBackground (Color.cyan);
  p.setLayout (new BoxLayout (p, BoxLayout.Y_AXIS));
  p.add (Box.createRigidArea (new Dimension(0,5)));
  p.add (titleLabel);
  p.add (Box.createRigidArea (new Dimension(0,5)));
  p.add (Combo);
  p.add (Box.createRigidArea (new Dimension(0,250)));
   
  panel=new JPanel();
  panel.setBackground(Color.cyan);
  tree t=new tree();
  panel.add(t);
   
  JSplitPane sp=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,p,panel);
  add(sp);
  }
  private class ComboListener implements ActionListener
  {
  public void actionPerformed (ActionEvent event)
  {
  int a = Combo.getSelectedIndex();
  if(a==1)
  {
  Computer c=new Computer();
 
  panel.add(c);
  }
  }
  }
}
class Computer extends JPanel
{
private JCheckBox jcb1,jcb2,jcb3,jcb4,jcb5;
private JLabel lc;
double count;
public Computer()
{
jcb1=new JCheckBox("英语演讲比赛一等奖");
jcb1.setBackground(Color.cyan);

jcb2=new JCheckBox("英语演讲比赛二等奖");
jcb2.setBackground(Color.cyan);

jcb3=new JCheckBox("英语演讲比赛三等奖");
jcb3.setBackground(Color.cyan);

jcb4=new JCheckBox("计算机二级");
jcb4.setBackground(Color.cyan);

jcb5=new JCheckBox("班委或协会干事");
jcb5.setBackground(Color.cyan);

lc=new JLabel("else :");
JTextField jtc=new JTextField();


setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
setBackground(Color.cyan);

add(jcb1);
add(Box.createRigidArea(new Dimension(0,10)));

add(jcb2);
add(Box.createRigidArea(new Dimension(0,10)));

add(jcb3);
add(Box.createRigidArea(new Dimension(0,10)));

add(jcb4);
add(Box.createRigidArea(new Dimension(0,10)));

add(jcb5);
add(Box.createRigidArea(new Dimension(0,10)));

add(lc);
add(jtc);
}
}
class tree extends JPanel implements TreeSelectionListener