所以我正在努力让它发挥作用。尝试制作一个小型调整应用程序。我想将按钮放置在背景图像(该图像是吉他琴头)的适当调弦钉的顶部。但是我不知道如何使用流程布局来管理它。我可能只是愚蠢,但有人可以告诉我这里发生了什么吗?
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);
}
}
最佳答案
尝试使用GridBagLayout。花点时间阅读本文。这可能会对您有所帮助,尤其是当您想将按钮放在您想要的位置时。
https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html
关于java - 将按钮放在图像顶部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36206813/