버튼에 대한 액션이 생기면 그에 따른 변화( 현재 이 코드에선 버튼의 이미지를 변화 시키기 위한 것 )가 안된다고 하기에 올려진 문의와 답변입니다.
문의내용
Hi, I have been fiddling with this for some time with no result I was wondering if anyone could tell me how to do this
Basically I have an action listener in another class that is supposed to change the colour of the buttons when it is pressed, below is the code for the action performed method. I have tried revalidate and repaint in various ways but keep getting a null pointer exception, please help:
public void actionPerformed(ActionEvent e)
{
forwardButton.setIcon(newImageIcon("images/forwardButtonBlue.jpg"));
stopButton.setIcon(new ImageIcon("images/stopButtonBlue.jpg "));
backButton.setIcon (new ImageIcon("images/backButtonBlue.jpg"));
magButton.setIcon (new ImageIcon("images/magnifyButtonBlue.jpg"));
homeButton.setIcon (new ImageIcon("images/homeButtonBlue.jpg"));
refreshButton.setIcon(new ImageIcon("images/refreshButtonBlue.jpg"));
}
답변내용
5 helpful answers below.
Sat, 14 Jul 2007 18:24:00 GMT | (1) camickra
> but keep getting a null pointer exception,
Where? We can't tell what the problem is based on a few lines of code.
Maybe its because you are trying to change the icon of the currently pressed button. If so then try wrapping your code in a SwingUtilities.invokeLater(...);
If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.
And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the code retains its original formatting.
Sat, 14 Jul 2007 18:24:00 GMT | (2) fellovertodaya
import javax.swing.text.html.*; import javax.swing.text.*; import javax.swing.event.*; import javax.swing.*; import java.io.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class button { public static void main(String[] args) { JFrame f = new JFrame (); final ImageIcon forwardButtonI = new ImageIcon("images/forwardButtonBlue.jpg"); final ImageIcon backButtonI = new ImageIcon("images/backButtonBlue.jpg"); JToolBar sliderBar = new JToolBar(); f.add(sliderBar); //create forward and back buttons final JButton forwardButton = new JButton (forwardButtonI ); final JButton backButton = new JButton (backButtonI); sliderBar.add(backButton); sliderBar.add(forwardButton); forwardButton.addActionListener (new greenListener(forwardButton,backButton)); f.pack(); f.show(); } } class greenListener implements ActionListener { private JButton backButton; private JButton forwardButton; public greenListener(JButton forwardButton, JButton backButton) { } public void actionPerformed(ActionEvent e) { backButton.setIcon(new ImageIcon("images/backButtonGreen.jpg")); } }
Sat, 14 Jul 2007 18:24:00 GMT | (3) fellovertodaya
Thanks for your help, I have posted some example code above, hope you can spot where I have gone wrong
Sat, 14 Jul 2007 18:24:00 GMT | (4) michael_dunna
public greenListener(JButton forwardButton, JButton backButton) { this.backButton = backButton; this.forwardButton = forwardButton;
public greenListener(JButton forwardButton, JButton backButton) {
this.backButton = backButton;//<-
this.forwardButton = forwardButton;//<-
}
Sat, 14 Jul 2007 18:24:00 GMT | (5) fellovertodaya
oops silly mistakethanks
출처 : http://www.thatsjava.com/java-core-gui-apis/29631/
'Software > JAVA' 카테고리의 다른 글
(이전글 2006년) Java Technology Forums - Error message in JNI (0) | 2013.12.05 |
---|---|
(이전글 2006년) jni 사용예 (0) | 2013.12.05 |
(이전글 2006년) JNI (0) | 2013.12.05 |
(이전글 2009년) - 리눅스 환경에서 JAVA를 이용한 USB이용에 대한 문의드립니다. (0) | 2013.12.04 |
JBUTTON 내부의 텍스트를 회전 하기 위한 방법 (0) | 2012.03.02 |