原地移除面板----1

本文介绍了一个使用Java Swing实现的简单应用程序,该程序能够在不同的面板之间进行切换,通过按钮触发面板的替换并保持布局不变。

package removepanel;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Rectangle;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Graphics;
import java.awt.*;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2007</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class Frame1 extends JFrame {
    JPanel contentPane;
    JPanel jPanel1 = new JPanel();
    JButton jButton1 = new JButton();
    JButton jButton2 = new JButton();
    JButton jButton3 = new JButton();
    public Frame1() {
        try {
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            jbInit();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    /**
     * Component initialization.
     *
     * @throws java.lang.Exception
     */
    private void jbInit() throws Exception {
        contentPane = (JPanel) getContentPane();
        contentPane.setLayout(null);
        setSize(new Dimension(500, 400));
        setTitle("Frame Title");
        jPanel1.setBorder(BorderFactory.createEtchedBorder());
        jPanel1.setBounds(new Rectangle(18, 16, 369, 213));
        jButton1.setBounds(new Rectangle(38, 254, 151, 42));
        jButton1.setText("jButton1");
        jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
        jButton2.setText("jButton2");
        jButton3.setBounds(new Rectangle(207, 255, 171, 41));
        jButton3.setText("jButton3");
        jButton3.addActionListener(new Frame1_jButton3_actionAdapter(this));
        contentPane.add(jPanel1);
        jPanel1.add(jButton2);
        contentPane.add(jButton1);
        contentPane.add(jButton3);
        panelWidth = jPanel1.getWidth();
        panelHeight = jPanel1.getHeight();
        panelX = jPanel1.getX();
        panelY = jPanel1.getY();
    }

    //创建新面板
    JPanel pane = new Panel1();
    //创建新面板
    JPanel pane1 = new Panel2();
    int panelWidth = 0;
    int panelHeight = 0;
    int panelX = 0;
    int panelY = 0;
    public void jButton1_actionPerformed(ActionEvent e) {
            //设置面板的的大小与以前的面板大小一致
            pane.setSize(panelWidth, panelHeight);
            //设置面板边框
            pane.setBorder(BorderFactory.createEtchedBorder());
            //设置面板的位置与以前的面板位置一致
            pane.setLocation(panelX, panelY);
            //设置重画
            Graphics Gra = contentPane.getGraphics();
            //移出面板
            contentPane.remove(jPanel1);
            //添加面板
            contentPane.add(pane);
            //更新面板
            contentPane.update(Gra);
            this.show();
            jPanel1 = pane;
    }

    public void jButton3_actionPerformed(ActionEvent e) {
            //设置面板的的大小与以前的面板大小一致
            pane1.setSize(panelWidth, panelHeight);
            //设置面板边框
            pane1.setBorder(BorderFactory.createEtchedBorder());
            //设置面板的位置与以前的面板位置一致
            pane1.setLocation(panelX, panelY);
            //设置重画
            Graphics Gra = contentPane.getGraphics();
            //移出面板
            contentPane.remove(jPanel1);
            //添加面板
            contentPane.add(pane1);
            //更新面板
            contentPane.update(Gra);
            this.show();
            jPanel1 = pane1;
    }
}


class Frame1_jButton3_actionAdapter implements ActionListener {
    private Frame1 adaptee;
    Frame1_jButton3_actionAdapter(Frame1 adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jButton3_actionPerformed(e);
    }
}


class Frame1_jButton1_actionAdapter implements ActionListener {
    private Frame1 adaptee;
    Frame1_jButton1_actionAdapter(Frame1 adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jButton1_actionPerformed(e);
    }
}

 

 

 

public class Panel1 extends JPanel {
    public Panel1() {
        try {
            jbInit();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    private void jbInit() throws Exception {
        this.setLayout(null);
        jButton1.setBounds(new Rectangle(24, 13, 229, 56));
        jButton1.setText("jButton1");
        this.add(jButton1);
    }

    JButton jButton1 = new JButton();
}

 

 

public class Panel2 extends JPanel {
    BorderLayout borderLayout1 = new BorderLayout();
    JButton jButton1 = new JButton();

    public Panel2() {
        try {
            jbInit();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    private void jbInit() throws Exception {
        this.setLayout(borderLayout1);
        jButton1.setText("jButton1");
        this.add(jButton1, java.awt.BorderLayout.NORTH);
    }
}
 

#!/bin/bash # 简化版用户信息管理系统 CSV_FILE="users.csv" init_file() { if [[ ! -f "$CSV_FILE" ]]; then echo "用户名,密码,年龄,角色" > "$CSV_FILE" fi } # 随机密码生成(8-12位) generate_password() { local chars='A-Za-z0-9@#$%^&*_+' tr -dc "$chars" </dev/urandom | head -c $((RANDOM%5+8)) } add_user() { read -p "用户名: " username if grep -q "^$username," "$CSV_FILE"; then echo "错误:用户已存在!" && return 1 fi read -p "年龄: " age [[ ! "$age" =~ ^[0-9]+$ ]] && echo "错误:年龄无效!" && return 1 read -p "角色: " role password=$(generate_password) echo "$username,$password,$age,$role" >> "$CSV_FILE" echo "添加成功!密码: $password" } delete_user() { read -p "用户名: " username grep -v "^$username," "$CSV_FILE" > tmp && mv tmp "$CSV_FILE" echo "操作完成" } update_user() { read -p "用户名: " username current=$(grep "^$username," "$CSV_FILE") || { echo "用户不存在!" && return 1 } echo "当前信息: $current" read -p "新年龄(留空保留): " age [[ -z "$age" ]] && age=$(echo "$current" | cut -d, -f3) read -p "新角色(留空保留): " role [[ -z "$role" ]] && role=$(echo "$current" | cut -d, -f4) sed -i "/^$username,/c\\$username,$(echo "$current"|cut -d, -f2),$age,$role" "$CSV_FILE" echo "更新成功!" } find_user() { read -p "搜索词: " term grep -i "$term" "$CSV_FILE" || echo "无匹配结果" } # 统计功能面板 stats_menu() { while true; do echo -e "\n[统计功能] 1.角色分布 2.平均年龄 3.年龄极值 4.返回" read -p "选择: " choice case $choice in 1) awk -F, 'NR>1{roles[$4]++}END{for(r in roles)print r": "roles[r]"人"}' "$CSV_FILE" ;; 2) awk -F, 'NR>1{sum+=$3;c++}END{printf "平均年龄:%.1f岁\n",sum/c}' "$CSV_FILE" ;; 3) echo "最高年龄:" sort -t, -k3 -nr "$CSV_FILE" | head -2 | tail -1 echo "最低年龄:" sort -t, -k3 -n "$CSV_FILE" | head -2 | tail -1 ;; 4) break ;; *) echo "无效输入" ;; esac done } # 主菜单 main_menu() { init_file while true; do echo -e "\n用户管理系统 v1.0" echo "1.添加用户 2.删除用户 3.修改用户" echo "4.查询用户 5.系统统计 6.退出" read -p "选择操作: " choice case $choice in 1) add_user ;; 2) delete_user ;; 3) update_user ;; 4) find_user ;;
最新发布
06-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值