Jpanel 自适应大小的问题

素直的纯白 2014-03-26 05:10:16
要做一个能够自适应窗口大小的界面,就是窗口拉大其中的各种组件就变大 窗口变小各种组件就跟着变小。
遇到问题的界面组成是:
JPanel
JScrollPane
Jpanel(A)
遇到的问题现象是: 水平拉伸窗口的时候 Jpanel(A) 中的内容会正常随着窗口变大而变大。但是水平收缩的的时候, Jpanel(A)中内容却不会随着变小。
...全文
1129 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
素直的纯白 2014-04-01
  • 打赏
  • 举报
回复
没搞定。。结贴 散分~~~
代码间的舞者 2014-03-26
  • 打赏
  • 举报
回复
我没试楼主给的代码。(代码不全,不好调试)。 不过,可以肯定的是,GridBagLayout 是可以自适应!
素直的纯白 2014-03-26
  • 打赏
  • 举报
回复
求大神啊 自己顶一下
素直的纯白 2014-03-26
  • 打赏
  • 举报
回复
引用 2 楼 secondfirstlife 的回复:
现在用swing很有前途么,我写过一个小例子,后来再以没碰了,因为他们说,swing现在用的人不是那么多
够呛有前途。 可是就有这么个活儿。 有没有前途也得整啊
secondfirstlife 2014-03-26
  • 打赏
  • 举报
回复
现在用swing很有前途么,我写过一个小例子,后来再以没碰了,因为他们说,swing现在用的人不是那么多
素直的纯白 2014-03-26
  • 打赏
  • 举报
回复
简化后的问题代码如下
private void initialize() {
		
		setBackground(new Color(240, 248, 255));
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{350, 0};
		gridBagLayout.rowHeights = new int[]{500, 0};
		gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		JScrollPane speOutputWindow = new JScrollPane();
		speOutputWindow.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		GridBagConstraints gbc_speOutputWindow = new GridBagConstraints();
		gbc_speOutputWindow.fill = GridBagConstraints.BOTH;
		gbc_speOutputWindow.gridx = 0;
		gbc_speOutputWindow.gridy = 0;
		add(speOutputWindow, gbc_speOutputWindow);
		speOutputWindow.setAlignmentY(Component.TOP_ALIGNMENT);
		speOutputWindow.setAlignmentX(Component.LEFT_ALIGNMENT);
		
		JPanel panel = new JPanel();
		panel.setBackground(new Color(240, 248, 255));
		speOutputWindow.setViewportView(panel);
		GridBagLayout gbl_plTotal = new GridBagLayout();
		gbl_plTotal.columnWidths = new int[] { 10, 200, 10};
		gbl_plTotal.rowHeights = new int[] { 0};
		gbl_plTotal.columnWeights = new double[] { 0.0, 1.0, 0.0 };
		gbl_plTotal.rowWeights = new double[] { 1.0};
		panel.setLayout(gbl_plTotal);

		
		ArrayList<String> message = new ArrayList<String>();
		
		message.add("test1");
		message.add("test2");
		message.add("test3");
		message.add("test4");
		message.add("test5");
		message.add("test6");
		message.add("test7");
		message.add("test8");
		message.add("test9");
		message.add("test10");
//		for (int i = 0; i < 5; i++) {
			int i = 0;
			OtherChatPanel plOtherBoard = new OtherChatPanel("other", message.get(2*i), "2014/01/10 9:14:30");
			GridBagConstraints gbc_plOtherBoard = new GridBagConstraints();
			//gbc_plOtherBoard.insets = new Insets(0, 0, 5, 5);
			gbc_plOtherBoard.fill = GridBagConstraints.HORIZONTAL;
			gbc_plOtherBoard.anchor = GridBagConstraints.LAST_LINE_START;
			gbc_plOtherBoard.gridx = 1;
			gbc_plOtherBoard.gridy = 2*i;
			//gbc_plOtherBoard.
			panel.add(plOtherBoard, gbc_plOtherBoard);
			

			MyChatPanel plMyBoard = new MyChatPanel("self", message.get(2*i+1),
					"2014/01/10 9:14:50");
			GridBagConstraints gbc_plMyBoard = new GridBagConstraints();
			//gbc_plMyBoard.insets = new Insets(0, 0, 5, 5);
			gbc_plMyBoard.fill = GridBagConstraints.HORIZONTAL;
			gbc_plMyBoard.anchor = GridBagConstraints.LAST_LINE_END;
			gbc_plMyBoard.gridx = 1;
			gbc_plMyBoard.gridy = 2*i + 1;
			panel.add(plMyBoard, gbc_plMyBoard);
		}

62,614

社区成员

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

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