简体   繁体   中英

Why isn't my JButton appearing on my JFrame?

I am not sure why my JButton is not appearing on my JFrame. Here is the code:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;

public class tttMain extends JPanel implements ActionListener {

    private static final long serialVersionUID = 2294426281847625986L;

    private static JFrame frame;
    private static JPanel panel;
    private static JButton b1, b2, b3, b4, b5, b6, b7, b8, b9;
    private static ArrayList<JButton> buttonList;

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.black);
        //vertical lines
        g.drawLine(150, 65, 150, 340);
        g.drawLine(260, 65, 260, 340);
        //horizontal lines
        g.drawLine(65, 150, 340, 150);
        g.drawLine(65, 260, 340, 260);
    }

    public static void main(String[] args) {

        frame = new JFrame();
        panel = new JPanel();
        frame.setSize(450, 450);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setContentPane(new tttMain());
        frame.add(panel);

        panel.setLayout(null);

        b1 = new JButton("Hi Stack Overflow!");

        b1.setBounds(85, 85, 65, 65);

        panel.add(b1);


        frame.setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {

    }
}

Can somebody help me out? I have used JButtons before with no problem, so I don't know why this doesn't work. this is my first time using the Graphics class so maybe that could be apart of the problem.

So the "symptom" of your problem is caused by panel.setLayout(null); , but the root of your problem is a mis-understanding into how component layout works.

Layout managers do a lot of important work and you should be taking the time to learn and understand them.

JPanel , by default, uses a FlowLayout . tttMain (which, by the way, violates the community naming conventions;)) extends from JPanel , since you never change the layout manager, it's using a FlowLayout .

You then add a JPanel to it, which has had it's layout set to null . This means that it is no longer providing sizing hints to it's parent - so it will be laid out with a size of 0x0 , which leads us to your underlying problem.

The short story - don't use null layouts. The long story, you may consider using a different approach, maybe taking advantage of a BorderLayout and adding both the tttMain () and the JPanel with the button to it, as an idea

I'd strongly recommend spending some time going through and understanding Laying Out Components Within a Container

It is happening because you have set the layout of the panel to null . Remove the following line and it will appear:

panel.setLayout(null);

I think there is no problem using null layout's however I always specify the minimumSize and maximumSize. Using absolute layout also, doesn't re-size well. and can cause some problem's down the road. I also use WindowBuilder in eclipse because, you should probably not be doing it by hand.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM