2016-03-22 23 views
0

Sadece bir düğmeye tıklandığında uygun ses dosyasını çalan basit bir gitar Tuner uygulaması yapmaya çalışıyorum. Bir gitarın bir kafasının görüntüsünü JFrame'e yerleştirmeye çalışıyorum ama bunun için Oracle belgelerini anlamakta biraz sorun yaşıyorum. Birisi bana resmimin bu kodda neden üretmediğini açıklayabilir mi?görüntüleri

p.s. Düğmeler için sınırın yerleştirilmesinin belirli bir yerde ayarlanmadığını ve hiçbir ses kodu ayarlanmadığını biliyorum. Öncelikle düğmelerdeki ön ayar görüntüsü üzerindeki uygun ayar pimi üzerine yerleştirmek istedim.

Buraya herhangi bir ipucu veya bilgi için şimdiden teşekkür ederiz!

package guitartuner; 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import java.text.*; 
import java.awt.image.*; 
import java.io.*; 
import javax.imageio.*; 


public class GuitarTuner extends JFrame{ 

private JFrame mainFrame; 
private JLabel EJLabel, AJLabel, DJLabel, GJLabel, BJLabel, eJLabel, guitarJLabel, 
     bassJLabel, loopJLabel; 
private JPanel controlPanel; 
private JButton EButton, AButton, DButton, GButton, BButton, eButton; 

public class LoadImageApp extends Component{ 
    BufferedImage img; 

    public void paint(Graphics g){ 
     g.drawImage(img, 0, 0, null); 
    } 

    public LoadImageApp(){ 
     try{ 
      img = ImageIO.read(new File("headstock.jpg")); 
     } catch (IOException e){  
     } 
    } 
    public Dimension getPreferredSize() { 
     if (img == null) { 
      return new Dimension(100,100); 
     } else { 
      return new Dimension(img.getWidth(null), img.getHeight(null)); 
     } 
    } 
} 

public GuitarTuner(){ 
    createUserInterface(); 
} 

public void createUserInterface(){ 
    JFrame f = new JFrame("Guitar Tuner"); 

    f.addWindowListener(new WindowAdapter(){ 
     public void windowClosing(WindowEvent e){ 
      System.exit(0); 
     } 
    }); 

    f.add(new LoadImageApp()); 
    f.pack(); 
    f.setVisible(true); 

    guitarJLabel = new JLabel(); 
    guitarJLabel.setBounds(16, 16, 90, 21); 
    guitarJLabel.setText("Guitar"); 
    f.add(guitarJLabel); 

    EButton = new JButton(); 
    EButton.setBounds(20, 20, 50, 50); 
    EButton.setText("E"); 
    f.add(EButton); 

    AButton = new JButton(); 
    AButton.setBounds(40, 40, 50, 50); 
    AButton.setText("A"); 
    f.add(AButton); 

    DButton = new JButton(); 
    DButton.setBounds(60, 60, 50, 50); 
    DButton.setText("D"); 
    f.add(DButton); 

    GButton = new JButton(); 
    GButton.setBounds(20, 100, 50, 50); 
    GButton.setText("G"); 
    f.add(GButton); 

    BButton = new JButton(); 
    BButton.setBounds(40, 100, 50, 50); 
    BButton.setText("B"); 
    f.add(BButton); 

    eButton = new JButton(); 
    eButton.setBounds(60, 100, 50, 50); 
    eButton.setText("e"); 
    f.add(eButton); 

    setTitle("Aaron's Awesome Guitar Tuner"); 
    setSize (500, 500); 
    setVisible(true); 
} 

public static void main(String[] args) { 
    GuitarTuner application = new GuitarTuner(); 
    application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
} 
} 

cevap

0

JFrame'iniz için bir LayoutManager oluşturun ve tüm bileşenleri ekleyin ve ardından Çerçeveyi görünür hale getirin.

public void createUserInterface(){ 
    JFrame f = new JFrame("Guitar Tuner"); 

    f.addWindowListener(new WindowAdapter(){ 
     public void windowClosing(WindowEvent e){ 
      System.exit(0); 
     } 
    }); 

    f.add(new LoadImageApp()); 
    //set LayoutManager 
    f.setLayout(new FlowLayout()); 

    guitarJLabel = new JLabel(); 
    guitarJLabel.setBounds(16, 16, 90, 21); 
    guitarJLabel.setText("Guitar"); 
    f.add(guitarJLabel); 

    EButton = new JButton(); 
    EButton.setBounds(20, 20, 50, 50); 
    EButton.setText("E"); 
    f.add(EButton); 

    AButton = new JButton(); 
    AButton.setBounds(40, 40, 50, 50); 
    AButton.setText("A"); 
    f.add(AButton); 

    DButton = new JButton(); 
    DButton.setBounds(60, 60, 50, 50); 
    DButton.setText("D"); 
    f.add(DButton); 

    GButton = new JButton(); 
    GButton.setBounds(20, 100, 50, 50); 
    GButton.setText("G"); 
    f.add(GButton); 

    BButton = new JButton(); 
    BButton.setBounds(40, 100, 50, 50); 
    BButton.setText("B"); 
    f.add(BButton); 

    eButton = new JButton(); 
    eButton.setBounds(60, 100, 50, 50); 
    eButton.setText("e"); 
    f.add(eButton); 

    f.pack(); 
    f.setSize (500, 500); 
    f.setTitle("Aaron's Awesome Guitar Tuner"); 
    f.setVisible(true); 
} 

FlowLayout'u ayarladım, gereksiniminize bağlı olarak herhangi bir Düzen ekleyebilirsiniz.

+0

Bu işe yaradı! Çok teşekkür ederim! – Staver

0

ben kodunda birkaç şey anlamadı:

  1. Eğer iki çerçeve (GuitarTuner ve Çerçeve f)
  2. bu sadece bir arka plan resmi çizmek mi sınıf LoadImageApp (amacı nedir kullanıyorsunuz Neden çerçeve için)

sadece bir Düzen Yöneticisi ve İçerik bölmesini ayarlayarak aşağıda daha yapabilirsiniz çerçeve arka planda bir görüntü çizmek istiyorsanız (I) tek bir kareyi kullanarak bunu yapmış:?

 package guitartuner; 

    import java.awt.BorderLayout; 
    import java.awt.FlowLayout; 
    import java.awt.event.WindowAdapter; 
    import java.awt.event.WindowEvent; 

    import javax.swing.ImageIcon; 
    import javax.swing.JButton; 
    import javax.swing.JFrame; 
    import javax.swing.JLabel; 


    public class GuitarTuner extends JFrame { 

     private JLabel guitarJLabel; 
     private JButton EButton, AButton; 

     public GuitarTuner() { 
      createUserInterface(); 
     } 

     public void createUserInterface() { 

      addWindowListener(new WindowAdapter() { 
       public void windowClosing(final WindowEvent e) { 
        System.exit(0); 
       } 
      }); 

      setLayout(new BorderLayout()); 
      setContentPane(new JLabel(new ImageIcon("headstock.jpg"))); 
      setLayout(new FlowLayout()); 

      this.guitarJLabel = new JLabel(); 
      this.guitarJLabel.setBounds(16, 16, 90, 21); 
      this.guitarJLabel.setText("Guitar"); 
      add(this.guitarJLabel); 

      this.EButton = new JButton(); 
      this.EButton.setBounds(20, 20, 50, 50); 
      this.EButton.setText("E"); 
      add(this.EButton); 

      this.AButton = new JButton(); 
      this.AButton.setBounds(40, 40, 50, 50); 
      this.AButton.setText("A"); 
      add(this.AButton); 

      setTitle("Aaron's Awesome Guitar Tuner"); 
      setSize(500, 500); 
      setVisible(true); 
     } 

     public static void main(final String[] args) { 
      GuitarTuner application = new GuitarTuner(); 
      application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     } 
    }