答案 0 :(得分:1)
以下是mcve,演示了使用GridBagLayout
as proposed by MadProgrammer的解决方案。
在问题中加入这样的mcve可以使帮助变得更加容易,并且您获得更好答案的机会也会更高。
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;
public class SwingMain {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setLayout(new GridBagLayout()); //place desired content in a GridbagLayout
frame.add(new TestPane());
frame.pack();
frame.setVisible(true);
}
}
class TestPane extends JPanel{
TestPane() {
Border padding = BorderFactory.createEmptyBorder(10, 10, 10, 10);
setBorder(padding);
setLayout(new GridLayout(2, 2, 10, 10));
add(new JLabel("Celsius"));
add(new JTextField(10));
add(new JLabel("Fahrenheit"));
add(new JTextField(10));
}
}