当前位置: 代码迷 >> Java相关 >> 为什么JFrame只能放入两个JPanel,该如何处理
  详细解决方案

为什么JFrame只能放入两个JPanel,该如何处理

热度:4216   发布时间:2013-02-25 21:49:28.0
为什么JFrame只能放入两个JPanel
代码如下,不知道为什么,只能显示第一个jpanel和最后一个jpanel,这是为什么,高手帮帮忙

package swing;

import java.awt.Color;

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

public class test2 extends JFrame{

/**

*/
private static final long serialVersionUID = 1L;
private JFrame frame;

public test2() {
// TODO Auto-generated constructor stub
frame = new JFrame();
//panel=(JPanel) frame.getContentPane();
//JPanel panel=new JPanel();
//panel.setLayout(new FlowLayout());
//panel.setOpaque(false);

JPanel jPanel=new JPanel();
JPanel jPanel2=new JPanel();
JPanel jPanel3=new JPanel();
JPanel jPanel4=new JPanel();

jPanel.setLayout(null);
jPanel2.setLayout(null);
jPanel3.setLayout(null);
jPanel4.setLayout(null);

jPanel.setSize(500, 200);
jPanel2.setSize(500, 200);
jPanel3.setSize(500, 200);
jPanel4.setSize(500, 100);

//jPanel.setBounds(100, 0, 100, 20);
JButton jButton=new JButton("第一个jpanel");
JButton jButton2=new JButton("第二个jpanel");
JButton jButton3=new JButton("第三个jpanel");
JButton jButton4=new JButton("第四个jpanel");

jButton.setBounds(0, 0, 200, 20);
jButton2.setBounds(0, 200, 200, 20);
jButton3.setBounds(0, 400, 200, 20);
jButton4.setBounds(0, 600, 200, 20);

jPanel.add(jButton);
jPanel2.add(jButton2);
jPanel3.add(jButton3);
jPanel4.add(jButton4);

jPanel.setBackground(Color.blue);
jPanel2.setBackground(Color.white);
jPanel3.setBackground(Color.yellow);
jPanel4.setBackground(Color.gray);

frame.add(jPanel);
frame.add(jPanel2);
frame.add(jPanel3);
frame.add(jPanel4);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 700);
frame.setVisible(true);


}

public static void main(String[] args) {
new test2();
}

}


------解决方案--------------------------------------------------------
Java code
package novemberTestDay;import java.awt.BorderLayout;import java.awt.EventQueue;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;public class Test extends JFrame {    /**     * Launch the application     * @param args     */    public static void main(String args[]) {        EventQueue.invokeLater(new Runnable() {            public void run() {                try {                    Test frame = new Test();                    frame.setVisible(true);                } catch (Exception e) {                    e.printStackTrace();                }            }        });    }    /**     * Create the frame     */    public Test() {        super();        setBounds(100, 100, 500, 375);        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        final JPanel panel = new JPanel();        getContentPane().add(panel, BorderLayout.NORTH);        final JPanel panel_1 = new JPanel();        panel.add(panel_1);        final JButton button = new JButton();        button.setText("New JButton");        panel_1.add(button);        final JPanel panel_2 = new JPanel();        panel.add(panel_2);        final JButton button_1 = new JButton();        button_1.setText("New JButton");        panel_2.add(button_1);        final JPanel panel_3 = new JPanel();        panel.add(panel_3);        final JButton button_2 = new JButton();        button_2.setText("New JButton");        panel_3.add(button_2);        final JPanel panel_4 = new JPanel();        panel.add(panel_4);        final JButton button_3 = new JButton();        button_3.setText("New JButton");        panel_4.add(button_3);        //    }}