不幸的是,我是一个菜鸟,但是当我使用BorderLayout时,我不知道为什么我的JButtons不会定位自己......我想我的JPanel上缺少一些东西,但我不知道不知道还有什么...
这是我的代码:
import javax.swing.*;
import java.awt.*;
public class GUItest {
private static void createandshowGUI() {
JFrame frame = new JFrame("GUItest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setMinimumSize(new Dimension(600,600));
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));
frame.getContentPane().add(panel);
JLabel label = new JLabel("Hey thare");
panel.add(label, BorderLayout.SOUTH);
JButton button = new JButton("Click thus");
panel.add(button, BorderLayout.NORTH);
button.setPreferredSize(new Dimension(10,10));
JButton button2 = new JButton("Click meh");
panel.add(button2, BorderLayout.EAST);
button2.setMinimumSize(new Dimension(50,10));
frame.pack();
panel.setVisible(true);
frame.setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createandshowGUI();
}
});
// TODO Auto-generated method stub
}
}