不兼容的类型GridLayout无法转换为布局管理器

时间:2017-07-25 12:47:30

标签: java swing jpanel grid-layout

这是我的演示代码,请查看为什么这段代码给我带来了问题。

package gridlayout;

import java.awt.*;
import javax.swing.*;

public class GridLayout {

   public static void main(String[] args) {
    EventQueue.invokeLater(()->{

        MyFrame frame = new MyFrame();
        //frame.setDefaultCloseOperation(0);
        //frame.setTitle("Grid Layout");
       // frame.setDefault
    });
  }
}

class MyFrame extends JFrame{

public MyFrame(){

    setTitle("My Programm");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    add(new MyPanel());
    pack();
    setVisible(true);
    }
 }

class MyPanel extends JPanel{
  private JButton display;
  private  double result;
  private String lastCommand;
  private boolean start;
  private JPanel panel;

// private static final int n = 4; 
  public MyPanel(){


    setLayout(new BorderLayout());
     //setLayout(new GridLayout(4,4));

    result = 0;
    lastCommand = "=";
    start = false;
    display = new JButton("0");

    setEnabled(false);
    add(display, BorderLayout.NORTH);

//        panel = new JPanel();
//        panel.setLayout(new GridLayout());

         GridLayout expLayout = new GridLayout(0,4);
}

public void addButtons(String Name){
    JButton button = new JButton(Name);
   }
}

1 个答案:

答案 0 :(得分:0)

问题似乎在于你有这个代码:

GridLayout

由于您已在其他类所在的包中定义了自定义类,并且该类名为panel.setLayout(new GridLayout()),因此当您执行GridLayout时,这两个名称会发生​​冲突 为避免这种情况,请重命名自定义panel.setLayout(new java.awt.GridLayout())以使其具有不同的名称或尝试panel.setLayout(new GridLayout())而不是 if(!isset($_SESSION)){ session_start(); } $username = $_SESSION['username']; require_once('connection.php'); $sql = "INSERT INTO [test].[dbo].[user_log] ([username] ,[login_date] ) VALUES ('$_SESSION[username]' ,GETDATE() )"; $result = sqlsrv_query($conn,$sql); if ($result === false ) { echo "Error in query "; exit(); }

P.S。 - 正如@XtremeBaumer正确地注意到你应该避免使用来自JDK分发包或其他知名库的众所周知的类名(至少你应该理解风险,如果你仍然需要你的类有这样的名字)