近日想了解GroupLayout布局模式,所以做了以下代码:
import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JScrollPane;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import java.awt.FlowLayout;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.GroupLayout;public class frame2 extends JFrame { private static final long serialVersionUID = 1L; private JPanel jContentPane = null; private JScrollPane jScrollPane = null; private JPanel jPanel = null; private JLabel jLabel_1 = null; private JTextArea jTextArea_1 = null; private JLabel jLabel_2 = null; private JTextArea jTextArea_2 = null; private JScrollPane jScrollPane1_1 = null; /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SwingUtilities.invokeLater(new Runnable() {
public void run() {
frame thisClass = new frame();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
} /**
* This is the default constructor
*/
public frame2() {
super();
this.setSize(411, 328);
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jScrollPane = new JScrollPane();
jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); // Generated
jPanel = new JPanel();
jLabel_2 = new JLabel();
jLabel_2.setText("JLabel_2"); // Generated
jLabel_1 = new JLabel();
jLabel_1.setText("JLabel_1"); // Generated
jTextArea_1 = new JTextArea();
jTextArea_1.setWrapStyleWord(true); // Generated
jTextArea_1.setLineWrap(true); // Generated
jTextArea_1.setText("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); // Generated
jScrollPane1_1 = new JScrollPane();
jScrollPane1_1.setViewportView(jTextArea_1);
jTextArea_2 = new JTextArea();
jTextArea_2.setWrapStyleWord(true); // Generated
jTextArea_2.setLineWrap(true); // Generated
jTextArea_2.setText("abcdefghijklmnopqrstuvwxyz"); // Generated
//GroupLayout相关的代码********************
GroupLayout layout = new GroupLayout(jPanel);
jPanel.setLayout(layout);
//自动设定组件、组之间的间隙
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
//设定水平组
GroupLayout.SequentialGroup hGp=layout.createSequentialGroup();
hGp.addGroup(layout.createParallelGroup()
.addComponent(jLabel_1)
.addComponent(jLabel_2))
.addGroup(layout.createParallelGroup()
.addComponent(jScrollPane1_1)
.addComponent(jTextArea_2));
layout.setHorizontalGroup(hGp);
//设定垂直组
GroupLayout.SequentialGroup vGp=layout.createSequentialGroup();
vGp.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(jLabel_1)
.addComponent(jScrollPane1_1)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(jLabel_2)
.addComponent(jTextArea_2)));
//.addComponent(b2));
layout.setVerticalGroup(vGp);
//*********************************************
jScrollPane.setViewportView(jPanel);
jContentPane.add(jScrollPane, BorderLayout.CENTER);
this.setContentPane(jContentPane);
this.setTitle("JFrame");
}
}运行结果如下图1:GroupLayout正确地调整了各组件的大小和位置。
用鼠标拖拉边框来扩大窗口,发现组件也随着窗口变大。如图2:可是当缩小窗口边框时,组件却不随窗口缩小……
不知道在设定GroupLayout中哪出错了。求各位大大指教!!
import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JScrollPane;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import java.awt.FlowLayout;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.GroupLayout;public class frame2 extends JFrame { private static final long serialVersionUID = 1L; private JPanel jContentPane = null; private JScrollPane jScrollPane = null; private JPanel jPanel = null; private JLabel jLabel_1 = null; private JTextArea jTextArea_1 = null; private JLabel jLabel_2 = null; private JTextArea jTextArea_2 = null; private JScrollPane jScrollPane1_1 = null; /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SwingUtilities.invokeLater(new Runnable() {
public void run() {
frame thisClass = new frame();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
} /**
* This is the default constructor
*/
public frame2() {
super();
this.setSize(411, 328);
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jScrollPane = new JScrollPane();
jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); // Generated
jPanel = new JPanel();
jLabel_2 = new JLabel();
jLabel_2.setText("JLabel_2"); // Generated
jLabel_1 = new JLabel();
jLabel_1.setText("JLabel_1"); // Generated
jTextArea_1 = new JTextArea();
jTextArea_1.setWrapStyleWord(true); // Generated
jTextArea_1.setLineWrap(true); // Generated
jTextArea_1.setText("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); // Generated
jScrollPane1_1 = new JScrollPane();
jScrollPane1_1.setViewportView(jTextArea_1);
jTextArea_2 = new JTextArea();
jTextArea_2.setWrapStyleWord(true); // Generated
jTextArea_2.setLineWrap(true); // Generated
jTextArea_2.setText("abcdefghijklmnopqrstuvwxyz"); // Generated
//GroupLayout相关的代码********************
GroupLayout layout = new GroupLayout(jPanel);
jPanel.setLayout(layout);
//自动设定组件、组之间的间隙
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
//设定水平组
GroupLayout.SequentialGroup hGp=layout.createSequentialGroup();
hGp.addGroup(layout.createParallelGroup()
.addComponent(jLabel_1)
.addComponent(jLabel_2))
.addGroup(layout.createParallelGroup()
.addComponent(jScrollPane1_1)
.addComponent(jTextArea_2));
layout.setHorizontalGroup(hGp);
//设定垂直组
GroupLayout.SequentialGroup vGp=layout.createSequentialGroup();
vGp.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(jLabel_1)
.addComponent(jScrollPane1_1)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(jLabel_2)
.addComponent(jTextArea_2)));
//.addComponent(b2));
layout.setVerticalGroup(vGp);
//*********************************************
jScrollPane.setViewportView(jPanel);
jContentPane.add(jScrollPane, BorderLayout.CENTER);
this.setContentPane(jContentPane);
this.setTitle("JFrame");
}
}运行结果如下图1:GroupLayout正确地调整了各组件的大小和位置。
用鼠标拖拉边框来扩大窗口,发现组件也随着窗口变大。如图2:可是当缩小窗口边框时,组件却不随窗口缩小……
不知道在设定GroupLayout中哪出错了。求各位大大指教!!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货