dddkk
dddkk

Reputation: 85

Invisible JButton in JPanel

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);
    }
}

Upvotes: 1

Views: 1679

Answers (2)

matt
matt

Reputation: 2429

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

        }
    }

}

Upvotes: 5

camickr
camickr

Reputation: 324098

  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.

Upvotes: 5

Related Questions