📄 shudu.java
字号:
package shudu;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import javax.swing.*;
/*
* Created by JFormDesigner on Wed Dec 31 17:41:39 CST 2008
*/
/**
* @author l h
*/
public class shudu extends JFrame {
/**
*
*/
private static final long serialVersionUID = 5522812837352627681L;
private JLabel selectedSquare;
private JLabel selectedNumber;
private Color previousNumbercolor;
public shudu() {
try {
UIManager.setLookAndFeel ("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
javax.swing.SwingUtilities.updateComponentTreeUI(this);
} catch(Exception e) {
e.printStackTrace();
}
selectedSquare = null;
selectedNumber = null;
previousNumbercolor = null;
initComponents();
for(int i=0;i<9;i++)
{
answer[i].setEnabled(false);
}
ok.setEnabled(false);
}
public void updateSquare(int sb,int sq,int number)
{
board[sb][sq].setText(number+"");
}
private void selectedSquare(MouseEvent e) {
if(selectedSquare != null)
{
selectedSquare.setIcon(null);
}
selectedSquare = (JLabel)e.getSource();
selectedSquare.setIcon(new ImageIcon("aim.GIF"));
for(int i=0;i<9;i++)
{
answer[i].setEnabled(true);
}
ok.setEnabled(true);
}
private void startActionPerformed(ActionEvent e) {
int[][] squ = new int[9][9];
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
if(board[i][j].getText().isEmpty())
squ[i][j]=0;
else
squ[i][j]= Integer.parseInt(board[i][j].getText());
}
}
board b = new board(this,squ);
b.start();
}
private void saveActionPerformed(ActionEvent e)
{
int[][] squ = new int[9][9];
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
if(board[i][j].getText().isEmpty())
squ[i][j]=0;
else
squ[i][j]= Integer.parseInt(board[i][j].getText());
}
}
ObjectOutputStream output;
try {
JFileChooser fileChooser=new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int result=fileChooser.showSaveDialog(shudu.this);//显示JFileChooser对话框
if(result==JFileChooser.CANCEL_OPTION)
return;
File fileName=fileChooser.getSelectedFile();
output = new ObjectOutputStream(new FileOutputStream(fileName));
output.writeObject(squ);
output.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
private void loadActionPerformed(ActionEvent e)
{
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
board[i][j].setText("");
}
}
ObjectInputStream input;
int[][] squ;
try {
JFileChooser fileChooser=new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int result=fileChooser.showSaveDialog(shudu.this);//显示JFileChooser对话框
if(result==JFileChooser.CANCEL_OPTION)
return;
File fileName=fileChooser.getSelectedFile();
input =new ObjectInputStream(new FileInputStream(fileName));
try {
squ = (int[][])input.readObject();
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
if(squ[i][j]!=0)
board[i][j].setText(squ[i][j]+"");
}
}
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
private void selectedNumber(MouseEvent e) {
if(selectedNumber != null)
{
selectedNumber.setForeground(previousNumbercolor);
}
selectedNumber = (JLabel)e.getSource();
previousNumbercolor = selectedNumber.getForeground();
selectedNumber.setForeground(Color.blue);
}
private void changeSquare(MouseEvent e) {
if(!ok.isEnabled())return;
if(selectedNumber == null)
{JOptionPane.showMessageDialog(null, "请选个数字");
return;
}
selectedSquare.setIcon(null);
selectedSquare.setText(selectedNumber.getText());
for(int i=0;i<9;i++)
{
answer[i].setEnabled(false);
}
ok.setEnabled(false);
}
private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
// Generated using JFormDesigner Evaluation license - l h
boardPanel = new JPanel();
boardbackground = new JLabel();
start = new JButton();
AnswerPanel = new JPanel();
ok = new JButton();
answerPanelBackground = new JLabel();
save = new JButton();
load = new JButton();
//======== this ========
setTitle("\u6570\u72ec");
Container contentPane = getContentPane();
contentPane.setLayout(null);
//======== boardPanel ========
{
// JFormDesigner evaluation mark
boardPanel.setBorder(new javax.swing.border.CompoundBorder(
new javax.swing.border.TitledBorder(new javax.swing.border.EmptyBorder(0, 0, 0, 0),
"JFormDesigner Evaluation", javax.swing.border.TitledBorder.CENTER,
javax.swing.border.TitledBorder.BOTTOM, new java.awt.Font("Dialog", java.awt.Font.BOLD, 12),
java.awt.Color.red), boardPanel.getBorder())); boardPanel.addPropertyChangeListener(new java.beans.PropertyChangeListener(){public void propertyChange(java.beans.PropertyChangeEvent e){if("border".equals(e.getPropertyName()))throw new RuntimeException();}});
boardPanel.setLayout(null);
//---- board ----
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
board[i][j]=new JLabel();
if(i%2!=0)
{
board[i][j].setForeground(Color.white);
}else
board[i][j].setForeground(Color.black);
board[i][j].setHorizontalAlignment(SwingConstants.CENTER);
board[i][j].addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
selectedSquare(e);
}
});
board[i][j].setBounds(i%3*120+j%3*40, i/3*120+j/3*40, 40, 40);
boardPanel.add(board[i][j]);
}
}
//---- boardbackground ----
boardbackground.setIcon(new ImageIcon("board.JPG"));
boardPanel.add(boardbackground);
boardbackground.setBounds(0, 0, 360, 360);
{ // compute preferred size
Dimension preferredSize = new Dimension();
for(int i = 0; i < boardPanel.getComponentCount(); i++) {
Rectangle bounds = boardPanel.getComponent(i).getBounds();
preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
}
Insets insets = boardPanel.getInsets();
preferredSize.width += insets.right;
preferredSize.height += insets.bottom;
boardPanel.setMinimumSize(preferredSize);
boardPanel.setPreferredSize(preferredSize);
}
}
contentPane.add(boardPanel);
boardPanel.setBounds(10, 10, 360, 360);
//---- start ----
start.setText("\u5f00\u59cb ");
start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
startActionPerformed(e);
}
});
contentPane.add(start);
start.setBounds(390, 15, 90, 25);
//---- save ----
save.setText("保存");
save.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
saveActionPerformed(e);
}
});
contentPane.add(save);
save.setBounds(390, 45, 90, 25);
//---- load ----
load.setText("载入");
load.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
loadActionPerformed(e);
}
});
contentPane.add(load);
load.setBounds(390, 75, 90, 25);
//======== AnswerPanel ========
{
AnswerPanel.setLayout(null);
//---- answer ----
for(int i=0;i<9;i++)
{
answer[i] = new JLabel();
answer[i].setText((i+1)+"");
if(i%2==0)
answer[i].setForeground(Color.white);
else
answer[i].setForeground(Color.black);
answer[i].setHorizontalAlignment(SwingConstants.CENTER);
answer[i].addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
selectedNumber(e);
}
});
AnswerPanel.add(answer[i]);
answer[i].setBounds(i%3*40,i/3*40, 40, 40);
}
//---- ok ----
ok.setText("\u786e\u5b9a");
ok.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
changeSquare(e);
}
});
AnswerPanel.add(ok);
ok.setBounds(new Rectangle(new Point(60, 125), ok.getPreferredSize()));
//---- answerPanelBackground ----
answerPanelBackground.setIcon(new ImageIcon("answerPanel.JPG"));
answerPanelBackground.setVerticalAlignment(SwingConstants.TOP);
AnswerPanel.add(answerPanelBackground);
answerPanelBackground.setBounds(0, 0, 120, 150);
{ // compute preferred size
Dimension preferredSize = new Dimension();
for(int i = 0; i < AnswerPanel.getComponentCount(); i++) {
Rectangle bounds = AnswerPanel.getComponent(i).getBounds();
preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
}
Insets insets = AnswerPanel.getInsets();
preferredSize.width += insets.right;
preferredSize.height += insets.bottom;
AnswerPanel.setMinimumSize(preferredSize);
AnswerPanel.setPreferredSize(preferredSize);
}
}
contentPane.add(AnswerPanel);
AnswerPanel.setBounds(new Rectangle(new Point(380, 220), AnswerPanel.getPreferredSize()));
{ // compute preferred size
Dimension preferredSize = new Dimension();
for(int i = 0; i < contentPane.getComponentCount(); i++) {
Rectangle bounds = contentPane.getComponent(i).getBounds();
preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
}
Insets insets = contentPane.getInsets();
preferredSize.width += insets.right;
preferredSize.height += insets.bottom;
contentPane.setMinimumSize(preferredSize);
contentPane.setPreferredSize(preferredSize);
}
setSize(515, 420);
setLocationRelativeTo(getOwner());
// JFormDesigner - End of component initialization //GEN-END:initComponents
}
// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables
// Generated using JFormDesigner Evaluation license - l h
private JPanel boardPanel;
private JLabel boardbackground;
private JButton start;
private JPanel AnswerPanel;
private JButton ok;
private JLabel answerPanelBackground;
private JLabel[][] board=new JLabel[9][9];
private JLabel[] answer = new JLabel[9];
private JButton save;
private JButton load;
// JFormDesigner - End of variables declaration //GEN-END:variables
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -