Search code examples
javaswinginputjtextfield

Getting text input without swing in Java


Hi I'm making a game in Java that randomly generates 100 numbers, and then ask the user to memorize as many as then can and then try to recall as many as they can. My game uses a JPanel and a Graphics g object to do all the drawing. How do I "draw" a JTextfield or get one to work on a jpanel?


Solution

  • Add a ActionListener to JTextField and then add that JTextField to JPanel. Now add this JPanel to JFrame using this.add(jpnel, BorderLayout.SOUTH); Create a new JPanel class Board where you draw things. Add that JPanel to JFrame as, this.add(new Board(), BorderLayout.CENTER);. Here I coded one example for you. Now you should have an idea how to do that...

    Board class

        public class Board extends JPanel {
    
        int[] numbers = {3, 25, 5, 6, 60, 100};
        int index = 0;
        static String num;
        boolean once = true;
        FontMetrics fm;
    
        Board() {
            setPreferredSize(new Dimension(400, 200));
            setBackground(Color.decode("#ffde00"));
        }
    
        @Override
        public void paint(Graphics g) {
            super.paint(g);
            Graphics2D g2 = (Graphics2D) g;
            if (index < 6) {
                num = numbers[index] + "";
            } else {
                num = "Game Ended.";
                Window.ans.setEditable(false);
            }
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setFont(new Font("Arial", Font.PLAIN, 50));
            if(once){
                fm = g2.getFontMetrics();
                once = false;
            }
            int x = ((getWidth() - fm.stringWidth(num)) / 2);
            int y = ((getHeight() - fm.getHeight()) / 2) + fm.getAscent();
            g2.drawString(num + "", x, y);
            index++;
        }
    
    }
    

    Window class

        public class Window extends JFrame {
    
        JPanel p = new JPanel();
        JLabel lbl = new JLabel("Enter the number if you have seen it before, Else empty.");
        JLabel res = new JLabel("....");
        static JTextField ans = new JTextField(10);
        Board board = new Board();
    
        public Window() {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            add(board, BorderLayout.CENTER);
            p.setLayout(new BorderLayout(8, 8));
            p.add(lbl, BorderLayout.WEST);
            ans.addActionListener(new ActionListener() {
    
                public void actionPerformed(ActionEvent e) {
                    if (ans.getText().equals(Board.num)) {
                        res.setText("Good");
                    } else {
                        res.setText("Bad");
                    }
                    ans.setText("");
                    board.repaint();
                }
    
            });
            p.add(ans, BorderLayout.CENTER);
            p.add(res, BorderLayout.EAST);
            p.setBorder(new EmptyBorder(10, 10, 10, 10));
            this.add(p, BorderLayout.SOUTH);
            setResizable(false);
            pack();
            setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new Window();
                }
            });
        }
    
    }