【问题标题】:GridBagLayout being flagged as errorGridBagLayout 被标记为错误
【发布时间】:2012-07-26 20:56:23
【问题描述】:
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); 的错误表明该方法未定义。
这个错误的原因是什么?请帮我解决。

【问题讨论】:

    标签: java swing layout awt layout-manager


    【解决方案1】:

    这些是由于您将班级命名为GridBagLayout 造成的。对于 java.awt.GridBagLayout 类的所有用途,您需要更改类的名称或使用显式完整路径,而不是对其使用导入。

    【讨论】:

      【解决方案2】:

      编译错误几乎描述了问题。只需将您的类重命名为 GridBagLayoutTest。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-28
        • 2023-01-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-30
        • 1970-01-01
        相关资源
        最近更新 更多