0

I've been getting the hang of GUI's in java pretty well, but I had not realized that setting a background would be so difficult/complicated. Any ideas on how to set an image as the background to my existing GUI? The image would be exactly the same height/width of my jframe.

 public class cipher extends JFrame implements ActionListener {

        private int wHEIGHT = 600;
        private int wWIDTH  = 900;

        private JTextField userString;
        private String userWords = "";

        private JButton encryptButton;
        private JButton importPic;
        private JButton importText;

        public cipher(){

            // JFrame management 
            setSize(wWIDTH, wHEIGHT);
            setTitle("Camo Cypher");
            setLocationRelativeTo(null);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setResizable(false);    

            // GridBag management 
            GridBagConstraints c = new GridBagConstraints();
            GridBagLayout myLayout = new GridBagLayout();
            setLayout(myLayout);

            // User input bar
            userString = new JTextField(50);
            userString.setPreferredSize(new Dimension(700,27));
            userString.setVisible(true);
            c.weightx = 0.5;
            c.gridx = 1;
            c.gridy = 1;
            add(userString,c );

            // Blank space
            JButton blank = new JButton("");
            blank.setPreferredSize(new Dimension(0, 10));
            c.weightx = 0.5;
            c.gridx = 1;
            c.gridy = 2;
            add(blank, c);      

            // Encypt button
            encryptButton = new JButton("Encrypt");
            encryptButton.addActionListener(this);
            encryptButton.setPreferredSize(new Dimension(150, 35));
            encryptButton.setForeground(Color.BLUE);
            c.weightx = 0.5;
            c.gridx = 1;
            c.gridy = 3;
            add(encryptButton, c);

            // Import buttons panel
            Panel buttonPanel = new Panel();
            importText = new JButton("Import Text File");
            importText.addActionListener(this);
            importPic = new JButton("Import Image");
            importPic.addActionListener(this);
            importText.setPreferredSize(new Dimension(200, 60));
            importPic.setPreferredSize(new Dimension(200, 60));     
            buttonPanel.add(importText);
            buttonPanel.add(importPic);     
            c.weightx = 100;
            c.gridx = 1;
            c.gridy = 4;
            add(buttonPanel, c);    

        }   
user1765804
  • 161
  • 2
  • 3
  • 10

0 Answers0