我正在尝试制作一个基本的吉他调音器应用程序,只需在单击按钮时播放相应的音频文件。我正在尝试将吉他的主轴箱图像放入JFrame中,但是在理解Oracle文档时遇到了一些麻烦。有人可以向我解释为什么我的图像不会在此代码中产生吗?
P.S。我知道按钮的绑定位置没有设置在任何特定的地方,我知道没有音频代码集。我想先将按钮放在主轴箱图像上的相应调音栓上。
提前感谢您提供的任何提示或信息!
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);
}
}
答案 0 :(得分:0)
为JFrame.Create设置LayoutManager并添加所有组件,然后使Frame可见。
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,您可以根据需要添加任何布局。
答案 1 :(得分:0)
我对代码中的一些内容并不了解:
如果您只想在框架背景中绘制图像,则可以通过设置布局管理器和内容窗格(我使用单个框架完成)来执行以下操作:
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);
}
}