1

Here's my JPanel. The first button is always visible, but the secound is visible only when you place a cursour on it. Where the problem can be?

P.S. Please use simple english, if u can, because I don't speak english well

public class GamePanel extends JPanel implements KeyListener{


GamePanel(){
    setLayout(null);
}

public void paint(Graphics g){

    JButton buttonShip1 = new JButton();
    buttonShip1.setLocation(10, 45);
    buttonShip1.setSize(40, 40);
    buttonShip1.setVisible(true);
    add(buttonShip1);

    JButton buttonShip2 = new JButton();
    buttonShip2.setLocation(110, 145);
    buttonShip2.setSize(440, 440);
    buttonShip2.setVisible(true);
    add(buttonShip2);
    }
}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
dddkk
  • 85
  • 1
  • 9
  • 1
    There are a lot of issues with the code. For starters, why are you adding components in the `paint(Graphics)` method? You should also avoid using `setSize`, `setLocation` and `setLayout(null)`. – Obicere Apr 11 '14 at 20:30
  • What the size of button `buttonShip2.setSize(440, 440);`? – Braj Apr 11 '14 at 20:30

2 Answers2

5
  1. Don't use a null layout
  2. Don't create components in a painting method. The paint() method is for custom painting only. There is no need for you to override the paint() method.

Read the section from the Swing tutorial How to Use FlowLayout for a simple example that uses buttons and a layout manager.

camickr
  • 321,443
  • 19
  • 166
  • 288
5

If you want to avoid problems and learn Java Swing correctly, go check out their tutorials here.

There are too many problems to discuss here, so I'll try to keep it simple.

  1. You're using a null layout. null layouts are avoided for the most part because there is usually a layout that does exactly what you want. It takes some time to get it working, but there are some defaults in this tutorial that are fairly simple to use. There are some nice pictures there that show you what you can do with each layout, too. If you use a layout manager, you generally don't need to use setLocation, setSize or setVisible on most components like JButtons.

  2. You're calling the paint method in a Swing application. You want to call paintComponent because you're using Swing and not Awt. You also need to call the super.paintComponent(g) method on the first line of the paintComponent method in order to correctly override the other paintComponent method.

  3. The paint/paintComponent related methods are called very often. You don't want to create/initialize objects in them. The paint/paintComponent methods aren't a one time method like they may sound. They're continuously called and you should design your GUI around this. Design your paint-related methods to be event-driven rather than sequential. In other words, program the paintComponent method with the mindset that your GUI is reacting to things continuously rather than running in sequential order like a normal program. That's a very basic approach to it and hopefully doesn't confuse you, but if you go check out that tutorial you'll see what I mean eventually.

There are two basic types of GUIs in Java. One is Swing and the other is Awt. Check out this answer on stackoverflow for a nice description of the two.

Here is an example of what two buttons on a JPanel look like.

public class Test 
{
    public static void main(String[] args) 
    {
        JFrame jframe = new JFrame();
        GamePanel gp = new GamePanel();

        jframe.setContentPane(gp);
        jframe.setVisible(true);
        jframe.setSize(500,500);
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }


    public static class GamePanel extends JPanel{

    GamePanel() {
        JButton buttonShip1 = new JButton("Button number 1");
        JButton buttonShip2 = new JButton("Button number 2");
        add(buttonShip1);
        add(buttonShip2);

        //setLayout(null);
        //if you don't use a layout manager and don't set it to null
        //it will automatically set it to a FlowLayout.
    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);

        // ... add stuff here later
            // when you've read more about Swing and
            // painting in Swing applications

        }
    }

}
Community
  • 1
  • 1
Mdev
  • 2,440
  • 2
  • 17
  • 25
  • I didn's say about that, but those buttons have to move free if some keys are pressed, so can I use null layout? – dddkk Apr 12 '14 at 09:17
  • 1
    I just gave you an example of some working code for a GUI to get you started. You can add the functionality on your own but I highly suggest you go through the tutorials I mentioned in the post. – Mdev Apr 12 '14 at 09:18
  • 1
    Anyway, thank you for such a full answer, your answwer was helpful – dddkk Apr 12 '14 at 10:54
  • If you want to move the components around, you should re-add them with their locations altered, but remember to do it in the context of the layout you choose. Once you get a grasp on how Swing works in Java, you can start using more advanced techniques like in camickr's (who is actually the other commenter!) ComponentMover tutorial: http://tips4java.wordpress.com/2009/06/14/moving-windows/ – Mdev Apr 12 '14 at 20:14