java写的傻瓜记牌器

早就想写一个记牌器了

开始是想像网上许多高手说的那样读内存

可是由于技术水平有限

而且一直没找到java读内存、读 qq游戏信息的资料

所以之前只做了一个记牌器界面

到现在都没有实现读游戏内存方式的记牌器

 

希望有相关经验和资料的朋友能在这里多多指教

 

现在只实现了一个比较傻瓜的记牌器:

通过鼠标点击按钮来去掉玩家出过的牌

共有三个类,类结说明如下:

    LeaveCards:主要用语显示还没有出的牌型

    DropCard:JDialog,主要完成点击按钮去牌操作

    Card:自定义的JButton子类

…………………………………………………………

代码如下:

LeaveCards类

package card; import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.Rectangle; import java.awt.Toolkit; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.UIManager; public class LeaveCards extends JFrame{ /** * 记牌器类,显示未出的牌的信息 */ //两个label 放上家,下家出过的牌 private JLabel up; private JLabel down; private JLabel leave; //没出的牌 public JLabel jokers; //王 public JLabel two; //2 public JLabel ace; //A public JLabel king; //k public JLabel quee; //q public JLabel jack; //j public JLabel ten; //10 public JLabel nine; //9 public JLabel eight; //8 public JLabel seven; //7 public JLabel six; //6 public JLabel five; //5 public JLabel four; //4 public JLabel three; //3 public JLabel[] labels; // private DropCard drop; private Dimension size=Toolkit.getDefaultToolkit().getScreenSize();//得到屏幕的大小 private static final long serialVersionUID = 1L; public LeaveCards(){ super("傻瓜记牌器"); init(); } //初始化窗体 public void init(){ //初始化所有label drop = new DropCard(this);//去牌窗台 drop.setLocationRelativeTo(null);//居中 up=new JLabel("AQK10101010AAAQQQKKKJJJJ"); down=new JLabel("AQK10101010AAAQQQKKKJJJJ"); leave=new JLabel("剩余牌型:"); leave.setForeground(Color.BLUE); jokers=new JLabel(": 大王 小王"); jokers.setForeground(Color.RED); two=new JLabel(": 2 2 2 2"); ace=new JLabel(": A A A A"); king=new JLabel(": K K K K"); quee=new JLabel(": Q Q Q Q"); jack=new JLabel(": J J J J"); ten=new JLabel(":10101010"); nine=new JLabel(": 9 9 9 9"); eight=new JLabel(": 8 8 8 8"); seven=new JLabel(": 7 7 7 7"); six=new JLabel(": 6 6 6 6"); five=new JLabel(": 5 5 5 5"); four=new JLabel(": 4 4 4 4"); three=new JLabel(": 3 3 3 3"); labels=new JLabel[]{three,four,five,six,seven,eight,nine,ten,jack,quee,king,ace,two,jokers}; Container con=this.getContentPane(); //设置布局方式 con.setLayout(new GridLayout(19,1));//19行,一列 //添加控件到窗体 JLabel pup=new JLabel("上家出过的牌:"); pup.setForeground(Color.BLUE); add(pup); add(up); JLabel pdown=new JLabel("下家出过的牌:"); pdown.setForeground(Color.BLUE); con.add(pdown); con.add(down); con.add(leave); con.add(jokers); con.add(two); con.add(ace); con.add(king); con.add(quee); con.add(jack); con.add(ten); con.add(nine); con.add(eight); con.add(seven); con.add(six); con.add(five); con.add(four); con.add(three); //设置窗体显示位置、背景颜色和大小 con.setBackground(Color.ORANGE); this.setMinimumSize(new Dimension(200,290));//可拉伸的最小窗口200*290 this.setMaximizedBounds(new Rectangle(size.width-200, size.height/4, 200, 380));//用户单击窗口最大化时的大小 this.setAlwaysOnTop(true); //始终在其他窗口上方(相对) this.setBounds(size.width-200, size.height/4, 200, 380); //宽200,高280 //控制鼠标拖动改变窗口大小,窗口大小:最大时,宽200,高380 this.addComponentListener(new ComponentAdapter() { public void componentResized(ComponentEvent evt) { if(getWidth()>200||getHeight()>380) setSize(200,380); } }); //设置默认关闭操作,和窗体显示 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } public static void main(String [] arg){ try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) { } new LeaveCards(); } }

DropCard类

package card; import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTabbedPane; public class DropCard extends JDialog implements MouseListener { private static final long serialVersionUID = 1L; /** * 手动去掉玩家出过的牌 * @param args */ private LeaveCards frame; private int width = 350; //窗口宽 private int height = 180; //窗口高 private JTabbedPane tabs; private Card[] cards=new Card[15]; //牌按钮数组 public DropCard(LeaveCards frame) { super(frame); this.setTitle("去牌器"); this.frame = frame; //初始化自定义的牌按钮数组 for(int i=0;i1 && str.length()-count*2>=1){ //不能非法去牌 if(no==16){ str=frame.jokers.getText(); frame.jokers.setText(str.replace("小王", "")); } else if(no==17){ str=frame.jokers.getText(); frame.jokers.setText(str.replace("大王", "")); }else{ frame.labels[no-3].setText(str.substring(0,str.length()-count*2)); } } } /* * 更新剩余牌型 */ public void dropOne(int no) { String str=frame.labels[no-3].getText(); if(str.length()<3) return; if(no==16){ str=frame.jokers.getText(); if(str.length()>=3) frame.jokers.setText(str.replace("小王", "")); } else if(no==17){ str=frame.jokers.getText(); if(str.length()>=3) frame.jokers.setText(str.replace("大王", "")); }else{ frame.labels[no-3].setText(str.substring(0,str.length()-2)); } } public void mousePressed(MouseEvent e) { if (e.getButton() == 1) { Card card = (Card) e.getSource(); int no = card.getNo();//牌编号(3,4,5,6,7,8,9,10,11-J,12-Q,13-K,14-A,15-2,16-大王,17-小王) int count=tabs.getSelectedIndex();//数量 //for(int i=1;i<=index;i++) drop(no,count); } } public void mouseClicked(MouseEvent arg0) {} public void mouseEntered(MouseEvent arg0) {} public void mouseExited(MouseEvent arg0) {} public void mouseReleased(MouseEvent e) {} /** *添加所有按钮,2,3……大小王 */ public void addAllCards(JPanel panel) { panel.setLayout(new GridLayout(3, 5)); // 循环加载按钮 Card card; for (int i = 0; i < cards.length; i++) { card=new Card(cards[i]); card.addMouseListener(this);// 监听器为DropCard的实例 panel.add(card); } } /** * 只添加除去王以外的按钮 */ public void addAnly(JPanel panel) { panel.setLayout(new GridLayout(3, 5)); // 循环加载按钮 Card card; for (int i = 0; i

Card:类

package card; import javax.swing.JButton; public class Card extends JButton { /** * 牌型类 * 用于生成一张牌(3,4,5,6,7,8,9,10,11-J,12-Q,13-K,14-A,15-2,16-大王,17-小王) */ private static final long serialVersionUID = 1L; private int no;//牌编号(3,4,5,6,7,8,9,10,11-J,12-Q,13-K,14-A,15-2,16-大王,17-小王) public Card(String text,int no){ super(text); this.no=no; } //拷贝构造函数 public Card(Card card){ this(card.getText(),card.getNo()); } public int getNo(){ return no; } }

你可能感兴趣的:(java)