ฉันยังใหม่กับการเข้ารหัส ฉันกำลังสร้างเกมเพชฌฆาตง่ายๆ และติดอยู่ในส่วนนี้ เมื่อฉันเปิดเกม ฉันต้องการให้เมื่อพิมพ์ตัวอักษรลงใน JTextField และกด JButton แล้ว JLabel จะแสดงตัวอักษรนั้น ความช่วยเหลือใด ๆ จะดีมาก! ขอบคุณล่วงหน้า.

(ละเว้นความคิดเห็นในรหัส มันช่วยฉันได้)

   public class Main {
    static JLabel jl;
      static JTextField tf;
     String[] letters = new String[100];
     static final int SCREEN_WIDTH = 600;
     static final int SCREEN_HEIGHT = 600;
     static final int UNIT_SIZE = 100;
     static final int UNIT_SIZE2 = 19;
     static final int UNIT_SIZE3 = 40;
     static final int UNIT_SIZE4 = 20;
     static final int UNIT_SIZE5 = 60;
     // underline ^
     static final int UNIT_SIZE6 = (int) 7.5;
     // break between ___ fix ^
     static final int UNIT_SIZE7 = 0;
     // remove the image
     static final int GAME_UNITS = (SCREEN_WIDTH * SCREEN_HEIGHT) / UNIT_SIZE;
     Random random;
     int HeadX = 400;
     int HeadY = 100;
     int BodyX = 430;
     int BodyY = 180;
     int Arm1X = 400;
     int Arm1Y = 240;
     int Arm2X = 460;
     int Arm2Y = 240;
     int Leg1X = 430;
     int Leg1Y = 300;
     int Leg2X = 451;
     int Leg2Y = 300;
     int Letter1UnderlineX = 0 + UNIT_SIZE6;
     int Letter1UnderlineY = 535;
     int Letter2UnderlineX = 75 + UNIT_SIZE6;
     int Letter2UnderlineY = 535;
     int Letter3UnderlineX = 150 + UNIT_SIZE6;
     int Letter3UnderlineY = 535;
     int Letter4UnderlineX = 225 + UNIT_SIZE6;
     int Letter4UnderlineY = 535;
     int Letter5UnderlineX = 300 + UNIT_SIZE6;
     int Letter5UnderlineY = 535;
     int Letter6UnderlineX = 375 + UNIT_SIZE6;
     int Letter6UnderlineY = 535;
     int Letter7UnderlineX = 450 + UNIT_SIZE6;
     int Letter7UnderlineY = 535;
     int Letter8UnderlineX = 525 + UNIT_SIZE6;
     int Letter8UnderlineY = 535;
     String a = ("a");
     String b = ("b");
     String c = ("c");
     String d = ("d");
     String e = ("e");
     String f1 = ("f");
     String g1 = ("g");
     String h = ("h");
     String i = ("i");
     String j = ("j");
     String k = ("k");
     String l = ("l");
     String m = ("m");
     String n = ("n");
     String o = ("o");
     String p = ("p");
     String q = ("q");
     String r = ("r");
     String s = ("s");
     String t = ("t");
     String u = ("u");
     String v = ("v");
     String w = ("w");
     String x = ("x");
     String y = ("y");
     String z = ("z");
   boolean acheck;
   boolean bcheck;
   boolean ccheck;

   String alpha[] = {" ",a,b,c,d,e,f1,g1,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z};
   
    
    
    
      JFrame frame = new JFrame();
    public static void main(String[] args) {
        
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
              
                frame.add(new GamePanel());
                frame.pack();
                frame.setLocationRelativeTo(null);
             
                frame.setVisible(true);
              
            ;  
              
                
             
                
                
                JTextArea rules = new JTextArea(8, 40);
                rules.setWrapStyleWord(true);
                rules.setLineWrap(true);
                rules.append("Rules: Guess the word one letter at a time!\n When you have a letter in mind\n Click on the drop down above and click on that letter\n Then click on 'Guess' \n Good luck!");


                JOptionPane.showMessageDialog(frame, rules, "The Rules", JOptionPane.PLAIN_MESSAGE);

            }
        });
    }
  
   
    public class GamePanel extends JPanel implements ActionListener {

        private static final long serialVersionUID = -3919093956447306405L;
       
      
      
        public GamePanel() {
           
              Start();
           
            JButton b=new JButton("Guess!");  
            b.setBounds(50,100,95,30);  
            b.setLocation(0,0);
            b.addActionListener(this);
            
           
            jl = new JLabel();
            jl.setBounds(50, 50, 350, 40);
            jl.setLocation(100,100);
            jl.setVisible(true);
    
           
            
            tf = new JTextField();
            tf.setBounds(50, 50, 150, 20);
            tf.setLocation(100,0);
           
        
            frame.add(b); 
            frame.add(jl); 
            frame.add(tf);
            
          
               
            
     
        }
     
        
        public void actionPerformed1(ActionEvent e) {
             
            if (!tf.getText().equals("")) {
                    jl.setText(tf.getText());
                } else {
                    jl.setText("Please write something in the edit box");
                }
            
        }
         
          
             
       
        public void Start() {
            
            Scanner YesNoScanner = new Scanner(System.in);
            Scanner Letter = new Scanner(System.in);
            
            String ans = "";
            int count = 0;

            System.out.println("Would you like to play? (Yes - 1 | No - 2)");
            int YesNo = YesNoScanner.nextInt();
            if (YesNo == 1) {
                System.out.println("Rules: Word MUST be less than or equal to 8 characters \n \t No SPACES in the word or subject \n \t Don't show anyone the word if its your turn for the word ");
                System.out.println("You are about to start. \nDon't show anyone the word and subject your word is based off.");
                System.out.println("When writing the word only write ONE letter at a time!");
                System.out.println("Write 'done' when you are done writing your word. \n Do not use numbers");

                System.out.println("\n\nChoose a word between 1-8 characters long\n ( ONE LETTER AT A TIME [press enter after every letter])");

                do {
                    System.out.print("Letter " + (count + 1) + ": ");
                    ans = Letter.nextLine();
                    if (!ans.equalsIgnoreCase("done")) {
                        letters[count] = ans;
                        count++;
                        if (count == 8) {
                            System.out.println("Maximium letters reached");
                            ans = ("done");

                        }
                    }
                    String one = letters[0];
                } while (!ans.equalsIgnoreCase("done"));

                System.out.println("\nYour word is: ");
                for (int i = 0; i < count; i++) {
                    System.out.print(letters[i]);
                }

                System.out.println(" \nIs this the word you would like? (Yes - 1 | No - 2)");

            } else {
                System.out.println("You chose to not play. Maybe later :C");
                System.exit(0);
            }

            Scanner YesNoScanner2 = new Scanner(System.in);
            int YesNo2 = YesNoScanner2.nextInt();
            if (YesNo2 == 1) {
                 Scanner Subject = new Scanner(System.in);
                 

                 System.out.println("Please choose a subject! (1-10 characters long)");

                 ans = Subject.nextLine();

                 System.out.println(" \nIs this the Subject you would like? (Yes - 1 | No - 2)");
                 System.out.println(ans);

                 Scanner YesNoScanner3 = new Scanner(System.in);
                 int YesNo3 = YesNoScanner3.nextInt();
                 if (YesNo3 == 1) {
                     System.out.println("Are you ready to play? (Yes - 1 | No - 2)");
                     Scanner YesNoScanner4 = new Scanner(System.in);
                     int YesNo4 = YesNoScanner4.nextInt();
                     if (YesNo4 == 1) {
                         for (int i = 0; i < 200; i++) {
                             System.out.println("\n");
                         }

                     } else {
                         System.exit(0);
                     }

                 } else {
                     System.exit(0);
                 } 
                 
            } else {
                Start();
            }

           
            
            
            
            
            

        }
 
        public void checkGuess() {
        
         
            
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(600, 600);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            draw(g);
          
        }

        private void draw(Graphics g) {
            // TODO Auto-generated method stub
            for (int i = 0; i < SCREEN_HEIGHT / UNIT_SIZE; i++) {
                g.drawLine(i * UNIT_SIZE, 0, i * UNIT_SIZE, SCREEN_HEIGHT);
                g.drawLine(0, i * UNIT_SIZE, SCREEN_WIDTH, i * UNIT_SIZE);
            }

            int fix1 = 30;

       
           
            
            g.setColor(Color.black);
            g.setFont(new Font("BiauKai", Font.BOLD, 20));
            FontMetrics metrics = getFontMetrics(g.getFont());

            g.setColor(Color.black);
            g.fillOval(HeadX, HeadY, UNIT_SIZE, UNIT_SIZE);
            g.setColor(Color.black);
            g.fillRect(BodyX, BodyY, UNIT_SIZE3, UNIT_SIZE + UNIT_SIZE4);
            g.setColor(Color.black);
            g.fillRect(Arm1X, Arm1Y, UNIT_SIZE3, UNIT_SIZE2);
            g.setColor(Color.black);
            g.fillRect(Arm2X, Arm2Y, UNIT_SIZE3, UNIT_SIZE2);
            g.setColor(Color.black);
            g.fillRect(Leg1X, Leg1Y, UNIT_SIZE2, UNIT_SIZE);
            g.setColor(Color.black);
            g.fillRect(Leg2X, Leg2Y, UNIT_SIZE2, UNIT_SIZE);
            g.setColor(Color.black);
            g.fillRect(Letter1UnderlineX, Letter1UnderlineY, UNIT_SIZE5, UNIT_SIZE3);
            g.setColor(Color.black);
            g.fillRect(Letter2UnderlineX, Letter2UnderlineY, UNIT_SIZE5, UNIT_SIZE3);
            g.setColor(Color.black);
            g.fillRect(Letter3UnderlineX, Letter3UnderlineY, UNIT_SIZE5, UNIT_SIZE3);
            g.setColor(Color.black);
            g.fillRect(Letter4UnderlineX, Letter4UnderlineY, UNIT_SIZE5, UNIT_SIZE3);
            g.setColor(Color.black);
            g.fillRect(Letter5UnderlineX, Letter5UnderlineY, UNIT_SIZE5, UNIT_SIZE3);
            g.setColor(Color.black);
            g.fillRect(Letter6UnderlineX, Letter6UnderlineY, UNIT_SIZE5, UNIT_SIZE3);
            g.setColor(Color.black);
            g.fillRect(Letter7UnderlineX, Letter7UnderlineY, UNIT_SIZE5, UNIT_SIZE3);
            g.setColor(Color.black);
            g.fillRect(Letter8UnderlineX, Letter8UnderlineY, UNIT_SIZE5, UNIT_SIZE3);
            g.setFont(new Font("BiauKai", Font.BOLD, 20));
            g.drawString(letters[0], Letter1UnderlineX + 25, Letter1UnderlineY + 30);
        if(acheck == true) {
               g.setColor(Color.white);
               g.setFont(new Font("BiauKai", Font.BOLD, 20));
               g.drawString(letters[0], Letter1UnderlineX + 25, Letter1UnderlineY + 30);
        }
        }
        public class MyKeyAdapter extends KeyAdapter{

            
            
            
            
            public void keyPressed(KeyEvent e) {
                
                switch(e.getKeyCode()) {
                case KeyEvent.VK_A:
                System.out.println(a);
                    }
                
                }
            }
        @Override
        public void actionPerformed(ActionEvent e) {
             
         
            
        }
    

    }
}
ไม่พบคำตอบ