Domanda

I am relatively new to Java. I have experience with the logic side of programming but not so much with GUI. I created a CrossWord generator that takes a text file with words and definitions and makes a crossword. Currently I have the solution stored in a 2d array of char(s). I need to figure out how to display the crossword and check the user input against the solution. I was wondering what the "easiest" way to display this using swing or some sort of graphic components. I was thinking of using a TilePane and textboxes but I am so completely new to GUI I have no idea how to code it. In your proposed solution please provide some example code of how to make whatever it is.

Thank you so much for your input.

È stato utile?

Soluzione

There are always many options when it comes to GUIs. And it is hardly feasible to ask for the best solution here. One the one hand, because there may be no "best" solution. On the other hand, in order to find out how "good" a solution is (or whether it is the "best" one), one has to be ridiculously precise concerning the specification of requirements and the evaluation metric.

However, in one of the most simple forms, the requirement the have a GUI component that can show a char[][] array (and allows obtaining the currently shown contents as a char[][] array) could be implemented like this...

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class CrosswordGUI
{
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI()
    {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        f.getContentPane().setLayout(new BorderLayout());

        JPanel container = new JPanel(new FlowLayout());
        final CrosswordPanel panel = new CrosswordPanel();
        container.add(panel);
        f.getContentPane().add(container, BorderLayout.CENTER);

        JButton generateButton = new JButton("Generate");
        generateButton.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                generate(panel);
            }
        });
        f.getContentPane().add(generateButton, BorderLayout.SOUTH);

        f.setSize(800, 800);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    private static Random random = new Random(0);
    private static void generate(CrosswordPanel panel)
    {
        int w = 4 + random.nextInt(8);
        int h = 4 + random.nextInt(8);
        char crossword[][] = new char[w][h];
        for (int y=0; y<h; y++)
        {
            for (int x=0; x<w; x++)
            {
                if (random.nextFloat() > 0.2)
                {
                    char c = (char)('A' + random.nextInt(26));
                    crossword[x][y] = c;
                }
            }
        }

        panel.setCrossword(crossword);
    }

}


class CrosswordPanel extends JPanel
{
    private JTextField textFields[][];

    void setCrossword(char array[][])
    {
        removeAll();
        int w = array.length;
        int h = array[0].length;
        setLayout(new GridLayout(w, h));
        textFields = new JTextField[w][h];

        for (int y=0; y<h; y++)
        {
            for (int x=0; x<w; x++)
            {
                char c = array[x][y];
                if (c != 0)
                {
                    textFields[x][y] = new JTextField(String.valueOf(c));
                    textFields[x][y].setFont(textFields[x][y].getFont().deriveFont(20.0f));
                    add(textFields[x][y]);
                }
                else
                {
                    add(new JLabel());
                }
            }
        }
        getParent().validate();
        repaint();
    }

    char[][] getCrossword()
    {
        int w = textFields.length;
        int h = textFields[0].length;
        char crossword[][] = new char[w][h];
        for (int y=0; y<h; y++)
        {
            for (int x=0; x<w; x++)
            {
                if (textFields[x][y] != null)
                {
                    String s = textFields[x][y].getText();
                    if (s.length() > 0)
                    {
                        crossword[x][y] = s.charAt(0);
                    }

                }
            }
        }
        return crossword;
    }

}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top