我很難嘗試將JButton
s添加到我的JFrame
。JFrame/JButton錯誤消息
我已經創建了兩種方法(現在都在同一個類中)。如果我做showGUI
方法靜態的,然後我收到錯誤:
//Listen for actions on buttons.
next.addActionListener(this); (CANNOT USE THIS IN A STATIC CONTEXT)
previous.addActionListener(this); (CANNOT USE THIS IN A STATIC CONTEXT)
classify.addActionListener(this); (CANNOT USE THIS IN A STATIC CONTEXT)
並添加JButton
對象,以我的JFrame的時候,我收到以下錯誤:
add(next); (Cannot make a static reference to the non-static method add(Component) from the type Container)
add(previous); (Cannot make a static reference to the non-static method add(Component) from the type Container)
add(classify); (Cannot make a static reference to the non-static method add(Component) from the type Container)
我如何克服這個問題?我已經包括了我的方法如下,僅供參考:
public void showGUI(BufferedImage img){
next = new JButton("Next Image");
next.setMnemonic(KeyEvent.VK_N);
next.setActionCommand("disable");
previous = new JButton("Previous Image");
previous.setMnemonic(KeyEvent.VK_P);
previous.setActionCommand("disable");
classify = new JButton("Classify");
classify.setMnemonic(KeyEvent.VK_C);
classify.setActionCommand("disable");
//Listen for actions on buttons.
next.addActionListener(this);
previous.addActionListener(this);
classify.addActionListener(this);
add(next);
add(previous);
add(classify);
//Display image on the screen.
frame.setTitle("TITLE");
RMHC newContentPane = new RMHC();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(new JLabel(new ImageIcon(img)));
frame.pack();
frame.setVisible(true);
frame.isResizable();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
爲了更好的幫助更快,發佈[SSCCE](http://sscce.org/)。 – 2012-03-04 21:45:12
爲什麼當您在之後立即創建新對象時,將nextImg設置爲next? – 2012-03-04 21:48:14
@stas我的代碼現在已經更新。 – MusTheDataGuy 2012-03-04 22:03:41