简体   繁体   中英

Changing Image on click with Java/Swing Jframe

OK there are similar questions out there, but the problem I have is specific.

What I have is a simple jFrame with 3 buttons. When one of the buttons is pressed it will calculate the BPM of a song. It will also edit a StringBuilder object to create a url path for the image. This part of the out put is fine here is my console output on press.

104.9      //Bpm of Song
One        //String representation of first char in array
Zero       //String representation of Second char in array
Four       //String representation of Third char in array
Nine       //String representation of Fifth char in array
res/1.png  // 
res/0.png  // New Url paths
res/4.png  //
res/9.png  //

The problem I have is in the code.

JButton tapButton = new JButton("Tap");
    tapButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            MathFunctions.tapBPM();
            ArrayComparison.stringCharArrays();

            String urlLocationOne = (ArrayComparison.urlLocationOneBuild.toString());
            String urlLocationTwo = (ArrayComparison.urlLocationTwoBuild.toString());
            String urlLocationThree = (ArrayComparison.urlLocationThreeBuild.toString());
            String urlLocationFive = (ArrayComparison.urlLocationFiveBuild.toString());

        }
    });
    tapButton.setBounds(100, 150, 120, 45);
    contentPane.add(tapButton);

urlLocation[number]Build is a StringBuilder object. On each click it checks the template url, deletes the number and replaces it (this bit we know works fine). The problem comes with the images. If I create a new image in the tapButton ActionEvent it doesn't load, I have to create the image as it's own JLabel (outside of the button)

(here is the code for the JLabel (there are five of these i'll show you only one))

JLabel locationOne = new JLabel("Image 1");
locationOne.setBackground(Color.WHITE); 
locationOne.setIcon(new ImageIcon(urlLocationOne));   //urlLocationOne not available outside of button
locationOne.setBounds(84, 38, 66, 100);
contentPane.add(locationOne);

so if the imageIcon is created inside the button it doesn't work, if it's created outside the button, it is outside of "urlLocationOne"'s scope and if I create a field I create an immutable string that only changes inside the tap button and when called outside the tapButton method it displays the original string. What is the best way to place the images so that the image's respond the path changes on each click. (remember the paths are changing I just can't get the ImageIcon JLabel to 'hear' the change.)

OK so after two days of persevering with the problem drawing diagrams and a lot of stress, it seemed the answer came from a good nights sleep. and here it is

final JLabel locationOne = new JLabel("Image 1");
    locationOne.setBackground(Color.WHITE);
    tapButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
    String urlLocationOne = (ArrayComparison.urlLocationOneBuild.toString());
    locationOne.setIcon(new ImageIcon(urlLocationOne));
        }
    });
    locationOne.setBounds(84, 38, 66, 100);
    contentPane.add(locationOne);

Rather than trying to access the string created in another method using returns and nonsense that just wouldn't work for some reason. I created a new action listener within the jLabel for the Image. This listened to the same button, but only had one action - create the string locally. (Seems so logically when you look at it in hindsight!!!!!!)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM