Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java PushCounterPanel无法解析为类型_Java_Swing_Types_Jpanel - Fatal编程技术网

Java PushCounterPanel无法解析为类型

Java PushCounterPanel无法解析为类型,java,swing,types,jpanel,Java,Swing,Types,Jpanel,我不断发现错误PushCounterPanel无法解析为第12行的类型。我哪里出了问题?每当我单击按钮时,我都会尝试创建一个随机数生成器,但我甚至无法开始。这将无法编译: import java.util.Random; import javax.swing.*; public PushCounterPanel extends JFrame{ count = 0; push = new JButton("Push Me!"); push.addActionListener(new Butto

我不断发现错误
PushCounterPanel
无法解析为第12行的类型。我哪里出了问题?每当我单击按钮时,我都会尝试创建一个随机数生成器,但我甚至无法开始。这将无法编译:

import java.util.Random;
import javax.swing.*;

public PushCounterPanel extends JFrame{

count = 0;
push = new JButton("Push Me!");
push.addActionListener(new ButtonListener());
label = new JLabel("Pushes: " + count);
add(push);
add(label);
setPreferredSize(new Dimension(300, 40));
setBackground(Color.cyan);
}
//*****************************************************************
// Represents a listener for button push (action) events.
//*****************************************************************
private class ButtonListener implements ActionListener
{
//--------------------------------------------------------------
// Updates the counter and label when the button is pushed.
//--------------------------------------------------------------
public void actionPerformed(ActionEvent event)
{
count++;
label.setText("Pushes: " + count);
}
}
}
import java.util.Random;
import javax.swing.*;

// need the class declaration and that it extends JPanel    
public class PushCounterPanel extends JPanel {
应该是

import java.util.Random;
import javax.swing.*;

public PushCounterPanel extends JFrame{
话虽如此,为什么要尝试将JFrame添加到JFrame?这毫无意义。你的推板不是应该延伸到JPanel吗

所以它实际上应该是:

import java.util.Random;
import javax.swing.*;

// need the class declaration    
public class PushCounterPanel extends JFrame{
此外,yoru PushCounterPanel的其余部分也不会编译:

import java.util.Random;
import javax.swing.*;

public PushCounterPanel extends JFrame{

count = 0;
push = new JButton("Push Me!");
push.addActionListener(new ButtonListener());
label = new JLabel("Pushes: " + count);
add(push);
add(label);
setPreferredSize(new Dimension(300, 40));
setBackground(Color.cyan);
}
//*****************************************************************
// Represents a listener for button push (action) events.
//*****************************************************************
private class ButtonListener implements ActionListener
{
//--------------------------------------------------------------
// Updates the counter and label when the button is pushed.
//--------------------------------------------------------------
public void actionPerformed(ActionEvent event)
{
count++;
label.setText("Pushes: " + count);
}
}
}
import java.util.Random;
import javax.swing.*;

// need the class declaration and that it extends JPanel    
public class PushCounterPanel extends JPanel {
您正在使用未声明的变量,您正在尝试在类中使用属于构造函数或方法的裸语句,。。。这就好像你只是在把代码扔到墙上,看看有什么东西粘在墙上,这是行不通的——永远不要试图在坏代码中添加好代码。要么使用IDE,如果代码未编译,它会立即标记您,要么不允许您使用IDE,然后频繁编译代码,在尝试将好代码添加到坏代码之前,先修复所有编译错误。您应该删除PushCounterPanel类并重新开始。从以下代码框架开始:

import java.util.Random;
import javax.swing.*;

public PushCounterPanel extends JFrame{

count = 0;
push = new JButton("Push Me!");
push.addActionListener(new ButtonListener());
label = new JLabel("Pushes: " + count);
add(push);
add(label);
setPreferredSize(new Dimension(300, 40));
setBackground(Color.cyan);
}
//*****************************************************************
// Represents a listener for button push (action) events.
//*****************************************************************
private class ButtonListener implements ActionListener
{
//--------------------------------------------------------------
// Updates the counter and label when the button is pushed.
//--------------------------------------------------------------
public void actionPerformed(ActionEvent event)
{
count++;
label.setText("Pushes: " + count);
}
}
}

并填写其余的。

确保已编译PushCounterPanel。话虽如此,为什么要尝试将JFrame添加到JFrame?这毫无意义。你的推板不是应该延伸到JPanel吗?另外,为什么要发布所有左对齐的代码?您可能希望只发布格式良好的代码,因为如果我们能够阅读它,我们可以更好地理解它并帮助您。