TBD.my

Full Version: array problem in GUI
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
soklan :

[Image: SGZXx.png]

ni code aku :

Code:
import java.awt.*;
import javax.swing.*;

public class assignmentmay2009 extends JFrame {
    
    private JButton bt1,bt2,bt3;
    //private JPanel p1,p2;
    private JLabel label1,label2;
    private ImageIcon ic;

    public static void main(String [] args){
        
        assignmentmay2009 as = new assignmentmay2009("Button & Icons");
        as.setSize(400,300);
        as.setVisible(true);
        as.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
    }
    
    assignmentmay2009(String t){
        
        setTitle(t);
        setLayout(new BorderLayout());
        
        bt1 = new JButton("Elephant");
        bt2 = new JButton("Mickey");
        bt3 = new JButton("Butterfly");
        ic = new ImageIcon("/Users/Afiq/Desktop/butterfly.jpeg");
        ///Users/Afiq/Desktop/Java Workspace/assignment 2/src/Mickey.jpg
        label1 = new JLabel(ic);
        
        JPanel p1 = new JPanel();
        p1.add(label1);
        
        JPanel p2 = new JPanel();
        p2.setLayout(new FlowLayout());
        p2.add(bt1);
        p2.add(bt2);
        p2.add(bt3);
        
        //set color for component
        //Color c = new Color(0,255,255);
        bt1.setForeground(Color.RED);
        bt1.setBackground(Color.RED);
        bt2.setForeground(Color.cyan);
        bt3.setForeground(Color.yellow);
        
        
        add(p1,BorderLayout.NORTH);
        add(p2,BorderLayout.CENTER);
        
    }
    
}

problem aku sekarang camner nak apply array to store the icon images??
Code:
package buttonAndIcons;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class buttonAndIcons extends JFrame implements ActionListener {
    private JButton Elephant, Mickey, Butterfly;
    private JLabel labelOfAnimals;
    private String elephant = "Elephant.jpg";
    private String mickey = "mickey.jpg";
    private String butterfly = "butterfly1.jpg";
    private String[] typesOfAnimal ={elephant,mickey,butterfly};
    private JPanel mainPanel;

    public buttonAndIcons(){
        //Set frame behaviour
        super("Button and Icons");
        setVisible(true);
        setSize(400,400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//exit
        setResizable(true);
        setLocationRelativeTo(null);
        getContentPane().setLayout(null);

        labelOfAnimals = new JLabel();
        labelOfAnimals.setLocation(20, 0);
        labelOfAnimals.setSize(350,350);    

        //create object button
        Elephant = new JButton ("Elephant");
        Mickey = new JButton ("Mickey");
        Butterfly = new JButton("Butterfly");

        //store image in array
        String elephant = "Elephant.jpg";
        String mickey = "mickey.jpg";
        String butterfly = "butterfly1.jpg";

        //add button to frame
        getContentPane().add(Elephant);
        getContentPane().add(Mickey);
        getContentPane().add(Butterfly);
        getContentPane().add(labelOfAnimals);

        //place button to frame
        Elephant.setLocation(30, 320);
        Elephant.setSize(100,30);
        Mickey.setLocation(140, 320);
        Mickey.setSize(100,30);
        Butterfly.setLocation(250, 320);
        Butterfly.setSize(100,30);

        //add button listener
        Elephant.addActionListener(this);
        Mickey.addActionListener(this);
        Butterfly.addActionListener(this);
    }
    @Override
    public void actionPerformed(ActionEvent ae) {
        if (ae.getSource()==Elephant){
            ImageIcon image = new ImageIcon(typesOfAnimal[0]);
            labelOfAnimals = new JLabel(" ", image, JLabel.CENTER);
            getContentPane().add(labelOfAnimals);
            labelOfAnimals.setLocation(20, 0);
            labelOfAnimals.setSize(350,350);
            validate();
        }
        else if (ae.getSource()==Mickey){
            getContentPane().remove(labelOfAnimals);
            ImageIcon image = new ImageIcon(typesOfAnimal[1]);
            labelOfAnimals = new JLabel(" ", image, JLabel.CENTER);
            getContentPane().add(labelOfAnimals);
            labelOfAnimals.setLocation(20, 0);
            labelOfAnimals.setSize(350,350);
            validate();            
        }
        if (ae.getSource()==Butterfly){
            getContentPane().remove(labelOfAnimals);
            ImageIcon image = new ImageIcon(typesOfAnimal[2]);
            labelOfAnimals = new JLabel(" ", image, JLabel.CENTER);
            getContentPane().add(labelOfAnimals);
            labelOfAnimals.setLocation(20, 0);
            labelOfAnimals.setSize(350,350);
            validate();    
        }
    }
}

maksud ko cm ni ker..simpan gambar dalam array.

Code:
private String elephant = "Elephant.jpg";
    private String mickey = "mickey.jpg";
    private String butterfly = "butterfly1.jpg";
    private String[] typesOfAnimal ={elephant,mickey,butterfly};