0

I am trying to place my JButton which has Image form to a desired place on my JFrame but when I define method setBounds for this particular button it is not working as expected and button is still on the same place. Code below:

public
    class GameFrame {

    JButton atB = new JButton();
    JButton beB = new JButton();
    JButton bgB = new JButton();
    JButton czB = new JButton();
    JButton deB = new JButton();
    JButton dkB = new JButton();
    JButton esB = new JButton();
    JButton plB = new JButton();
    JButton roB = new JButton();
    JButton ruB = new JButton();

    public GameFrame(){
        generateGameFrame();
    }

    public void generateGameFrame() {

        JPanel gamePanel;
        Countries countries = new Countries();
        JFrame gameFrame = new JFrame("Koronawirus AntiPlague - Game");
        try {
            gameFrame.setIconImage(ImageIO.read(GameFrame.class.getResourceAsStream("/resources/images/Icon.png")));
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        gamePanel = new JPanel() {
            public void paintComponent(Graphics g) {
                Image img = Toolkit.getDefaultToolkit().getImage(
                        MenuFrame.class.getResource("/resources/images/map.png"));
                g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
            }
        };
        gameFrame.setContentPane(gamePanel);

        gameFrame.setSize(700, 700);
        gameFrame.setVisible(true);
        gameFrame.setResizable(false);
        gameFrame.setLocationRelativeTo(null);
        gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        loadImageAsButtonAndDesign();
        gamePanel.add(atB);


    }

    public void loadImageAsButtonAndDesign()
    {
        try {
            Image img = ImageIO.read(getClass().getResource("/resources/images/at.png"));
            atB.setIcon(new ImageIcon(img));
        } catch (Exception ex) {
            System.out.println(ex);
        }
        atB.setBorderPainted(false);
        atB.setOpaque(false);
        atB.setBackground(Color.DARK_GRAY.darker());
        atB.setBounds(100,100,20,15);
    }
}

As you can see above I am trying setBounds but unsuccessfully. I was trying also:

atB.setLayout(null);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
matri1395
  • 1
  • 2
  • 1
    1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). E.G. The code in [this answer](https://stackoverflow.com/a/10862262/418556) hot links to an image embedded in [this question](https://stackoverflow.com/q/10861852/418556). 3) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to .. – Andrew Thompson May 20 '21 at 09:51
  • 1
    .. pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson May 20 '21 at 09:51
  • 2
    Also, don't do I/O in the paintComponent() method. A painting method should be efficient. The image should be read by the constructor of your class. – camickr May 20 '21 at 14:05

0 Answers0