2016-03-24 8 views
1

私はこれを動作させるのに苦労しています。小さなチューニングアプリケーションを作成しようとしています。私はバックグラウンド画像の適切なチューニングペグの上にボタンを配置したい(画像はギターヘッドストックのものです)。しかし、私はフローレイアウトでそれを管理する方法を理解できません。私はちょうど愚かかもしれないが、誰かがここで何が起こって私を話すことができますか?画像の上にボタンを配置しますか?

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("/Users/Yojimbo/NetBeansProjects/guitarTuner/src/guitartuner/headstock.jpg")); 
     } catch (IOException e){  
     } 
    } 
    public Dimension getPreferredSize() { 
     if (img == null) { 
      return new Dimension(500,500); 
     } 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.setLayout(new FlowLayout()); 

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

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

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

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

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

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

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

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

SOあなたのコードをあなたの話をするためにここではありません。 –

+0

私は文字通り意味しなかった...私はなぜ私のボタンが私のイメージの上に置かれることを拒否しているのですか?しかし、あなたが失礼で無駄になりたいと思っていたら、あなたの時間と私の両方が先に進む... – Staver

答えて

0

また)(あなたのcreateUserInterfaceの先頭に以下の行を追加することができます - 方法

編集:あなたのLoadImageAppのあなたはまたf.addを削除する必要が(...を)このようにすればいいのです。

public void createUserInterface() throws IOException { 
    BufferedImage img = ImageIO.read(new File("/Users/Yojimbo/NetBeansProjects/guitarTuner/src/guitartuner/headstock.jpg")); 
    JFrame f = new JFrame("Guitar Tuner"); 
    JPanel contentPane = new JPanel(){ 
     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      g.drawImage(img, 0, 0, getWidth(), getHeight(), this); 
     } 
    }; 
    f.setContentPane(contentPane); 
... 
0
  1. いけないが、コンポーネントからLoadImageAppを拡張JPanel
  2. オーバーライドpaintComponent方法ないpaintから延びています。
  3. 新しい画像パネルのレイアウトをコンストラクタからFlowLayoutに設定します。
  4. フレームレイアウトを設定しないでください。イメージパネルを追加するだけです。すべてのボタンをイメージパネルに直接追加します。ここで

作業コードです:

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 JPanel { 
     BufferedImage img; 

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

     public LoadImageApp() { 
      super(new FlowLayout()); 
      try { 
       img = ImageIO 
         .read(new File("/Users/Yojimbo/NetBeansProjects/guitarTuner/src/guitartuner/headstock.jpg")); 
      } catch (IOException e) { 
      } 
     } 

     public Dimension getPreferredSize() { 
      if (img == null) { 
       return new Dimension(500, 500); 
      } 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); 
      } 
     }); 

     LoadImageApp panel = new LoadImageApp(); 
     f.add(panel); 

     EButton = new JButton(); 
     EButton.setBounds(0, 0, 50, 10); 
     EButton.setText("E"); 
     panel.add(EButton); 

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

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

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

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

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

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

    public static void main(String[] args) { 
     GuitarTuner application = new GuitarTuner(); 
     application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 
} 
関連する問題