JPanel添加了JButton之后不能显示

xueyoo 2012-09-12 11:00:53
请问一个问题:我建了一个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之后就可以显示了
请问这个是什么原因呢?

谢谢
...全文
888 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
xueyoo 2012-09-12
  • 打赏
  • 举报
回复
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);
}

我这地个地方是设置了的啊
捏造的信仰 2012-09-12
  • 打赏
  • 举报
回复
楼主要学会正确的贴代码方式
shenhua 2012-09-12
  • 打赏
  • 举报
回复
你既然设置的setLayout(null),那么对应的JButton必须设置.setBounds(new Rectangle(左边距,上边距,宽度,高度));
MiceRice 2012-09-12
  • 打赏
  • 举报
回复
没有设置布局,就是 绝对定位布局。

那么里面的控件需要设置 位置大小,才能正确显示的。

不过一般不推荐用绝对定位布局模式。
z744489075 2012-09-12
  • 打赏
  • 举报
回复
下个Swing插件 用swing拖把。。。。
shan4260 2012-09-12
  • 打赏
  • 举报
回复
好乱得代码,加些注释吧,button的位置是不是超出边框了
pinkpqs 2012-09-12
  • 打赏
  • 举报
回复
要jpanel setLayout(null);
新铺村长 2012-09-12
  • 打赏
  • 举报
回复
自己顶一下,求解!
新铺村长 2012-09-12
  • 打赏
  • 举报
回复
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);
}


这个方法设置了的啊
新铺村长 2012-09-12
  • 打赏
  • 举报
回复
UI.MakeButton("English", 3 * MainFrame.ScreenSize.width / 4 + 10, 0,
control.getSize().width, MainFrame.ScreenSize.height / 5,
control);


这个地方设置了的

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧