import javax.swing.*;
public class PushCounter
{
//-----------------------------------------------------------------
// Creates the main program frame.
//-----------------------------------------------------------------
public static void main(String[] args)
{
JFrame frame = new JFrame("Push Counter");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new PushCounterPanel());
frame.pack();
frame.setVisible(true);
}
}
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
无法解析为第12行的类型。我在哪里错了?我在点击按钮时尝试创建一个随机数生成器,但我似乎无法开始。
答案 0 :(得分:1)
这不会编译:
import java.util.Random;
import javax.swing.*;
public PushCounterPanel extends JFrame{
应该是
import java.util.Random;
import javax.swing.*;
// need the class declaration
public class PushCounterPanel extends JFrame{
话虽如此,为什么要尝试将JFrame添加到JFrame?这是没有意义的。您的PushCounterPanel不应该扩展JPanel吗?
所以真的应该是:
import java.util.Random;
import javax.swing.*;
// need the class declaration and that it extends JPanel
public class PushCounterPanel extends JPanel {
此外,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);
}
}
}
你正在使用未声明的变量,你试图在属于构造函数或方法的类中使用裸体语句,...就好像你只是在墙上抛出代码并看到了什么棒,那就是永远不会工作 - 永远不要尝试为坏代码添加好的代码。要么使用IDE,如果你的代码没有编译就会立即标记你,或者你不允许使用它,然后经常编译你的代码,并在尝试添加好的代码之前先修复所有的编译错误。您应该删除PushCounterPanel类并重新开始。从这段代码框架开始:
import javax.swing.*;
public class PushCounterPanel extends JPanel {
// TODO: put variables here
public PushCounterPanel() {
// TODO finish this constructor
}
// TODO: put methods here
}
并填补其余部分。