我正在使用netbeans编译器版本7.11。 我正在尝试创建GridLayout但它显示错误 在我编写代码的行中,为Layout创建按钮。
它给出了以下errors
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();
}
}
答案 0 :(得分:2)
您似乎面临的问题是您正在进入JButton
2 String
,并且没有带有该签名的构造函数。试试这个
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
。