【问题标题】:error on GridLayout网格布局错误
【发布时间】:2014-04-09 21:35:40
【问题描述】:
import java.awt.Container; //Container or *
import javax.swing.*; //JFrame, JLabel, *, or etc...

public class NumerologyEC extends JFrame
{
    private static final int Width = 400;
    private static final int Height = 300;

    private JLabel word1;

   public  NumerologyEC()
   {
       setTitle ("Numerology Extra Credit");
       word1 = new JLabel ("Enter a word: ", SwingConstants.RIGHT);

       Container pane = getContentPane();
       pane.setLayout (new GridLayout (1, 2));

       pane.add(word1);

       setSize(Width, Height);
       setVisible (true);
       setDefaultCloseOperation (EXIT_ON_CLOSE);
    }

   public static void main (String[] args)
   {
    NumerologyEC rectObject = new NumerologyEC();
    }
}

我在“新 GridLayout”上不断收到错误消息。我正在为我的课学习这本书,它没有解释我是否需要导入某些东西或声明它以使其工作。任何提示将不胜感激。

【问题讨论】:

  • 如果您使用 IDE,例如 Eclipse、NetBeans 或 IntelliJ IDEA,它可以帮助您添加必要的导入(在 IDEA 中,可以键入 Alt-Enter,在 Eclipse 中,Shift-Ctrl-O )。

标签: java swing layout import grid


【解决方案1】:

您还需要导入GridLayout。添加此导入

import java.awt.GridLayout;

或者您可以将导入更改为以下以导入包中的所有内容

import java.awt.*;

或者显式写

new java.awt.GridLayout (1, 2)

【讨论】: