用Java实现周易算卦

 一、源代码

/**

 * 摇一卦:根据周易原理实现算卦

 *

 * @author YangYong

 *

 */

import java.awt.Font;

import java.awt.GridLayout;

import java.awt.Toolkit;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.Random;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JTextField;

 

public class Fortune_telling {

  private JFrame frame = null;

  private JLabel[] label = null;

  private JTextField[] text = null;

  private JButton[] button = null;

 

  public static void main(String[] args) {

     newFortune_telling().myHandler();

  }

 

  public Fortune_telling() {

     frame = new JFrame("摇一卦");

     int height, widht;

     height = Toolkit.getDefaultToolkit().getScreenSize().height;

     widht = Toolkit.getDefaultToolkit().getScreenSize().width;

     frame.setBounds((widht - 500) >> 2, (height - 309) >> 2, 500,309);

     label = new JLabel[2];

     text = new JTextField[2];

     button = new JButton[2];

     frame.setLayout(new GridLayout(3, 2));

     label[0] = new JLabel("卦名:");

     label[1] = new JLabel("爻序(从下到上):");

     text[0] = new JTextField();

     text[1] = new JTextField();

     button[0] = new JButton("清空");

     button[1] = new JButton("摇一卦");

     int i;

     for (i = 0; i < label.length; i++) {

       frame.add(label[i]);

       text[i].setEditable(false);

       text[i].setFont(new Font(Font.DIALOG, Font.BOLD, 20));

       frame.add(text[i]);

     }

     frame.add(button[0]);

     frame.add(button[1]);

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

     frame.setResizable(false);

     frame.setVisible(true);

  }

 

  public void myHandler() {

     String[] gua= newString[] { "", "", "", "", "", "", "", "", "小畜", "", "", "", "同人", "大有", "", "",

         "", "", "", "", "噬嗑", "", "", "", "无妄", "大畜", "", "大过", "", "", "", "", "", "大壮", "",

         "明夷", "家人", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "归妹", "",

         "", "", "", "", "", "中孚", "小过", "既济", "未济" };

     button[0].addActionListener(new ActionListener() {

       @Override

       public voidactionPerformed(ActionEvent e) {

         text[0].setText(null);

         text[1].setText(null);

       }

     });

     button[1].addActionListener(new ActionListener() {

       @Override

       public voidactionPerformed(ActionEvent e) {

         Randomra = new Random();

         text[0].setText(gua[ra.nextInt(gua.length)] + "");

         text[1].setText("" + (ra.nextInt(6) + 1));

       }

     });

  }

}

二、运行与测试

用Java实现周易算卦_第1张图片

运行效果

你可能感兴趣的:(java,周易)