סגירה של Frame עם Button - תכנות - HWzone פורומים
עבור לתוכן
  • צור חשבון

סגירה של Frame עם Button


Hacker1221

Recommended Posts

/*suduko.java - software that sloves suduko
the program use an image and listen to cilcks on it.
when the user cilck on the image, He can type number in 1-9 in a textfield.
after he finish enter all the numbers in the suduko table, the program check the suduko table.
if impossible suduko workable table, the program send an error message.
if it possible, the program start workable the suduko.
*/
/**
*
* @author Yiftach Shlezinger
* @version 1
*/
package sudukoworkable;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SudukoWorkable {
//arrays of the coordinates of the images
int [] xcoord = new int [9];
int [] ycoord = new int [9];
//the frame of the software
JFrame f=new JFrame("SudukoWorkable") {
public void paint(Graphics g) {
g.setFont(new Font("Times New Roman", Font.BOLD, 15));
g.drawImage(image, 0, 100, null);
for (int x=0; x<9; x++) {
for (int y=0; y<9; y++) {
if (sudukotable[x][y]>0) {
g.drawString(""+sudukotable[x][y], xcoord[x], ycoord[y]);
}
}
}
}
};
public void closeerror () {
error.setEnabled(false);


}
//button of exit the program
JButton exit=new JButton("exit");
//Button when the user is finished enter the numbers
JButton ready=new JButton("ready");
//frame for error messages
JFrame error=new JFrame("error");
//panel for error messages
JPanel pan=new JPanel();
//label for error messages
JLabel lab=new JLabel("error");
//button when the user is finished read the error message
JButton ok=new JButton("ok");
//the suduko table image
Image image=f.getToolkit().getImage("D:/Users/משפחת שלזינגר/Desktop/suduko.jpg");
//textfields for the 1-9 coordinates
JTextField xtext=new JTextField();
JTextField ytext=new JTextField();
//textfield for the number value 1-9
JTextField val=new JTextField();
//button for send the number to the suduko table
JButton send=new JButton("send");
//array for the suduko table
int [][] sudukotable = new int [9][9];
//the boxes of the suduko
int [][] box11 = new int [3][3];
int [][] box12 = new int [3][3];
int [][] box13 = new int [3][3];
int [][] box21 = new int [3][3];
int [][] box22 = new int [3][3];
int [][] box23 = new int [3][3];
int [][] box31 = new int [3][3];
int [][] box32 = new int [3][3];
int [][] box33 = new int [3][3];
//method for the listeners
public void init_buttons () {
//buttons array of all the buttons in the code
JButton [] buttons = {exit, ready, send, ok};
//loop for the listeners of the buttons
for (JButton b: buttons) {
//add listener for the button
b.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
//add listener for the exit button
if (((JButton)e.getSource()).getActionCommand().equalsIgnoreCase("exit")) {
System.exit(0);
}
//add listener for the send button
else if (((JButton)e.getSource()).getActionCommand().equalsIgnoreCase("send")){
val.setEditable(false);
writesend();


}
else if (((JButton)e.getSource()).getActionCommand().equalsIgnoreCase("ok")) {
closeerror();
}
//add listener for the ready button
else {
check();
}
}
});
}
//add listener for the ok button
//add the ok button to the error frame
//add listener for the image
f.addMouseListener(
new MouseAdapter () {
public void mousePressed(MouseEvent e) {
//y-100 - because the image's y coordinate is 100
write(e.getX(), e.getY()-100);
}
}
);
}
//function for send numbers to the suduko table
/**
*
* @param x for the x coordinate of the clicked
* @param y for the y coordinate of the clicked
*/
public void write (int x, int y) {
//xx for the x coordinate of the suduko table
//yy for the x coordinate of the suduko table
int yy=0; int xx=0;
//see if the click is on the image
if (y<=0 || y>219) { return; }
if (x==0 || x>219) {return; }
//check which square of the suduko table it is
if (y<26) {
yy=0;
ycoord[0]=126;
}
else if (y>27 && y<49) {
yy=1;
ycoord[1]=149;
}
else if (y>50 && y<73) {
yy=2;
ycoord[2]=173;
}
else if (y>74 && y<97) {
yy=3;
ycoord[3]=197;
}
else if (y>98 && y<121) {
yy=4;
ycoord[4]=221;
}
else if (y>122 && y<145) {
yy=5;
ycoord[5]=245;
}
else if (y>146 && y<169) {
yy=6;
ycoord[6]=269;
}
else if (y>170 && y<193) {
yy=7;
ycoord[7]=293;
}
else if (y>194 && y<217) {
yy=8;
ycoord[8]=317;
}
else {
return;
}
if (x<26) {
xx=0;
xcoord[0]=126;
}
else if (x>27&& x<50) {
xx=1;
xcoord[1]=150;
}
else if (x>51 && x<73) {
xx=2;
xcoord[2]=173;
}
else if (x>75 && x<98) {
xx=3;
xcoord[3]=198;
}
else if (x>99 && x<122) {
xx=4;
xcoord[4]=222;
}
else if (x>123 && x<145) {
xx=5;
xcoord[5]=245;
}
else if (x>147 && x<169) {
xx=6;
xcoord[6]=269;
}
else if (x>171 && x<193) {
xx=7;
xcoord[7]=293;
}
else if (x>195 && x<217) {
xx=8;
xcoord[8]=317;
}
else {
return;
}
//set the value of the text fields
xtext.setText(""+(xx+1));
ytext.setText(""+(yy+1));
//let pass to the text field
val.setEditable(true);
}
//check method - check if the string is an integer
/**
*
* @param s string for check
* @return if the string is number and the number is 1-9
*/
public boolean checkint (String s) {
//number for implement
int num=0;
/**
* @throws check if the string is a number
*/
try {
//implement the string's value to the integer
num=Integer.parseInt(s);
}
//check if the string is a number
catch(NumberFormatException e) {
return false;
}
//check if the number is 1-9
if (num>0 && num<10)
return true;
//return false if the number isn't 1-9
return false;

}
//the contruction
public SudukoWorkable () {
init();
}
//the init method
public void init () {
//set the icon image of the software frame
f.setIconImage(image);
//create the action command of the buttons
exit.setActionCommand("exit");
ready.setActionCommand("ready");
ok.setActionCommand("ok");
send.setActionCommand("send");
//set the edit pass to the text fields
xtext.setEditable(false);
xtext.setSize(10, 10);
ytext.setEditable(false);
ytext.setSize(10, 10);
val.setEditable(false);
val.setSize(10, 10);
//set the layout of the frame
f.setLayout(new FlowLayout());
//use the init buttons method
init_buttons();
//add the buttons and the text fields in the frame
f.add(xtext);
f.add(ytext);
f.add(val);
f.add(exit);
f.add(ready);
f.add(send);
//add the error label in the error panel
pan.add(lab);
//set the layout of the error frame
error.setLayout(new FlowLayout());
//add the error panel and the ok button in the error frame
error.add(pan);
error.add(ok);
//draw the image on 0,100
//set the sizv of the frame
f.setSize(500,500);
//set the size of the error frame
error.setSize(300, 300);
f.setResizable(false);
error.setResizable(false);
//open the frame
f.setBackground(Color.yellow);


f.setVisible(true);
}
//method for send error messages
/**
*
* @param errormessage the error message
*/
public void error (String errormessage) {
//set the text of the label
lab.setText("error: " + errormessage);
//open the error message
error.setVisible(true);
}
/*method for check if the texts in the text fields
are numbers and if it can be on the table and draw it on the table
*/
public void writesend()
{
//check if the texts on the text fields are numbers of 1-9
if (!(checkint(xtext.getText()) && checkint(ytext.getText()) && checkint(val.getText())
) ) {
//send an error message
error("enter number 1-9");
//Block access to the text field
val.setEditable(false);
//end the method
return; }
//the value of the number
int num=0;
//the value of the x coordinate
int x=0;
//the value of the y coordinate
int y=0;
//set the values
try {
num=Integer.parseInt(val.getText());
x=Integer.parseInt(xtext.getText());
y=Integer.parseInt(ytext.getText());
}
//if there is problem
catch(NumberFormatException e) {
error("problem with the numbers");
val.setEditable(false);
return;
}
//check if the number can enter the table
for (int xfor=0; xfor<9; xfor++) {
if (sudukotable[xfor][y-1]==num) {
if (xfor!=x-1) {
error("cannot put " + num + " here");
val.setEditable(false);
return;
}
}
}
for (int yfor=0; yfor<9; yfor++) {
if(sudukotable[x-1][yfor]==num) {
if (yfor!=y-1) {
error ("cannot put " + num + " here");
val.setEditable(false);
return;
}
}
}
int xbox=0; int ybox=0; int box=0;
if(x==1 || x==2 || x==3) {
xbox=1;
}
if(x==4 || x==5 || x==6) {
xbox=2;
}
if (x==7 || x==8 || x==9) {
xbox=3;
}
if (y==1 || y==2 || y==3) {
ybox=1;
}
if (y==4 || y==5 || y==6) {
ybox=2;
}
if (y==7 || y==8 || y==9) {
ybox=3;
}
if (xbox==1 && ybox==1) {
for (int xfor=0; xfor<3; xfor++) {
for (int yfor=0; yfor<3; yfor++) {
if (box11[xfor][yfor]==num) {
if (!(xfor==x-1 && yfor==y-1)) {
error ("cannot put " + num + " here");
val.setEditable(false);
return;
}
}
}
}
}
if (xbox==1 && ybox==2) {
for (int xfor=0; xfor<3; xfor++) {
for (int yfor=0; yfor<3; yfor++) {
if (box12[xfor][yfor]==num) {
if (!(xfor==x-1 && yfor==y-1)) {
error ("cannot put " + num + " here");
val.setEditable(false);
return;
}
}
}
}
}
if (xbox==1 && ybox==3) {
for (int xfor=0; xfor<3; xfor++) {
for (int yfor=0; yfor<3; yfor++) {
if (box13[xfor][yfor]==num) {
if (!(xfor==x-1 && yfor==y-1)) {
error ("cannot put " + num + " here");
val.setEditable(false);
return;
}
}
}
}
}
if (xbox==2 && ybox==1) {
for (int xfor=0; xfor<3; xfor++) {
for (int yfor=0; yfor<3; yfor++) {
if (box21[xfor][yfor]==num) {
if (!(xfor==x-1 && yfor==y-1)) {
error ("cannot put " + num + " here");
val.setEditable(false);
return;
}
}
}
}
}
if (xbox==2 && ybox==2) {
for (int xfor=0; xfor<3; xfor++) {
for (int yfor=0; yfor<3; yfor++) {
if (box22[xfor][yfor]==num) {
if (!(xfor==x-1 && yfor==y-1)) {
error ("cannot put " + num + " here");
val.setEditable(false);
return;
}
}
}
}
}
if (xbox==2 && ybox==3) {
for (int xfor=0; xfor<3; xfor++) {
for (int yfor=0; yfor<3; yfor++) {
if (box23[xfor][yfor]==num) {
if (!(xfor==x-1 && yfor==y-1)) {
error ("cannot put " + num + " here");
val.setEditable(false);
return;
}
}
}
}
}
if (xbox==3 && ybox==1) {
for (int xfor=0; xfor<3; xfor++) {
for (int yfor=0; yfor<3; yfor++) {
if (box31[xfor][yfor]==num) {
if (!(xfor==x-1 && yfor==y-1)) {
error ("cannot put " + num + " here");
val.setEditable(false);
return;
}
}
}
}
}
if (xbox==3 && ybox==2) {
for (int xfor=0; xfor<3; xfor++) {
for (int yfor=0; yfor<3; yfor++) {
if (box32[xfor][yfor]==num) {
if (!(xfor==x-1 && yfor==y-1)) {
error ("cannot put " + num + " here");
val.setEditable(false);
return;
}
}
}
}
}
if (xbox==3 && ybox==3) {
for (int xfor=0; xfor<3; xfor++) {
for (int yfor=0; yfor<3; yfor++) {
if (box33[xfor][yfor]==num) {
if (!(xfor==x-1 && yfor==y-1)) {
error ("cannot put " + num + " here");
val.setEditable(false);
return;
}
}
}
}
}
//enter the number to the suduko table
sudukotable[x-1][y-1]=num;
if (xbox==1 && ybox==1) {
int xx=0;
int yy=0;
if (x==1)
xx=0;
if (x==2)
xx=1;
if (x==3)
xx=2;
if (y==1)
yy=0;
if (y==2)
yy=1;
if (y==3)
yy=2;
box11[xx][yy]=num;
}
if (xbox==1 && ybox==2) {
int xx=0;
int yy=0;
if (x==1)
xx=0;
if (x==2)
xx=1;
if (x==3)
xx=2;
if (y==4)
yy=0;
if (y==5)
yy=1;
if (y==6)
yy=2;
box12[xx][yy]=num;
}
if (xbox==1 && ybox==3) {
int xx=0;
int yy=0;
if (x==1)
xx=0;
if (x==2)
xx=1;
if (x==3)
xx=2;
if (y==7)
yy=0;
if (y==8)
yy=1;
if (y==9)
yy=2;
box13[xx][yy]=num;
}
if (xbox==2 && ybox==1) {
int xx=0;
int yy=0;
if (x==4)
xx=0;
if (x==5)
xx=1;
if (x==6)
xx=2;
if (y==1)
yy=0;
if (y==2)
yy=1;
if (y==1)
yy=2;
box21[xx][yy]=num;
}
if (xbox==2 && ybox==2) {
int xx=0;
int yy=0;
if (x==4)
xx=0;
if (x==5)
xx=1;
if (x==6)
xx=2;
if (y==4)
yy=0;
if (y==5)
yy=1;
if (y==6)
yy=2;
box22[xx][yy]=num;
}
if (xbox==2 && ybox==3) {
int xx=0;
int yy=0;
if (x==4)
xx=0;
if (x==5)
xx=1;
if (x==6)
xx=2;
if (y==7)
yy=0;
if (y==8)
yy=1;
if (y==9)
yy=2;
box23[xx][yy]=num;
}
if (xbox==3 && ybox==1) {
int xx=0;
int yy=0;
if (x==7)
xx=0;
if (x==8)
xx=1;
if (x==9)
xx=2;
if (y==1)
yy=0;
if (y==2)
yy=1;
if (y==3)
yy=2;
box31[xx][yy]=num;
}
if (xbox==3 && ybox==2) {
int xx=0;
int yy=0;
if (x==7)
xx=0;
if (x==8)
xx=1;
if (x==9)
xx=2;
if (y==4)
yy=0;
if (y==5)
yy=1;
if (y==6)
yy=2;
box32[xx][yy]=num;
}
if (xbox==3 && ybox==3) {
int xx=0;
int yy=0;
if (x==7)
xx=0;
if (x==8)
xx=1;
if (x==9)
xx=2;
if (y==7)
yy=0;
if (y==8)
yy=1;
if (y==9)
yy=2;
box33[xx][yy]=num;
}
//draw the number on the image
Graphics g=f.getGraphics();
g.setFont(new Font("Times New Roman", Font.BOLD, 15));
for (int xx=0; xx<9; xx++) {
for (int yy=0; yy<9; yy++) {
if (sudukotable[xx][yy]>0) {
g.drawString(""+sudukotable[xx][yy], xcoord[xx], ycoord[yy]);
}
}
}
}
//check the suduko table
public void check(){


}


/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
SudukoWorkable sw=new SudukoWorkable();
}

}


זה כפתור שנמצא בתוך הודעת שגיאה שסוגר את ההודעה

else if (((JButton)e.getSource()).getActionCommand().equalsIgnoreCase("ok")) {error.setEnables(false);
}

קישור לתוכן
שתף באתרים אחרים

לא שזה משנה במיוחד, אבל למה אתה יוצר את אותו ActionListener מחדש שוב ושוב לכל כפתור? או שתיצור אחד ותתן אותו לכל הכפתורים, או שלכל כפתור תיצור ActionListener עבורו (ואז גם לא תצטרך את סדרת ה-if/else שיש בו).

בכל מקרה עשית שבלחיצה על הכפתור (error.setEnabled(false, שזה עושה בדיוק מה שאתה מצפה לו - הופך את הפריים ללא פעיל.

קישור לתוכן
שתף באתרים אחרים

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.

×
  • צור חדש...