2015-10-06 107 views
-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不能被解析爲一個類型

+0

確保PushCounterPanel編譯。話雖如此,爲什麼你想添加一個JFrame到JFrame?這是沒有意義的。你的PushCounterPanel不應該擴展JPanel嗎?另外,你爲什麼要發佈左 - 左對齊的代碼?你只需要努力發佈格式良好的代碼,因爲如果我們可以閱讀它,我們可以更好地理解它並幫助你。 –

回答

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 { 

而且夜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 
} 

並填寫其餘

相關問題