我試圖在Java中的GUI,使用這些方針的東西:GUI在Java中,私有類的監聽器不工作
public class GUIApp
{
DrawingPanel dp;
buttonPanel bp;
ova = Oval;
public GUIApp()
{
JFrame win = new JFrame("Drawing App");
dp = new DrawingPanel();
bp = new buttonPanel(this);
//Settings for panels and frame
ova = new Oval(100,100,100,100);
}
public void setOval(int c){
//Change color of oval
}
}
然後在我的buttonPanel類我有這樣的:
public class ButtonPanel extends JPanel
{
private JButton btnRed, btnGreen, btnBlue;
public ButtonPanel(GUIApp d)
{
ButtonListener listener = new ButtonListener();
btnRed = new JButton("Red");
btnGreen = new JButton("Green");
btnBlue = new JButton("Blue");
btnRed.addActionListener(listener);
btnGreen.addActionListener(listener);
btnBlue.addActionListener(listener);
setBackground(Color.lightGray);
GridLayout grid = new GridLayout(3,1);
add(btnRed,grid);
add(btnGreen,grid);
add(btnBlue,grid);
}
private class ButtonListener implements ActionListener{
public void clickButton(ActionEvent event) {
Object location = event.getSource();
if (location == btnRed){
d.setOval(1);
}
else if(location == btnGreen){
d.setOval(2);
}
else if(location == btnBlue){
d.setOval(3);
}
}
}
}
但netbeans給內部的ButtonListener類提供了一個錯誤,我不知道爲什麼。我也不知道如何正確地從該類中調用setOval到GUIApp類。我究竟做錯了什麼?
所以我們知道你得到一個錯誤......但我們不知道那個錯誤是什麼。 *總是*說錯誤是什麼 - 否則就像去看醫生,說你感覺不好,但不是以什麼方式說。請閱讀http://tinyurl.com/so-hints – 2010-09-13 06:32:35