⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 basepanel.java~73~

📁 javaGUI编写的人事管理系统(毕业学生答辩可参考)
💻 JAVA~73~
字号:
package project;

import javax.swing.*;
import java.awt.Rectangle;
import javax.swing.BorderFactory;
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentAdapter;
import java.awt.Font;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class Basepanel
    extends JPanel {

  private JPanel Up_Down_Panel = null;
  private JLabel Up_Button = null;
  private JLabel Down_Button = null;
  private JPanel Btn_Panel = null;
  JPanel[] panel = null;
  int space_size;
  Timer t = null;
  int delay = 200;
  ActionListener taskPerformer1 = null;
  ActionListener taskPerformer2 = null;

  public Basepanel(JPanel[] panel, int space_size, JPanel abp, String label_name) {
    this.panel = panel;
    this.space_size = space_size;
    this.Btn_Panel = abp;
    if(label_name != null){
      jLabel1 = new JLabel(label_name, SwingConstants.LEFT);
    }
    try {
      jbInit();

      taskPerformer1 = new ActionListener(){
                      public void actionPerformed(ActionEvent evt) {
                          Down_Btn_Move();
                      }
                  };
      taskPerformer2 = new ActionListener(){
                              public void actionPerformed(ActionEvent evt) {
                                  Up_Btn_Move();
                              }
                };


    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  private void jbInit() throws Exception {
    this.setLayout(null);
    if(jLabel1 == null){
      jLabel1 = new JLabel("jLabel1", SwingConstants.CENTER);
    }
    jLabel1.setBorder(BorderFactory.createEtchedBorder());
    jLabel1.setBounds(new Rectangle(0, 0, 200, 30));
    jLabel1.setOpaque(true);
    jLabel1.setBackground(Color.lightGray);
    jLabel1.setFont(new java.awt.Font("宋体", Font.PLAIN, 16));
    jLabel1.addMouseListener(new Basepanel_jLabel1_mouseAdapter(this));

    this.setSize(200, 200);

    Up_Down_Panel = new JPanel();
    Up_Down_Panel.setLayout(null);
    Up_Down_Panel.setBounds(new Rectangle(0, jLabel1.getHeight(), 200,
                                          this.getHeight() - jLabel1.getHeight()));
    Up_Down_Panel.setOpaque(false);

    Up_Button = new JLabel("UP", SwingConstants.CENTER);
    Up_Button.setForeground(Color.RED);
    Up_Button.setBounds(new Rectangle(0, 0, 200, 25));
    Up_Button.addMouseListener(new Basepanel_Up_Button_mouseAdapter(this));

    Down_Button = new JLabel("DOWN", SwingConstants.CENTER);
    Down_Button.setForeground(Color.RED);
    Down_Button.setBounds(new Rectangle(0, Up_Down_Panel.getHeight() - 25, 200,
                                        25));
    Down_Button.addMouseListener(new Basepanel_Down_Button_mouseAdapter(this));


    Up_Down_Panel.add(Up_Button);
    Up_Down_Panel.add(Down_Button);

    Btn_Panel.setLocation(0, jLabel1.getHeight());
    Btn_Panel.addComponentListener(new Basepanel_Btn_Panel_componentAdapter(this));

    this.add(jLabel1);
    this.add(Up_Down_Panel);
    this.add(Btn_Panel);
  }

  JLabel jLabel1 = null;

  public void jLabel1_mouseEntered(MouseEvent e) {
    jLabel1.setBorder(BorderFactory.createRaisedBevelBorder());
  }

  public void jLabel1_mouseExited(MouseEvent e) {
    jLabel1.setBorder(BorderFactory.createEtchedBorder());
  }

  public void jLabel1_mousePressed(MouseEvent e) {
    jLabel1.setBorder(BorderFactory.createLoweredBevelBorder());
  }

  public void jLabel1_mouseReleased(MouseEvent e) {
    jLabel1.setBorder(BorderFactory.createRaisedBevelBorder());
  }

  public void Up_Button_mouseEntered(MouseEvent e) {
    Up_Button.setBorder(BorderFactory.createRaisedBevelBorder());
    Up_Button.setOpaque(true);
  }

  public void Up_Button_mouseExited(MouseEvent e) {
    Up_Button.setBorder(BorderFactory.createEtchedBorder());
    Up_Button.setOpaque(false);
    Up_Button.updateUI();
  }

  public void Up_Button_mousePressed(MouseEvent e) {
    Up_Button.setBorder(BorderFactory.createLoweredBevelBorder());
    t = new Timer(delay,taskPerformer2);
    t.start();
  }

  public void Up_Button_mouseReleased(MouseEvent e) {
    Up_Button.setBorder(BorderFactory.createRaisedBevelBorder());
    t.stop();
  }

  public void Down_Button_mouseEntered(MouseEvent e) {
    Down_Button.setBorder(BorderFactory.createRaisedBevelBorder());
    Down_Button.setOpaque(true);
  }

  public void Down_Button_mouseExited(MouseEvent e) {
    Down_Button.setBorder(BorderFactory.createEtchedBorder());
    Down_Button.setOpaque(false);
     this.repaint();
  }

  public void Down_Button_mousePressed(MouseEvent e) {
    Down_Button.setBorder(BorderFactory.createLoweredBevelBorder());
    t = new Timer(delay,taskPerformer1);
    t.start();
  }

  public void Down_Button_mouseReleased(MouseEvent e) {
    Down_Button.setBorder(BorderFactory.createRaisedBevelBorder());
    t.stop();

  }

  public void Btn_Panel_componentMoved(ComponentEvent e) {
    if (Btn_Panel.getLocation().y >= jLabel1.getHeight()) {
      Up_Button.setVisible(false);
    }
    else {
      Up_Button.setVisible(true);
      Up_Button.setBorder(BorderFactory.createEmptyBorder());
    }

    if ( (Btn_Panel.getLocation().y + Btn_Panel.getHeight()) >
        (Up_Down_Panel.getLocation().y + Up_Down_Panel.getHeight())) {
      Down_Button.setVisible(true);
      Down_Button.setBorder(BorderFactory.createEmptyBorder());
    }
    else {
      Down_Button.setVisible(false);
    }

  }

  private void setShowDownBtn() {
    Up_Button.setVisible(false);
    if (Btn_Panel.getHeight() > this.space_size) {
      Down_Button.setVisible(true);
    }
    else {
      Down_Button.setVisible(false);
    }
  }

  private void myClick() {
    if (this.getHeight() <= jLabel1.getHeight()) {
      for (int i = 0; i < panel.length; i++) {
        panel[i].setSize(200, jLabel1.getHeight());
        ((Basepanel)panel[i]).getlabel().setBackground(Color.lightGray);
        ((Basepanel)panel[i]).getlabel().setForeground(Color.BLACK);
        if (i == 0) {
          panel[i].setLocation(0, 0);
        }
        else {
          panel[i].setLocation(0,
                               panel[i - 1].getLocation().y + panel[i - 1].getHeight());
        }
        Btn_Panel.setLocation(0, jLabel1.getHeight());
      }

      this.setSize(200, jLabel1.getHeight() + this.space_size);
      Up_Down_Panel.setSize(200, this.getHeight() - jLabel1.getHeight());
      Down_Button.setLocation(0,Up_Down_Panel.getHeight()-25);
      this.setShowDownBtn();

      for (int i = 0; i < panel.length; i++) {
        if (panel[i].getHeight() > jLabel1.getHeight()) {
          for (int j = i + 1; j < panel.length; j++) {
            panel[j].setLocation(0,
                                 panel[j - 1].getLocation().y + panel[j - 1].getHeight());
          }
          break;
        }
      }

    }
    else {
      for (int i = 0; i < panel.length; i++) {
      if (panel[i].getHeight() > jLabel1.getHeight()) {
        for (int j = i + 1; j < panel.length; j++) {
          panel[j].setLocation(0,
                               panel[j - 1].getLocation().y + jLabel1.getHeight());
        }
      }
    }
    this.setSize(200, jLabel1.getHeight());
    }

  }

private JLabel getlabel() {
    return this.jLabel1;
  }

  private JPanel getAddBtnPanel(){
    return this.Btn_Panel;
  }

  private JPanel getUpDownPanel(){
    return this.Up_Down_Panel;
  }

  private JLabel getDownBtn(){
    return this.Down_Button;
  }

  private JLabel getUpBtn(){
    return this.Up_Button;
  }
  public static void call_resize(Basepanel[] panel, int reHeight){
    int open_panel_num = -1;
    for(int i = 0; i < panel.length; i++){
     if(panel[i].getHeight()>panel[i].getlabel().getHeight()){
        open_panel_num = i;
        break;
      }
    }
    for(int i = 0;i < panel.length;i++){
      panel[i].setSize(200, panel[i].getlabel().getHeight());
      if(i == 0){
        panel[i].setLocation(0, 0);
        }else{
          panel[i].setLocation(0, panel[i-1].getLocation().y+panel[i-1].getHeight());
        }
        panel[i].getAddBtnPanel().setLocation(0, panel[i].getlabel().getHeight());
        panel[i].space_size = reHeight;
    }

    if(open_panel_num != -1){
      panel[open_panel_num].setSize(200, panel[open_panel_num].getlabel().getHeight() + panel[open_panel_num].space_size);
      panel[open_panel_num].getUpDownPanel().setSize(200, panel[open_panel_num].getHeight()-panel[open_panel_num].getlabel().getHeight());
      panel[open_panel_num].getDownBtn().setLocation(0,panel[open_panel_num].getUpDownPanel().getHeight()-25);

      panel[open_panel_num].getUpBtn().setVisible(false);
      if(panel[open_panel_num].getAddBtnPanel().getHeight()>panel[open_panel_num].space_size){
      panel[open_panel_num].getDownBtn().setVisible(true);
      }else{
        panel[open_panel_num].getDownBtn().setVisible(false);
      }
      for(int i=open_panel_num + 1; i < panel.length ;i++ ){
        panel[i].setLocation(0, panel[i - 1].getLocation().y + panel[i - 1].getHeight());
      }
      }


  }

  private void Down_Btn_Move(){
    int space = (Btn_Panel.getLocation().y + Btn_Panel.getHeight()) - (Up_Down_Panel.getLocation().y + Up_Down_Panel.getHeight());
    if(space > 5){
      Btn_Panel.setLocation(0, Btn_Panel.getLocation().y - 5);
    }else{
      Btn_Panel.setLocation(0, Btn_Panel.getLocation().y - space);
      Down_Button.setVisible(false);
      t.stop();

    }
  }

  private void Up_Btn_Move(){
    int space = Btn_Panel.getLocation().y - jLabel1.getHeight();
    if(space < -5){
      Btn_Panel.setLocation(0, Btn_Panel.getLocation().y + 5);
      }else{
      Btn_Panel.setLocation(0, Btn_Panel.getLocation().y - space);
      Up_Button.setVisible(false);
      t.stop();

      }
  }

  public void jLabel1_mouseClicked(MouseEvent e) {
    myClick();
    if(jLabel1.getBackground()== Color.LIGHT_GRAY){
      jLabel1.setBackground(Color.DARK_GRAY);
      jLabel1.setForeground(Color.WHITE);
    }else{
      jLabel1.setBackground(Color.lightGray);
      jLabel1.setForeground(Color.BLACK);
    }

  }

}

class Basepanel_Up_Button_mouseAdapter
    extends MouseAdapter {
  private Basepanel adaptee;
  Basepanel_Up_Button_mouseAdapter(Basepanel adaptee) {
    this.adaptee = adaptee;
  }

  public void mousePressed(MouseEvent e) {
    adaptee.Up_Button_mousePressed(e);
  }

  public void mouseExited(MouseEvent e) {
    adaptee.Up_Button_mouseExited(e);
  }

  public void mouseReleased(MouseEvent e) {
    adaptee.Up_Button_mouseReleased(e);
  }

  public void mouseEntered(MouseEvent e) {
    adaptee.Up_Button_mouseEntered(e);
  }
}

class Basepanel_Down_Button_mouseAdapter
    extends MouseAdapter {
  private Basepanel adaptee;
  Basepanel_Down_Button_mouseAdapter(Basepanel adaptee) {
    this.adaptee = adaptee;
  }

  public void mouseReleased(MouseEvent e) {
    adaptee.Down_Button_mouseReleased(e);
  }

  public void mousePressed(MouseEvent e) {
    adaptee.Down_Button_mousePressed(e);
  }

  public void mouseExited(MouseEvent e) {
    adaptee.Down_Button_mouseExited(e);
  }

  public void mouseEntered(MouseEvent e) {
    adaptee.Down_Button_mouseEntered(e);
  }
}

class Basepanel_Btn_Panel_componentAdapter
    extends ComponentAdapter {
  private Basepanel adaptee;
  Basepanel_Btn_Panel_componentAdapter(Basepanel adaptee) {
    this.adaptee = adaptee;
  }

  public void componentMoved(ComponentEvent e) {
    adaptee.Btn_Panel_componentMoved(e);
  }
}

class Basepanel_jLabel1_mouseAdapter
    extends MouseAdapter {
  private Basepanel adaptee;
  Basepanel_jLabel1_mouseAdapter(Basepanel adaptee) {
    this.adaptee = adaptee;
  }
  public void mouseExited(MouseEvent e) {
    adaptee.jLabel1_mouseExited(e);
  }
  public void mouseClicked(MouseEvent e) {
    adaptee.jLabel1_mouseClicked(e);
  }

  public void mousePressed(MouseEvent e) {
    adaptee.jLabel1_mousePressed(e);
  }

  public void mouseReleased(MouseEvent e) {
    adaptee.jLabel1_mouseReleased(e);
  }

  public void mouseEntered(MouseEvent e) {
    adaptee.jLabel1_mouseEntered(e);
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -