2013-04-25 33 views
2

我試圖創建一個圖像JButton點擊並顯示文本,一旦它被點擊,但我似乎無法弄清楚如何使其工作。我對java很陌生,所以大量基本的解釋對我非常有幫助。這裏是我目前正在使用的代碼。如何使jbutton可點擊並顯示文本

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class States extends JFrame { 
    private JTabbedPane jtpFigures = new JTabbedPane(); 


    //State Labels 


    private JButton VTPanel = new JButton(); 
    frame.add(VTPanel); 
    VTPanel.addActionListener(new ActionListener(){ 
    public void actionPerformed(ActionEvent); 
    System.out.println("The State Capital of VT is Montpelier"); 
} 



    private JButton NYPanel = new JButton(); 
    private JButton CAPanel = new JButton(); 
    private JButton MEPanel = new JButton(); 
    private JButton NHPanel = new JButton(); 
    private JButton CTPanel = new JButton(); 
    private JButton MAPanel = new JButton(); 
    private JButton FLPanel = new JButton(); 
    private JButton HIPanel = new JButton(); 
    private JButton NDPanel = new JButton(); 

    //Images for each of the states 
    private ImageIcon[] stateImage = { 
    new ImageIcon("image/VT.png"), 
    new ImageIcon("image/NY.png"), 
    new ImageIcon("image/CA.png"), 
    new ImageIcon("image/ME.png"), 
    new ImageIcon("image/NH.png"), 
    new ImageIcon("image/CT.png"), 
    new ImageIcon("image/MA.png"), 
    new ImageIcon("image/FL.png"), 
    new ImageIcon("image/HI.png"), 
    new ImageIcon("image/ND.png")}; 



    public States() { 

     //adds each of the images to the panel 
     VTPanel.setIcon(stateImage[0]); 
     NYPanel.setIcon(stateImage[1]); 
     CAPanel.setIcon(stateImage[2]); 
     MEPanel.setIcon(stateImage[3]); 
     NHPanel.setIcon(stateImage[4]); 
     CTPanel.setIcon(stateImage[5]); 
     MAPanel.setIcon(stateImage[6]); 
     FLPanel.setIcon(stateImage[7]); 
     HIPanel.setIcon(stateImage[8]); 
     NDPanel.setIcon(stateImage[9]); 



    //Adds the panels and name 
     add(jtpFigures, BorderLayout.CENTER); 
     jtpFigures.add(VTPanel, "Vermont"); 
     jtpFigures.add(NYPanel, "New York"); 
     jtpFigures.add(CAPanel, "California"); 
     jtpFigures.add(MEPanel, "Maine"); 
     jtpFigures.add(NHPanel, "New Hampshire"); 
     jtpFigures.add(CTPanel, "Connecticut"); 
     jtpFigures.add(MAPanel, "Massachusetts"); 
     jtpFigures.add(FLPanel, "Florida"); 
     jtpFigures.add(HIPanel, "Hawaii"); 
     jtpFigures.add(NDPanel, "North Dakota"); 


     //Sets the default index 
     jtpFigures.setSelectedIndex(3); 
    } 


    public static void main(String[] args) { 
     States frame = new States(); 
     frame.pack(); 
     frame.setTitle("State License Plates"); 
     frame.setLocationRelativeTo(null); // Center the frame 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 
     frame.setSize(560,250); 

    } 
} 
+3

'public void actionPerformed(ActionEvent);'應該是'public void actionPerformed(ActionEvent e){....}'否則它不會編譯。 – Smit 2013-04-25 19:24:09

+0

而我們的問題是你想在哪裏顯示你的文字? – Smit 2013-04-25 19:25:56

+2

重複 - 同一問題4小時前問:http://stackoverflow.com/questions/16217903/changing-image-to-jbutton-that-will-pop-up-text – camickr 2013-04-25 19:26:50

回答