我正在尝试在paint方法中更改名为label11的JLabel的位置。我将它添加到我在Sudoku中扩展的JFrame中,它只能打印一次,而不是三次。当我运行它时,它会进入顶部中间,并打印三次。如何将JLabel的位置设置为(x,y)坐标,并且只打印一次。此外,如果你取消注释所有打印出矩形的线条,那么它应该制作一个数独板,每个盒子里面会有一个数字。我的代码如下。任何帮助表示赞赏。谢谢!
路加福音
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import java.awt.FlowLayout;
import javax.swing.*;
import java.awt.BorderLayout;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.CardLayout;
import java.awt.Rectangle;
import java.awt.Canvas;
import java.awt.Dimension;
import javax.swing.border.LineBorder;
public class Soduku extends JFrame{
static JFrame frame = new JFrame("Dots and Boxes");
static JPanel panel = new JPanel();
public Soduku() {
//super("Draw A Rectangle In JFrame");
setSize(new Dimension(804, 804));
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public void paint(Graphics g) {
/* g.setColor(Color.black);
g.fillRect(67,67, 603, 9);
g.setColor(Color.black);
g.fillRect(67,134, 603, 3);
g.setColor(Color.black);
g.fillRect(67,201, 603, 3);
g.setColor(Color.black);
g.fillRect(67,268, 603, 9);
g.setColor(Color.black);
g.fillRect(67,335, 603, 3);
g.setColor(Color.black);
g.fillRect(67,402, 603, 3);
g.setColor(Color.black);
g.fillRect(67,469, 603, 9);
g.setColor(Color.black);
g.fillRect(67,536, 603, 3);
g.setColor(Color.black);
g.fillRect(67,603, 603, 3);
g.setColor(Color.black);
g.fillRect(67,670, 612, 9);
g.setColor(Color.black);
g.fillRect(67,67, 9, 603);
g.setColor(Color.black);
g.fillRect(134,67, 3, 603);
g.setColor(Color.black);
g.fillRect(201,67, 3, 603);
g.setColor(Color.black);
g.fillRect(268,67, 9, 603);
g.setColor(Color.black);
g.fillRect(335,67, 3, 603);
g.setColor(Color.black);
g.fillRect(402,67, 3, 603);
g.setColor(Color.black);
g.fillRect(469,67, 9, 603);
g.setColor(Color.black);
g.fillRect(536,67, 3, 603);
g.setColor(Color.black);
g.fillRect(603,67, 3, 603);
g.setColor(Color.black);
g.fillRect(670,67, 9, 603);
*/
JLabel label11;JLabel label21;JLabel label31;JLabel label41;JLabel label51;JLabel label61;JLabel label71;JLabel label81;JLabel label91;
JLabel label12;JLabel label22;JLabel label32;JLabel label42;JLabel label52;JLabel label62;JLabel label72;JLabel label82;JLabel label92;
JLabel label13;JLabel label23;JLabel label33;JLabel label43;JLabel label53;JLabel label63;JLabel label73;JLabel label83;JLabel label93;
JLabel label14;JLabel label24;JLabel label34;JLabel label44;JLabel label54;JLabel label64;JLabel label74;JLabel label84;JLabel label94;
JLabel label15;JLabel label25;JLabel label35;JLabel label45;JLabel label55;JLabel label65;JLabel label75;JLabel label85;JLabel label95;
JLabel label16;JLabel label26;JLabel label36;JLabel label46;JLabel label56;JLabel label66;JLabel label76;JLabel label86;JLabel label96;
JLabel label17;JLabel label27;JLabel label37;JLabel label47;JLabel label57;JLabel label67;JLabel label77;JLabel label87;JLabel label97;
JLabel label18;JLabel label28;JLabel label38;JLabel label48;JLabel label58;JLabel label68;JLabel label78;JLabel label88;JLabel label98;
JLabel label19;JLabel label29;JLabel label39;JLabel label49;JLabel label59;JLabel label69;JLabel label79;JLabel label89;JLabel label99;
String string11 = "0";String string21 = "0";String string31 = "0";String string41 = "0";String string51 = "0";String string61 = "0";String string71 = "0";String string81 = "0";String string91 = "0";
String string12 = "0";String string22 = "0";String string32 = "0";String string42 = "0";String string52 = "0";String string62 = "0";String string72 = "0";String string82 = "0";String string92 = "0";
String string13 = "0";String string23 = "0";String string33 = "0";String string43 = "0";String string53 = "0";String string63 = "0";String string73 = "0";String string83 = "0";String string93 = "0";
String string14 = "0";String string24 = "0";String string34 = "0";String string44 = "0";String string54 = "0";String string64 = "0";String string74 = "0";String string84 = "0";String string94 = "0";
String string15 = "0";String string25 = "0";String string35 = "0";String string45 = "0";String string55 = "0";String string65 = "0";String string75 = "0";String string85 = "0";String string95 = "0";
String string16 = "0";String string26 = "0";String string36 = "0";String string46 = "0";String string56 = "0";String string66 = "0";String string76 = "0";String string86 = "0";String string96 = "0";
String string17 = "0";String string27 = "0";String string37 = "0";String string47 = "0";String string57 = "0";String string67 = "0";String string77 = "0";String string87 = "0";String string97 = "0";
String string18 = "0";String string28 = "0";String string38 = "0";String string48 = "0";String string58 = "0";String string68 = "0";String string78 = "0";String string88 = "0";String string98 = "0";
String string19 = "0";String string29 = "0";String string39 = "0";String string49 = "0";String string59 = "0";String string69 = "0";String string79 = "0";String string89 = "0";String string99 = "0";
//Columns left to right
String[] column1;
column1 = new String [10]; //Column 1
String[] column2;
column2 = new String [10]; //Column 2
String[] column3;
column3 = new String [10]; //Column 3
String[] column4;
column4 = new String [10]; //Column 4
String[] column5;
column5 = new String [10]; //Column 5
String[] column6;
column6 = new String [10]; //Column 6
String[] column7;
column7 = new String [10]; //Column 7
String[] column8;
column8 = new String [10]; //Column 8
String[] column9;
column9 = new String [10]; //Column 8
//Rows Top to Bottum
String[] row1;
row1 = new String [10]; //Row 1
String[] row2;
row2 = new String [10]; //Row 2
String[] row3;
row3 = new String [10]; //Row 3
String[] row4;
row4 = new String [10]; //Row 4
String[] row5;
row5 = new String [10]; //Row 5
String[] row6;
row6 = new String [10]; //Row 6
String[] row7;
row7 = new String [10]; //Row 7
String[] row8;
row8 = new String [10]; //Row 8
String[] row9;
row9 = new String [10]; //Row 8
//All boxes (Top Left, Top Middle, Top Right, Middle Left, Middle, Middle Right, Buttom Left, Buttom Middle, Buttom Right)
String[] box1;
box1 = new String [10]; //box 1
String[] box2;
box2 = new String [10]; //box 2
String[] box3;
box3 = new String [10]; //box 3
String[] box4;
box4 = new String [10]; //box 4
String[] box5;
box5 = new String [10]; //box 5
String[] box6;
box6 = new String [10]; //box 6
String[] box7;
box7 = new String [10]; //box 7
String[] box8;
box8 = new String [10]; //box 8
String[] box9;
box9 = new String [10]; //box 9
//Defining the Columns
column1[1] = string11; column1[2] = string12; column1[3] = string13; column1[4] = string14; column1[5] = string15; column1[6] = string16; column1[7] = string17; column1[8] = string18; column1[9] = string19;
column2[1] = string21; column2[2] = string22; column2[3] = string23; column2[4] = string24; column2[5] = string25; column2[6] = string26; column2[7] = string27; column2[8] = string28; column2[9] = string29;
column3[1] = string31; column3[2] = string32; column3[3] = string33; column3[4] = string34; column3[5] = string35; column3[6] = string36; column3[7] = string37; column3[8] = string38; column3[9] = string39;
column4[1] = string41; column4[2] = string42; column4[3] = string43; column4[4] = string44; column4[5] = string45; column4[6] = string46; column4[7] = string47; column4[8] = string48; column4[9] = string49;
column5[1] = string51; column5[2] = string52; column5[3] = string53; column5[4] = string54; column5[5] = string55; column5[6] = string56; column5[7] = string57; column5[8] = string58; column5[9] = string59;
column6[1] = string61; column6[2] = string62; column6[3] = string63; column6[4] = string64; column6[5] = string65; column6[6] = string66; column6[7] = string67; column6[8] = string68; column6[9] = string69;
column7[1] = string71; column7[2] = string72; column7[3] = string73; column7[4] = string74; column7[5] = string75; column7[6] = string76; column7[7] = string77; column7[8] = string78; column7[9] = string79;
column8[1] = string81; column8[2] = string82; column8[3] = string83; column8[4] = string84; column8[5] = string85; column8[6] = string86; column8[7] = string87; column8[8] = string88; column8[9] = string89;
column9[1] = string91; column9[2] = string92; column9[3] = string93; column9[4] = string94; column9[5] = string95; column9[6] = string96; column9[7] = string97; column9[8] = string98; column9[9] = string99;
row1[1] = string11; row1[2] = string21; row1[3] = string31; row1[4] = string41; row1[5] = string51; row1[6] = string61; row1[7] = string71; row1[8] = string81; row1[9] = string91;
row2[1] = string12; row2[2] = string22; row2[3] = string32; row2[4] = string42; row2[5] = string52; row2[6] = string62; row2[7] = string72; row2[8] = string82; row2[9] = string92;
row3[1] = string13; row3[2] = string23; row3[3] = string33; row3[4] = string43; row3[5] = string53; row3[6] = string63; row3[7] = string73; row3[8] = string83; row3[9] = string93;
row4[1] = string14; row4[2] = string24; row4[3] = string34; row4[4] = string44; row4[5] = string54; row4[6] = string64; row4[7] = string74; row4[8] = string84; row4[9] = string94;
row5[1] = string15; row5[2] = string25; row5[3] = string35; row5[4] = string45; row5[5] = string55; row5[6] = string65; row5[7] = string75; row5[8] = string85; row5[9] = string95;
row6[1] = string16; row6[2] = string26; row6[3] = string36; row6[4] = string46; row6[5] = string56; row6[6] = string66; row6[7] = string76; row6[8] = string86; row6[9] = string96;
row7[1] = string17; row7[2] = string27; row7[3] = string37; row7[4] = string47; row7[5] = string57; row7[6] = string67; row7[7] = string77; row7[8] = string87; row7[9] = string97;
row8[1] = string18; row8[2] = string28; row8[3] = string38; row8[4] = string48; row8[5] = string58; row8[6] = string68; row8[7] = string78; row8[8] = string88; row8[9] = string98;
row9[1] = string19; row9[2] = string29; row9[3] = string39; row9[4] = string49; row9[5] = string59; row9[6] = string69; row9[7] = string79; row9[8] = string89; row9[9] = string99;
box1[1] = string11; box1[2] = string21; box1[3] = string31; box1[4] = string12; box1[5] = string22; box1[6] = string32; box1[7] = string13; box1[8] = string23; box1[9] = string33;
box2[1] = string12; box2[2] = string22; box2[3] = string32; box2[4] = string42; box2[5] = string52; box2[6] = string62; box2[7] = string72; box2[8] = string82; box2[9] = string92;
box3[1] = string13; box3[2] = string23; box3[3] = string33; box3[4] = string43; box3[5] = string53; box3[6] = string63; box3[7] = string73; box3[8] = string83; box3[9] = string93;
box4[1] = string14; box4[2] = string24; box4[3] = string34; box4[4] = string44; box4[5] = string54; box4[6] = string64; box4[7] = string74; box4[8] = string84; box4[9] = string94;
box5[1] = string15; box5[2] = string25; box5[3] = string35; box5[4] = string45; box5[5] = string55; box5[6] = string65; box5[7] = string75; box5[8] = string85; box5[9] = string95;
box6[1] = string16; box6[2] = string26; box6[3] = string36; box6[4] = string46; box6[5] = string56; box6[6] = string66; box6[7] = string76; box6[8] = string86; box6[9] = string96;
box7[1] = string17; box7[2] = string27; box7[3] = string37; box7[4] = string47; box7[5] = string57; box7[6] = string67; box7[7] = string77; box7[8] = string87; box7[9] = string97;
box8[1] = string18; box8[2] = string28; box8[3] = string38; box8[4] = string48; box8[5] = string58; box8[6] = string68; box8[7] = string78; box8[8] = string88; box8[9] = string98;
box9[1] = string19; box9[2] = string29; box9[3] = string39; box9[4] = string49; box9[5] = string59; box9[6] = string69; box9[7] = string79; box9[8] = string89; box9[9] = string99;
label11 = new JLabel("hello");
add(label11);
//label11.setMaximumSize(new Dimension(50, 50));
label11.setLocation(500,500);
label11.setBorder(new LineBorder(Color.BLACK));
}
public static void main(String arg[]) throws InterruptedException {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Soduku();
}
});
new Soduku();
//frame.add(panel);
System.out.println("Working");
}
}