Jpanel 自适应大小的问题 SWING组件自适应大小 界面 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 简化后的问题代码如下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); } 现在用swing很有前途么,我写过一个小例子,后来再以没碰了,因为他们说,swing现在用的人不是那么多 我没试楼主给的代码。(代码不全,不好调试)。不过,可以肯定的是,GridBagLayout 是可以自适应! Java基础知识 import语句问题 如何设置TextField回车键事件为单击确认按钮 逻辑有点乱了!!!!! 用户界面线程中光标位置总是重置问题 tomcat启动时出错高手快来看看 在linux下怎么用System.loadLibrary? 多线程对静态变量进行++和--操作,需要同步吗? 我要得到一个当前日期的java.sql.date的值,如何最才最简单的方法 寻求简单的计时器的使用 请教:java中包是如何被的创建和引用的? 算法题:找出一个数组中前K个大数的位置 求大神给个思路,真心没有思路啊
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);
}
我没试楼主给的代码。(代码不全,不好调试)。不过,可以肯定的是,GridBagLayout 是可以自适应!