开发者

why does my button not appear on my JPanel - Java

Or maybe my JPanel is not appearing at all.

I am trying to have a JPanel at the bottom of the screen that hold several buttons. Can someone set me strait?

public class MyAWTMenu extends java.awt.Frame// implements ActionListener 
{
public void init() { 
setBackground( Color.white );
JPanel开发者_运维问答 bottom = new JPanel();
    bottom.setBackground(Color.BLACK);
    JButton b1 = new JButton("test");
 b1.setVisible(true);
    bottom.add(b1);
    bottom.setVisible(true);
    add(bottom,BorderLayout.CENTER);
}
public static void main( String args [] ) {

    MyAWTMenu objAppFrame = new MyAWTMenu();

    objAppFrame.addWindowListener(      //Register an anonymous class as a listener.
         new WindowAdapter() {
            public void windowClosing( WindowEvent e ) 
            {  
               System.exit( 0 );
            }
         }
    );
    objAppFrame.init();
    objAppFrame.setSize( 760, 378);
    objAppFrame.setVisible( true );                    

  }


I'd better rewrite it as follows:

public class FooFrame extends JFrame {

    public FooFrame() {

        // your code, copy/pasted
        setBackground(Color.white);
        JPanel bottom = new JPanel();
        bottom.setBackground(Color.BLACK);
        JButton b1 = new JButton("test");
        bottom.add(b1);
        add(bottom, BorderLayout.CENTER);

        // set size & pack
        Dimension size = new Dimension(400, 400);
        setPreferredSize(size);
        setMinimumSize(size);
        pack();
        setLocationRelativeTo(null);
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FooFrame().setVisible(true);
            }
        });
    }
}


add(bottom,BorderLayout.SOUTH); in your init()

Here's your code that i was running. It seems to work fine for me. I did add a call to pack();


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JPanel;

public class MyAWTMenu extends java.awt.Frame// implements ActionListener
{

    public void init() {
        setBackground(Color.white);
        JPanel bottom = new JPanel();
        bottom.setBackground(Color.BLACK);
        JButton b1 = new JButton("test");
        bottom.add(b1);
        bottom.setVisible(true);
        add(bottom, BorderLayout.SOUTH);
        pack();
    }

    public static void main(String args[]) {

        MyAWTMenu objAppFrame = new MyAWTMenu();

        objAppFrame.addWindowListener( //Register an anonymous class as a listener.
                new WindowAdapter() {

            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        objAppFrame.init();
        objAppFrame.setSize(760, 378);
        objAppFrame.setVisible(true);

    }
}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜