swing问题~

u012562203 2013-11-30 12:18:31

public class MainFrame extends JFrame {
private JButton button;
private JLabel title;
private JTable table;

public MainFrame (){
super("账号密码管理系统");
init();
setSize(370,370);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// setResizable(false);
setVisible(true);
}
public void init(){
Object rowData[][] = new Object[5][5];
String columnName[] = {"账号密码归属","账号","密码","预留验证信息","生成密码"};
button = new JButton("添加");
button.setBounds(150,300,60,25);
add(button);
title = new JLabel("账号密码管理系统",JLabel.CENTER);
title.setFont(new Font("宋体",Font.BOLD,25));
setLayout(new BorderLayout());
add(title,BorderLayout.NORTH);
table = new JTable(rowData,columnName);
JScrollPane scrollpane = new JScrollPane(table);
scrollpane.setBorder(new EmptyBorder(50,0,0,0));
add(scrollpane,BorderLayout.CENTER);
};
public static void main(String arg[]){
MainFrame frame = new MainFrame();
}
}
我用鼠标改变窗口大小时,为什么button的位置不跟着改变?
...全文
172 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
u012562203 2013-11-30
  • 打赏
  • 举报
回复
引用 1 楼 terry21 的回复:
button.setBounds(150,300,60,25);
因为你使用了固定位置的方式。
一般使用setBounds添加组件,父组件的layout都是null的,这样方便自由定制界面。

用setBounds添加组件怎么确定位置的,可以举个例说明下吗?
我想做成这样子,窗口初始大小是370,370,用setBounds怎么实现呀
terry21 2013-11-30
  • 打赏
  • 举报
回复
button.setBounds(150,300,60,25); 因为你使用了固定位置的方式。 一般使用setBounds添加组件,父组件的layout都是null的,这样方便自由定制界面。
u012562203 2013-11-30
  • 打赏
  • 举报
回复
引用 5 楼 terry21 的回复:

	public void init() {
		Object rowData[][] = new Object[5][5];
		String columnName[] = { "账号密码归属", "账号", "密码", "预留验证信息", "生成密码" };
		button = new JButton("添加");
		button.setBounds(150, 300, 60, 25);
		add(button);
		title = new JLabel("账号密码管理系统", JLabel.CENTER);
		title.setFont(new Font("宋体", Font.BOLD, 25));
		setLayout(new BorderLayout());
		add(title, BorderLayout.NORTH);
		table = new JTable(rowData, columnName);
		JScrollPane scrollpane = new JScrollPane(table);
		scrollpane.setBorder(new EmptyBorder(50, 0, 0, 0));
		add(scrollpane, BorderLayout.CENTER);
		JPanel panelSouth = new JPanel();
		panelSouth.setLayout(new FlowLayout());
		add(panelSouth, BorderLayout.SOUTH);
		panelSouth.add(button);
	};
可以了,谢谢,为什么加个JPanel就可以了?
terry21 2013-11-30
  • 打赏
  • 举报
回复

	public void init() {
		Object rowData[][] = new Object[5][5];
		String columnName[] = { "账号密码归属", "账号", "密码", "预留验证信息", "生成密码" };
		button = new JButton("添加");
		button.setBounds(150, 300, 60, 25);
		add(button);
		title = new JLabel("账号密码管理系统", JLabel.CENTER);
		title.setFont(new Font("宋体", Font.BOLD, 25));
		setLayout(new BorderLayout());
		add(title, BorderLayout.NORTH);
		table = new JTable(rowData, columnName);
		JScrollPane scrollpane = new JScrollPane(table);
		scrollpane.setBorder(new EmptyBorder(50, 0, 0, 0));
		add(scrollpane, BorderLayout.CENTER);
		JPanel panelSouth = new JPanel();
		panelSouth.setLayout(new FlowLayout());
		add(panelSouth, BorderLayout.SOUTH);
		panelSouth.add(button);
	};
u012562203 2013-11-30
  • 打赏
  • 举报
回复
引用 3 楼 terry21 的回复:
[quote=引用 2 楼 u012562203 的回复:] 用setBounds添加组件怎么确定位置的,可以举个例说明下吗? 我想做成这样子,窗口初始大小是370,370,用setBounds怎么实现呀

public class MainFrame extends JFrame {
	private JButton button;

	private JLabel title;

	private JTable table;

	public MainFrame() {
		super("账号密码管理系统");
		init();
		setSize(370, 370);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		// setResizable(false);
		setVisible(true);
	}

	public void init() {
		Object rowData[][] = new Object[5][5];
		String columnName[] = { "账号密码归属", "账号", "密码", "预留验证信息", "生成密码" };
		button = new JButton("添加");
		button.setBounds(150, 300, 60, 25);
		add(button);
		title = new JLabel("账号密码管理系统", JLabel.CENTER);
		title.setFont(new Font("宋体", Font.BOLD, 25));
		title.setBounds(50, 5, 250, 25);
		setLayout(null);
		add(title);
		table = new JTable(rowData, columnName);
		JScrollPane scrollpane = new JScrollPane(table);
		scrollpane.setBorder(new EmptyBorder(50, 0, 0, 0));
		add(scrollpane);
		scrollpane.setBounds(0, 30, 370, 250);
	};

	public static void main(String arg[]) {
		MainFrame frame = new MainFrame();
	}
}
一般使用swing做软件都会自己定制位置的,最大化的实现再另外写一套组件位置移位的代码。[/quote] 我用鼠标拖动改变窗口大小,button的位置还是一样不会跟着改变呀~~
terry21 2013-11-30
  • 打赏
  • 举报
回复
引用 2 楼 u012562203 的回复:
用setBounds添加组件怎么确定位置的,可以举个例说明下吗? 我想做成这样子,窗口初始大小是370,370,用setBounds怎么实现呀

public class MainFrame extends JFrame {
	private JButton button;

	private JLabel title;

	private JTable table;

	public MainFrame() {
		super("账号密码管理系统");
		init();
		setSize(370, 370);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		// setResizable(false);
		setVisible(true);
	}

	public void init() {
		Object rowData[][] = new Object[5][5];
		String columnName[] = { "账号密码归属", "账号", "密码", "预留验证信息", "生成密码" };
		button = new JButton("添加");
		button.setBounds(150, 300, 60, 25);
		add(button);
		title = new JLabel("账号密码管理系统", JLabel.CENTER);
		title.setFont(new Font("宋体", Font.BOLD, 25));
		title.setBounds(50, 5, 250, 25);
		setLayout(null);
		add(title);
		table = new JTable(rowData, columnName);
		JScrollPane scrollpane = new JScrollPane(table);
		scrollpane.setBorder(new EmptyBorder(50, 0, 0, 0));
		add(scrollpane);
		scrollpane.setBounds(0, 30, 370, 250);
	};

	public static void main(String arg[]) {
		MainFrame frame = new MainFrame();
	}
}
一般使用swing做软件都会自己定制位置的,最大化的实现再另外写一套组件位置移位的代码。

58,454

社区成员

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

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