根据您的指示,我决定使用GridBagLayout的,但我也面临着定位按钮的问题在面板的按钮预计将在右上,但它显示在中心,请告诉我是什么问题,我code`如何把按钮位于顶部右侧OG jpane
import java.awt.ComponentOrientation;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
public class Test extends JFrame {
private JPanel contentPane;
private JButton button2;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test frame = new Test();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Test() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 573, 410);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new GridBagLayout());
contentPane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
setContentPane(contentPane);
button2 = new JButton("button2");
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
contentPane.add(button2, c);
}
}
这个输出 http://postimg.org/image/bhnzskznj/
NetBeans IDE有一个很好的GUI编辑器,生成类似上面的代码。 – 2014-09-03 14:14:20