1

我正在使用 netbeans 编译器 7.11 版。我正在尝试创建一个 GridLayout,但它在我编写代码以创建布局按钮的行中显示错误。

它给出了以下错误

no suitable constructor found for JButton(Java.lang.String.java.lang.String)
Constructor javax.swing.JButton.JButton(java.lang.String,javax.swing.Icon)is not applicable
(actual argument Java.lang.String cannot be converted to javax.swing.Icon by method
 invocation conversion)
Constructor javax.swing.JButton.JButton(javax.swing.Action)is not applicable
(actual and formal argument lists differ in length)
Constructor javax.swing.JButton.JButton(java.lang.String)is not applicable
(actual and formal argument lists differ in length)
Constructor javax.swing.JButton.JButton(javax.swing.Icon)is not applicable
(actual and formal argument lists differ in length)
Constructor javax.swing.JButton.JButton()is not applicable
(actual and formal argument lists differ in length)

它从第 7 行到第 11 行显示了这些错误。下面是我输入 Java Netbeans 编译器的代码。

import java.awt.*;
import javax.swing.*;
public class Griddemo extends JFrame{
public Griddemo()
{
  setLayout(new GridLayout(3,2));
  add(new JButton("Row-1","Col-1")); ---- line7
  add(new JButton("Row-1","Col-2")); ---- line8
  add(new JButton("Row-2","Col-1")); ---- line9
  add(new JButton("Row-2","Col-2")); ---- line10
  add(new JButton("Row-3","Col-1")); ---- line11
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  pack();
  setVisible(true);
}
public static void main(String args[])
 {
   new Griddemo();
 }
}
4

1 回答 1

2

您似乎面临的问题是您正在传递JButton2 Strings,并且没有具有该签名的构造函数。试试这个

add(new JButton("Row-1, Col-1")); ---- line7
add(new JButton("Row-1, Col-2")); ---- line8
add(new JButton("Row-2, Col-1")); ---- line9
add(new JButton("Row-2, Col-2")); ---- line10
add(new JButton("Row-3, Col-1")); ---- line11

使每个按钮标签都具有描述性,但只有一个String.

于 2012-07-29T07:14:26.970 回答