所以我努力让这个工作起来。试图做一个小的调整应用程序。我想将按钮放在背景图像的适当调音栓的顶部(图像是吉他琴头)。但是,我无法弄清楚如何使用流布局来管理它。我可能只是愚蠢但是有人可以跟我说说这里发生什么事吗?
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 :(得分:0)
尝试使用GridBagLayout。花点时间阅读本文。如果您想将按钮放在所需的位置,这可能对您有所帮助。
https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html
答案 1 :(得分:0)
您还可以在createUserInterface() - Method
的开头添加这些行编辑:如果您应该这样做,还必须删除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);
...
答案 2 :(得分:0)
如果您希望组件相互重叠,则可以使用JLayeredPane。
请参阅:https://docs.oracle.com/javase/tutorial/uiswing/components/layeredpane.html
您可以将LoadImageApp放在一个图层中,将另一个JPanel放在FlowLayout中,其中包含您上面的图层中的按钮。
答案 3 :(得分:0)
LoadImageApp
,将其从JPanel
paintComponent
方法而不是paint
。FlowLayout
。以下是工作代码:
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);
}
}