【发布时间】:2012-09-29 11:06:31
【问题描述】:
我正在尝试用 Java 创建一个 SpringLayout,这是我拥有的代码(从 Oracle Java 文档中获得)
import javax.swing.*;
import java.awt.*;
public class SpringForm {
private static void createAndShowGUI() {
String[] labels = {"Side1: ", "Side2: ", "Side3: "};
int numPairs = labels.length;
//Create and populate the panel.
JPanel p = new JPanel(new SpringLayout());
javax.swing.JButton calculate_btn;
calculate_btn = new javax.swing.JButton();
for (int i = 0; i < numPairs; i++) {
JLabel l = new JLabel(labels[i], JLabel.TRAILING);
p.add(l);
JTextField textField = new JTextField(10);
l.setLabelFor(textField);
p.add(textField);
}
calculate_btn.setText("Calculate");
p.add(calculate_btn);
//Lay out the panel.
SpringUtilities.makeCompactGrid(p,
numPairs, 2, //rows, cols
6, 6, //initX, initY
6, 6); //xPad, yPad
//Create and set up the window.
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the content pane.
p.setOpaque(true); //content panes must be opaque
frame.setContentPane(p);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
通过上面的代码,我得到了这个
但实际上我希望得到这个
我不介意任何其他布局!只是oracle帮助表明springlayout真的很接近我的需要 我想做的是得到如下图的布局,我不确定下面使用的布局是什么,而且在我尝试使用 SpringLayout 时,我注意到当我们扩展窗口大小时控件会自动改变它们的大小我想禁用它,但它有点没有意义,因为 SpringLayout 清楚地表示它在做什么,调整控件,当我们调整窗口时
【问题讨论】:
-
已经尝试了一整天,以编程方式获取布局,我不介意任何布局。
标签: java swing user-interface layout springlayout