Java GUI的增加、删除、修改

public class Hero {
    //增加id属性
    public int id;
     public String name;
     public float hp;
     public int damage;
 
     public static void main(String[] args){}
}
package multiplethread;
import java.util.LinkedList;
import java.util.Collections;
import java.util.Random;
import java.util.List;
public class Producer_Consumer {
	
	LinkedList stack1 = new LinkedList();
	List stack =   Collections.synchronizedList(stack1);
	
	public synchronized Character get(){
		Random r = new Random();
		String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		char a = str.charAt(r.nextInt(26));
		return a;
	}
	public synchronized void push(){
		while(true){
			if(stack.size()==20){
				try {
					this.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			char a = get();
			stack.add(a);
			System.out.println("压入:"+a);
			this.notifyAll();
			
		}
	}
	public synchronized void pull(){
		while(true){
			if(stack.size()==0){
				try {
					this.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
				char a = (char) stack.remove(0);
				System.out.println("弹出:"+a);
				this.notifyAll();
				
			
		}
	}
  public static void main(String[] args){
	Producer_Consumer pc = new Producer_Consumer();
	Thread producer = new Thread(){
		public void run(){
			while(true){
				pc.push();
				try {
					Thread.sleep(10000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}
	};
	producer.start();
	
	Thread consume = new Thread(){
		public void run(){
			while(true){
				pc.pull();
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}
	};
	consume.start();
  }
}

package gui;

import javax.swing.JFrame;
import javax.swing.JDialog;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import file.Hero;
import jdbc.HeroDAO;

public class TESTGUI{
	static HeroTableModel htm = new HeroTableModel();
	static JTable t = new JTable(htm);
	
	public static void main(String[] args){
		final JFrame f = new JFrame();
		f.setSize(400,300);
		f.setLocation(200,200);
		f.setLayout(new BorderLayout());
		
		t.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		t.getSelectionModel().setSelectionInterval(0, 0);
		JPanel pOperation = new JPanel();
		JButton bAdd = new JButton("增加");
		JButton bDelete = new JButton("删除");
		JButton bEdit = new JButton("编辑");
		pOperation.add(bAdd);
		pOperation.add(bDelete);
		pOperation.add(bEdit);
		
	    bAdd.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				new AddDialog(f).setVisible(true);
			}
	    });

	    
	  bDelete.addActionListener(new ActionListener(){

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			int index = t.getSelectedRow();
			if(-1==index){
				JOptionPane.showMessageDialog(f, "删除前需要先选中一行");
				return;
			}
			int option = JOptionPane.showConfirmDialog(f, "是否确认删除");
			if(JOptionPane.OK_OPTION==option){
				Hero h = htm.heros.get(index);
				int id = h.id;
				new HeroDAO().delete(id);
				JOptionPane.showMessageDialog(f, "删除成功");
				updateTable();
			}
		}
		  
	  });
	  
	  bEdit.addActionListener(new ActionListener(){

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			int Row = t.getSelectedRow();
			if(Row==-1){
				JOptionPane.showMessageDialog(f, "编辑前需选中一行");
			    return;
			}
			Hero h = htm.heros.get(Row);
			EditDialog ed = new EditDialog(f);
			ed.tfName.setText(h.name);
			ed.tfHp.setText(String.valueOf((int)h.hp));
			ed.setVisible(true);
		}
	  });
	    
		JScrollPane sp = new JScrollPane(t);
		f.add(sp,BorderLayout.CENTER);
		f.add(pOperation,BorderLayout.SOUTH);

		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setVisible(true);	
	}
	
	
	static class AddDialog extends JDialog{
		
		JLabel lName = new JLabel("名称");
		JLabel lHp = new JLabel("血量");
		
		JTextField tfName = new JTextField();
		JTextField tfHp = new JTextField();
		
		JButton bSubmit = new JButton("提交");
		
		AddDialog(JFrame f){
			super(f);
			this.setModal(true);
			int gap = 50;
			this.setLayout(null);
			
			JPanel pInput = new JPanel();
			JPanel pSubmit = new JPanel();
			pInput.setLayout(new GridLayout(2,2,gap,gap));
			pInput.add(lName);
			pInput.add(lHp);
			pInput.add(tfName);
			pInput.add(tfHp);
			pSubmit.add(bSubmit);
			
			pInput.setBounds(50,20,200,100);
			pSubmit.setBounds(0,130,300,150);
			
			this.add(pInput);
			this.add(pSubmit);
			this.setSize(300,200);
			this.setLocationRelativeTo(f);
			System.out.println(this);
			bSubmit.addActionListener(new ActionListener(){

				@Override
				public void actionPerformed(ActionEvent e) {
					// TODO Auto-generated method stub
					if(checkEmpty(tfName,"名称")){
						if(checkNumber(tfHp,"hp")){
							String name = tfName.getText();
							int hp = Integer.parseInt(tfHp.getText());
						    Hero h = new Hero();
						    h.name = name;
						    h.hp = hp;
						    
						    new HeroDAO().add(h);
						    JOptionPane.showMessageDialog(f,"提交成功");
						    AddDialog.this.setVisible(false);
						    updateTable();
						}
					}
				}
				
			});
		}
	}
	
static class EditDialog extends JDialog{
	
		JLabel lName = new JLabel("名称");
		JLabel lHp = new JLabel("血量");
		
		JTextField tfName = new JTextField();
		JTextField tfHp = new JTextField();
		
		JButton bSubmit = new JButton("提交");
		
		EditDialog(JFrame f){
			super(f);
			this.setModal(true);
			int gap = 50;
			this.setLayout(null);
			
			JPanel pInput = new JPanel();
			JPanel pSubmit = new JPanel();
			pInput.setLayout(new GridLayout(2,2,gap,gap));
			pInput.add(lName);
			pInput.add(lHp);

			pInput.add(tfName);
			pInput.add(tfHp);
			pSubmit.add(bSubmit);
			
			pInput.setBounds(50,20,200,100);
			pSubmit.setBounds(0,130,300,150);
			
			this.add(pInput);
			this.add(pSubmit);
			this.setSize(300,200);
			this.setLocationRelativeTo(f);
			System.out.println(this);
			bSubmit.addActionListener(new ActionListener(){

				@Override
				public void actionPerformed(ActionEvent e) {
					// TODO Auto-generated method stub
					if(checkEmpty(tfName,"名称")){
						if(checkNumber(tfHp,"hp")){
							
							int Row = t.getSelectedRow();
							Hero h = htm.heros.get(Row);
							
							String name = tfName.getText();
							int hp = Integer.parseInt(tfHp.getText());
						    Hero hero = new Hero();
						    hero.name = name;
						    hero.hp = hp;
						    hero.id = h.id;
						    
						    new HeroDAO().update(hero);
						    JOptionPane.showMessageDialog(f,"提交成功");
						    EditDialog.this.setVisible(false);
						    updateTable();
						}
					}
				}
				
			});
		}
	}
	
	public static void updateTable(){
		htm.heros = new HeroDAO().list();
		t.updateUI();
		if(!htm.heros.isEmpty()){
			t.getSelectionModel().setSelectionInterval(0, 0);
			
		}
	}
	private static boolean checkEmpty(JTextField tf,String msg){
		String value = tf.getText();
		if(value.length()==0){
			JOptionPane.showMessageDialog(null,msg+"不能为空");
		    return false;
		}
		return true;
	}
	private static boolean checkNumber(JTextField tf,String msg){
		String value = tf.getText();
		if(value.length()==0){
			JOptionPane.showMessageDialog(null, msg+"不能为空");
			return false;
		}
		try{
			Integer.parseInt(value);
		}catch(NumberFormatException e){
			JOptionPane.showMessageDialog(null, msg+"只能是整数");
			tf.grabFocus();
			return false;
		}
		return true;
	}

	
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值