-1
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
不能被解析爲一個類型上線十二人。我在哪裏錯了?我試圖創建一個隨機數生成器,只要點擊按鈕,但我似乎無法開始。PushCounterPanel不能被解析爲一個類型
確保PushCounterPanel編譯。話雖如此,爲什麼你想添加一個JFrame到JFrame?這是沒有意義的。你的PushCounterPanel不應該擴展JPanel嗎?另外,你爲什麼要發佈左 - 左對齊的代碼?你只需要努力發佈格式良好的代碼,因爲如果我們可以閱讀它,我們可以更好地理解它並幫助你。 –