📄 mainform.java
字号:
/*
* @author: Abraham1@163.com
* Copyright: NJUSoftware 2009
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class mainForm extends JFrame{
public int[] num=new int[82];
private static final long serialVersionUID = -8610433282926880253L;
/**
*
*/
//private boolean focused=false;
private JTextField[] textBoxes=new JTextField[81];
public mainForm(String title){
this.setTitle(title);
}
private void init(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel txtPanel =new JPanel(new GridLayout(9,9,5,5));
//Container frame=getContentPane();
this.add(txtPanel);
this.setLayout(null);
txtPanel.setBounds(25, 40, 300, 300);
for(int ix=0;ix<81;ix++)
{
JTextField newTextBox=new JTextField(2);
newTextBox.setHorizontalAlignment(JTextField.CENTER);
newTextBox.setFont(new Font("宋体",23, 23));
newTextBox.addKeyListener(new TextBoxEvent(newTextBox));
newTextBox.addFocusListener(new TextBoxFocusEvent(newTextBox));
txtPanel.add(newTextBox);
newTextBox.setName(Integer.toString(ix));
textBoxes[ix]=newTextBox;
}
JLabel label1=new JLabel("请输入要求解的数独.");
label1.setFont(new Font("宋体",15,20));
label1.setForeground(Color.BLUE );
label1.setBounds(20,5,200,30);
this.add(label1);
JLabel label2=new JLabel("Copyright NJUSoftware 2009 Author: Abraham");
label2.setFont(new Font("Times new roman",15,16));
label2.setForeground(Color.BLUE );
label2.setBounds(20,390,350,30);
this.add(label2);
JButton buttonOK=new JButton("计算");
buttonOK.setBounds(60,350,70,30);
buttonOK.addActionListener(new buttonEvent("calc"));
JButton buttonExit =new JButton("退出");
buttonExit.addActionListener(new buttonEvent("exit"));
buttonExit.setBounds(200,350,70,30);
this.add(buttonExit);
this.add(buttonOK);
this.setSize(350,450);
this.setResizable(false);
this.setLocation(new Point(300,200));
this.setVisible(true);
}
public static void main(String[] args){
mainForm myWindow=new mainForm("数独求解器");
myWindow.init();
}
private class buttonEvent implements ActionListener{
private String _name;
public buttonEvent(String name){
_name=name;
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if(_name=="exit")
System.exit(0);
else if(_name=="calc"){
boolean suc=calcSoduku();
if (suc==false)
JOptionPane.showMessageDialog(null, "数独无解,请重新输入!");
else{
for(int ix=0;ix<81;ix++){
textBoxes[ix].setText(Integer.toString(num[ix+1]));
}
}
}
// calcSudoku();
}
}
private boolean calcSoduku(){
for(int ix=0;ix<81;ix++){
String tmp=textBoxes[ix].getText();
if(tmp.isEmpty()==true)
tmp="0";
num[ix+1]=Integer.parseInt(tmp);
}
ShuDu newShudu=new ShuDu(num);
boolean success=newShudu.calcSoduku();
return success;
}
private class TextBoxEvent implements KeyListener{
private JTextField _source;
public TextBoxEvent(JTextField source){
_source=source;
}
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_LEFT){
int index=Integer.parseInt(_source.getName());
int newIndex;
if(index==0)
newIndex=80;
else
newIndex=index-1;
textBoxes[newIndex].grabFocus();;
}
else if(e.getKeyCode()==KeyEvent.VK_RIGHT){
int index=Integer.parseInt(_source.getName());
int newIndex;
if(index==80)
newIndex=1;
else
newIndex=index+1;
textBoxes[newIndex].grabFocus();;
}
}
public void keyReleased(KeyEvent arg0) {
}
public void keyTyped(KeyEvent e) {
if(_source.getText().length()>0){
if(_source.getSelectedText()==null){
e.consume();
Toolkit.getDefaultToolkit().beep();
}
}
if(e.getKeyChar()<'0'|| e.getKeyChar()>'9'){
e.consume();
Toolkit.getDefaultToolkit().beep();
}
}
}
private class TextBoxFocusEvent implements FocusListener{
private JTextField _source;
public TextBoxFocusEvent(JTextField source){
_source=source;
}
public void focusGained(FocusEvent arg0) {
_source.setSelectionStart(0);
_source.setSelectionEnd(1);
}
public void focusLost(FocusEvent arg0) {
if(_source.getText().isEmpty())
_source.setText("0");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -