import javax.swing.JFrame;
import javax.swing.SwingUtilities;
>>>import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import javax.swing.JButton;
import javax.swing.BorderFactory;
public class GridBagLayout {
public static void createWindow(){
JFrame aWin = new JFrame("Title");
aWin.setBounds(0,0,200,200);
aWin.setVisible(true);
GridBagLayout gridBag = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
>>>aWin.getContentPane().setLayout(gridBag);
constraints.weightx = constraints.weighty = 10.0;
constraints.fill = GridBagConstraints.BOTH;
addButton(" Press ",constraints,gridBag,aWin);
constraints.gridwidth=GridBagConstraints.REMAINDER;
}
static void addButton(String label,GridBagConstraints constraints,GridBagLayout layout,JFrame window){
JButton button = new JButton(label);
button.setBorder(BorderFactory.createEtchedBorder());
>>>layout.setConstraints(button,constraints);
window.getContentPane().add(button);
}
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
createWindow();
}
});
}
}
标有 >>> 的行被 Eclipse IDE 标记为错误。
错误import java.awt.GridBagLayout;
表示可能与同一文件中的类型发生冲突
错误aWin.getContentPane().setLayout(gridBag);
表示无法应用 GridBagLayout 尽管当我尝试 FlowLayout 时,它很好。
错误layout.setConstraints(button,constraints);
表明该方法未定义。
这个错误的原因是什么?请帮我解决它。