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

📄 userjframe.java

📁 这是我学java时做的小作业
💻 JAVA
字号:
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JScrollPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.JOptionPane;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JComboBox;
import javax.swing.JList;

/*
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
*/


class zoo{
	
     int sum = 3;
    static  int birdn = 2;
    static  int fishn = 1;
    static int burun = 0;
    static  int insectn = 0;
    
    static  Object birds[][] = new String[2][10];
    static  Object fishes[][] = new String[2][10];
    static  Object burus[][] = new String[2][10];
    static  Object insects[][] = new String[2][10];
    
    public zoo(){
    	
       birds[0][0] = "企鹅";
       birds[1][0] = "企鹅生长在南极,憨态可掬";
       birds[0][1] = "驼鸟";
       birds[1][1] = "驼鸟生长在澳洲,体态滑稽";
       fishes[0][0] = "鲨鱼";
       fishes[1][0] = "鲨鱼生长在深海,性情残忍";
    	}
    
    
	
	
	
    public static String showmessage(){
    	String str = "----------动物清单------------\n";
    	
    	int i = 0;
         str += "鸟类:\n\n";
       	 for(int i1 = 0;i1 <= birdn - 1;i1++)
    	  {str += Integer.toString(++i) + birds[0][i1] + "\n" + birds[1][i1] + "\n";
    	 }
    	 str += "--------------------------------------------------\n";
    	 i = 0;
    	 str += "鱼类:\n\n";
       	 for(int i1 = 0;i1 <= fishn - 1;i1++)
    	  {str += Integer.toString(++i) + fishes[0][i1] + "\n" + fishes[1][i1] + "\n";
    	 }
    	 str += "--------------------------------------------------\n";
    	 i = 0;
    	 str += "哺乳类:\n\n";
    	 for(int i1 = 0;i1 <= burun - 1;i1++)
    	  {str += Integer.toString(++i) + burus[0][i1] + "\n" + burus[1][i1] + "\n";
    	 }
    	 str += "--------------------------------------------------\n";
    	 i = 0;
    	 str += "昆虫类:\n\n";
    	 for(int i1 = 0;i1 <=insectn - 1;i1++)
    	  {str += Integer.toString(++i) + insects[0][i1] + "\n" + insects[1][i1] +"\n";
    	 }
    	 str += "--------------------------------------------------\n";
    	//str += 
    	 return str;
    	
    	}
 	
    	
}
public class UserJFrame  extends JFrame 
{
                                    //添加按钮
   
    
    public UserJFrame()
    {
        super("动物园成员清单");        
        //this.setLayout(new FlowLayout(FlowLayout.CENTER));
        this.setSize(360,200);
        this.setLocation(300,240);
     	this.setResizable(false);
        
        //this.setDefaultCloseOperation(EXIT_ON_CLOSE);    //单击窗口关闭按钮时,结束程序运行
       // this.setLayout(new GridLayout(3,1)); 
        MyShowAnimalPanel myshow = new MyShowAnimalPanel();
       	this.getContentPane().add(myshow,BorderLayout.CENTER);
       	myshow.setVisible(true);
 

       
    }
    
 

   
    
    
}    


    
class MyShowAnimalPanel extends JPanel implements ActionListener,ItemListener
{   static alterframe myalterframe = new alterframe();
    static addAndmodify addandmodify = new addAndmodify();
    static JTextArea text_user;                                //文本区
    private JComboBox alternative;
   	private JScrollPane scrollPane;
   	JButton b_refresh =new JButton("刷新列表");
    public MyShowAnimalPanel(){
   		  new zoo();   //完成输入初始成员
   		 //this.setLocation(600,240);
          text_user = new JTextArea("",7,25);
          text_user.setEditable(false);
       
          scrollPane=new JScrollPane(text_user,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
     	  this.add(scrollPane);
         // this.add(text_user);
       	  Object alter_content[] = {"选择操作","添加","删除","修改"};
          alternative = new JComboBox(alter_content);
          alternative.addItemListener(this);
          this.add(alternative);
          b_refresh.addActionListener(this);
          this.add(b_refresh);
          text_user.setText(zoo.showmessage());
   		//this.setVisible(true);
   		}
   	public void actionPerformed(ActionEvent e){
   		  if (e.getSource()== b_refresh) {
   		  	 JOptionPane.showMessageDialog(this,"无需刷新,此列表已经在您修改时自动更新过!呵呵");
   		  	
   		  	}
	}
	public void itemStateChanged(ItemEvent e)            //在组合框的下拉列表中选择数据项时触发执行
    {                                                    //实现ItemListener接口中的方法
        switch (alternative.getSelectedIndex()) {
           case 0:myalterframe.setVisible(false);
                  addandmodify.setVisible(false);
                  break;
           case 1:myalterframe.setVisible(false);
                  addandmodify.setVisible(true);
                  break;
           case 2:addandmodify.setVisible(false);
                  myalterframe.setVisible(true);
                  break;
           case 3:myalterframe.setVisible(false);
                  addandmodify.setVisible(true);
                  break;
                  
           
          	}
    }
    
	}
	
	
class alterframe extends JFrame implements ActionListener {
	private JComboBox xuanzelei;
	private JComboBox xuanzehao;
	private JButton submit;
	public alterframe(){
		super("删除成员");
		
		this.setSize(360,100);
	
		this.setLocation(300,440);
		this.setLayout(new FlowLayout());
		Object alter_content1[] = {"选择类名","鸟类","鱼类","哺乳类","昆虫类"};
        xuanzelei = new JComboBox(alter_content1);
		Object alter_content2[] = {"选择编号","1","2","3","4","5","6","7","8","9","10"};
        xuanzehao = new JComboBox(alter_content2);
		submit = new JButton("确定");
		submit.addActionListener(this);
		this.add(xuanzelei);
		this.add(xuanzehao);
		this.add(submit);
	}
  public void actionPerformed(ActionEvent e){
  		int i;//循环时用!
  
  	if (e.getSource()== submit) { 
  	    switch (xuanzelei.getSelectedIndex())
        {
            case 0: 
                    break;
            case 1:if(xuanzehao.getSelectedIndex() == 0)
                      break;  
                   if((zoo.birdn == 0 )|| xuanzehao.getSelectedIndex() > zoo.birdn ){
                      JOptionPane.showMessageDialog(this,"无此编号的动物成员!");
                      break;}
                   zoo.birdn = zoo.birdn - 1;
                   for(  i = xuanzehao.getSelectedIndex() - 1;i <  zoo.birdn; i++){
                      zoo.birds[0][i] = zoo.birds[0][i + 1];
                      zoo.birds[1][i] = zoo.birds[1][i + 1];}
                   MyShowAnimalPanel.text_user.setText(zoo.showmessage());     
                   break;   
         
            case 2:if(xuanzehao.getSelectedIndex() == 0)
                      break;  
                   if((zoo.fishn == 0 )|| xuanzehao.getSelectedIndex() > zoo.fishn ){
                      JOptionPane.showMessageDialog(this,"无此编号的动物成员!");
                      break;}
                   zoo.fishn = zoo.fishn - 1;
                   for( i = xuanzehao.getSelectedIndex() - 1;i <  zoo.fishn; i++){
                      zoo.fishes[0][i] = zoo.fishes[0][i + 1];
                      zoo.fishes[1][i] = zoo.fishes[1][i + 1];}
                   MyShowAnimalPanel.text_user.setText(zoo.showmessage());     
                   break; 
            case 3:if(xuanzehao.getSelectedIndex() == 0)
                      break;  
                   if((zoo.burun == 0 )|| xuanzehao.getSelectedIndex() > zoo.burun ){
                      JOptionPane.showMessageDialog(this,"无此编号的动物成员!");
                      break;}
                   zoo.burun = zoo.burun - 1;
                   for( i = xuanzehao.getSelectedIndex() - 1;i <  zoo.burun; i++){
                      zoo.burus[0][i] = zoo.burus[0][i + 1];
                      zoo.burus[1][i] = zoo.burus[1][i + 1];}
                   MyShowAnimalPanel.text_user.setText(zoo.showmessage());     
                   break;            
            case 4:if(xuanzehao.getSelectedIndex() == 0)
                      break;  
                   if((zoo.insectn == 0 )|| xuanzehao.getSelectedIndex() > zoo.insectn ){
                      JOptionPane.showMessageDialog(this,"无此编号的动物成员!");
                      break;}
                   zoo.insectn = zoo.insectn - 1;
                   for( i = xuanzehao.getSelectedIndex() - 1;i <  zoo.insectn; i++){
                      zoo.insects[0][i] = zoo.insects[0][i + 1];
                      zoo.insects[1][i] = zoo.insects[1][i + 1];}
                   MyShowAnimalPanel.text_user.setText(zoo.showmessage());     
                   break;                     
       
             } 
  	}
  	
  	}
  
	}	
class addAndmodify extends JFrame implements ActionListener, ItemListener{
	private JComboBox xuanzelei;
	private JTextArea shurudongwu;  
	private JTextArea shuruzhushi;  
	private JButton submit;
	private int flag = 0;    //用于下面的FOR循环
	public addAndmodify(){
		super("添加或者修改成员");
	    this.setSize(360,100);
		//this.setVisible(true);
		this.setLocation(300,440);
		this.setLayout(new FlowLayout());
		Object alter_content1[] = {"选择类名","鸟类","鱼类","哺乳类","昆虫类"};
        xuanzelei = new JComboBox(alter_content1);
        this.add(xuanzelei);
        shurudongwu = new JTextArea("输入动物名称",1,8);
        this.add(shurudongwu);
        submit = new JButton("提交");
        submit.addActionListener(this);
		this.add(submit);
        shuruzhushi = new JTextArea("在此添加注释",1,30);
		this.add(shuruzhushi);
	
		}
	public void actionPerformed(ActionEvent e){
		
	 if (e.getSource()== submit) {  
        switch (xuanzelei.getSelectedIndex())
        {
            case 0:                   
                    break;
            case 1: 
                    if(zoo.birdn < 10){
                      for(int i=0;i<zoo.birdn;i++){
                      	if(zoo.birds[0][i] == shurudongwu.getText()){
                      	   zoo.birds[1][i] = shuruzhushi.getText();
                      	   flag = 1;
                      	   
                      	   break;}
                      	
                      	   }	              
                      if(flag ==0){
                         zoo.birdn++;
                         zoo.birds[0][zoo.birdn - 1] = shurudongwu.getText();
                         zoo.birds[1][zoo.birdn - 1] = shuruzhushi.getText();}
                       //   System.out.println((String)); 
                        }
                    else
                      JOptionPane.showMessageDialog(this,"额定超员:每类动物数额要小于10!");
                    break;
            case 2: 
                    if(zoo.fishn < 10){              
                      zoo.fishn++;
                      zoo.fishes[0][zoo.fishn - 1] = shurudongwu.getText();
                      zoo.fishes[1][zoo.fishn - 1] = shuruzhushi.getText();}
                    else
                      JOptionPane.showMessageDialog(this,"额定超员:每类动物数额要小于10!");
                    break;
            case 3: 
                    if(zoo.burun < 10){              
                      zoo.burun++;
                      zoo.burus[0][zoo.burun - 1] = shurudongwu.getText();
                      zoo.burus[1][zoo.burun - 1] = shuruzhushi.getText();}
                    else
                      JOptionPane.showMessageDialog(this,"额定超员:每类动物数额要小于10!");
                    break;
            case 4:  
                    if(zoo.insectn < 10){              
                      zoo.insectn++;
                      zoo.insects[0][zoo.insectn - 1] = shurudongwu.getText();
                      zoo.insects[1][zoo.insectn - 1] = shuruzhushi.getText();}
                    else
                      JOptionPane.showMessageDialog(this,"额定超员:每类动物数额要小于10!");
                    break;
           default: System.out.println("Data Error!"); 
           
        }        
        }
       System.out.println(zoo.insectn);
       MyShowAnimalPanel.text_user.setText(zoo.showmessage());  
	}
		
    public void itemStateChanged(ItemEvent e){
	  	}   
}	
	
	

⌨️ 快捷键说明

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