请问一个问题:我建了一个JFrame,然后再里面添加了一个JPanel,最后我向JPanel里面添加JButton,但是JButton不能显示了请问这个是什么原因呢?
/**
 * 
 */
package src.com.fornew.tvm.ui.maintainui.manage;import java.awt.BorderLayout;
// import java.awt.Button;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.LayoutManager;
import java.awt.Toolkit;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRootPane;import src.com.fornew.tvm.ui.maintainui.manage.MainPanel;
import src.com.fornew.tvm.ui.maintainui.manage.MakeButton;/**
 * @author Administrator
 * 
 */public class MainFrame extends JFrame {
/**
 * 
 */
private static final long serialVersionUID = 1L;public static Dimension ScreenSize = Toolkit.getDefaultToolkit()
.getScreenSize();public MainFrame(LayoutManager layout, String title, Dimension dim,
Color backgroud) {
setTitle(title);
setLayout(layout);
setSize(dim);
setBackground(backgroud);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setUndecorated(true); // 去掉窗口的装饰
getRootPane().setWindowDecorationStyle(JRootPane.NONE);
}protected void MakeButton(String name, int x, int y, int width, int height,
Container obj) {
JButton button = new JButton(name);
button.setText(name);
button.setVisible(true);
button.setBounds(x, y, width, height);
obj.add(button);
}protected void MakeButton(String name, Container obj) {
JButton button = new JButton(name);
button.setText(name);
obj.add(button);
}public static void main(String args[]) {
JLabel PriceSelect = new JLabel("选择票价:");
JLabel LineSelect = new JLabel("选择线路:");MainFrame UI = new MainFrame(null, null, ScreenSize, new Color(0, 0, 0));
MainPanel map = new MainPanel(Color.pink, null, 0,
MainFrame.ScreenSize.height / 8,
3 * MainFrame.ScreenSize.width / 4,
3 * MainFrame.ScreenSize.height / 4);
MainPanel Bulletin = new MainPanel(new Color(0, 255, 255), null, 0, 0,
3 * MainFrame.ScreenSize.width / 4,
MainFrame.ScreenSize.height / 8);
MainPanel control = new MainPanel(Color.gray, null,
3 * MainFrame.ScreenSize.width / 4, 0,
MainFrame.ScreenSize.width / 4, MainFrame.ScreenSize.height);
MainPanel line = new MainPanel(Color.green, new FlowLayout(), 0,
7 * MainFrame.ScreenSize.height / 8,
3 * MainFrame.ScreenSize.width / 4,
MainFrame.ScreenSize.height / 8);
// PriceSelect.setBounds(3 * MainFrame.ScreenSize.width / 4+10, 10,
// control.getSize().width, control.getSize().height);
// control.add(PriceSelect);
UI.MakeButton("English", 3 * MainFrame.ScreenSize.width / 4 + 10, 0,
control.getSize().width, MainFrame.ScreenSize.height / 5,
control);
    
line.add(LineSelect);
UI.MakeButton("1号线", line);
UI.MakeButton("2号线", line);
UI.MakeButton("3号线", line);
UI.MakeButton("4号线", line);
UI.add(map);
UI.add(line);
UI.add(Bulletin);
UI.add(control);
UI.setVisible(true);
}
}这个是我的代码 
在JPanel(Control)里面加了一个Button,但是这个button始终不能显示
JPanel没有使用布局管理器
但是当我把JPanel的布局改成FlowLayout或者Borderlayout之后就可以显示了
请问这个是什么原因呢?谢谢

解决方案 »

  1.   

    你既然设置的setLayout(null),那么对应的JButton必须设置.setBounds(new Rectangle(左边距,上边距,宽度,高度));
      

  2.   

    protected void MakeButton(String name, int x, int y, int width, int height,
    Container obj) {
    JButton button = new JButton(name);
    button.setText(name);
    button.setVisible(true);
    button.setBounds(x, y, width, height);
    obj.add(button);
    }我这地个地方是设置了的啊
      

  3.   

    UI.MakeButton("English", 3 * MainFrame.ScreenSize.width / 4 + 10, 0,
    control.getSize().width, MainFrame.ScreenSize.height / 5,
    control);
    这个地方设置了的
      

  4.   

    protected void MakeButton(String name, int x, int y, int width, int height,
    Container obj) {
    JButton button = new JButton(name);
    button.setText(name);
    button.setVisible(true);
    button.setBounds(x, y, width, height);
    obj.add(button);
    }
    这个方法设置了的啊
      

  5.   

    好乱得代码,加些注释吧,button的位置是不是超出边框了
      

  6.   

    下个Swing插件 用swing拖把。