为什么我的图像不显示在我的布局中?
Why won't my image display in my layout?
我在 Java 从事这个项目,需要显示图像、简历和播放 song/sound 的按钮。我完成了按钮和简介,但我可以弄清楚如何让图像显示在布局的北部部分以及中心部分的按钮,任何帮助都会很棒。
这是我的错误:类型 Container 中的方法 add(String, Component) 不适用于参数 (Image, String)
public void init() {
// image
myPicture = getImage(getCodeBase(), "sample.jpg");
// THIS IS WHERE MY PROBLEM IS vvvvvvv
add(myPicture, BorderLayout.NORTH);
add(bio);
paneSouth.add(play);
getContentPane().add(paneSouth, BorderLayout.SOUTH);
mySound = getAudioClip(getDocumentBase(), "sample.wav");
play.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
mySound.play();
}});
}
public void paint(Graphics g){
g.drawImage(myPicture, 10, 10, this);
}
private JPanel paneSouth = new JPanel();
private TextArea bio = new TextArea("bio thing");
private JButton play = new JButton("Play");
private AudioClip mySound;
private Image myPicture;
}
编辑:
public void init() {
// image
ImageIcon icon = new ImageIcon(myPicture);
JLabel myLabelImage = new Image(icon);
add(myLabelImage, BorderLayout.NORTH);
// bio
add(bio);
add(bio, BorderLayout.CENTER);
// sound
paneSouth.add(play);
getContentPane().add(paneSouth, BorderLayout.SOUTH);
mySound = getAudioClip(getDocumentBase(), "sample.wav");
play.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
mySound.play();
}});
}
private JPanel paneSouth = new JPanel();
private TextArea bio = new TextArea("bio.");
private JButton play = new JButton("Play");
private AudioClip mySound;
private Image myPicture;
}
我猜你正在使用 JApplet
因为你正在使用 Swing 组件。尝试这样的事情:
public class ImageApplet extends JApplet {
private JPanel paneSouth = new JPanel();
private JTextArea bio = new JTextArea("bio thing");
private JButton play = new JButton("Play");
private Image myPicture;
private ImageIcon icon;
private JLabel label;
public void init() {
try {
URL pic = new URL(getDocumentBase(), "sample.jpg");
myPicture = ImageIO.read(pic);
icon = new ImageIcon(myPicture);
label = new JLabel(icon);
} catch (Exception e) {
e.printStackTrace();
}
// add image
add(label, BorderLayout.NORTH);
// bio
add(bio, BorderLayout.CENTER);
// sound
paneSouth.add(play);
add(paneSouth, BorderLayout.SOUTH);
// here add your sound declaration and button event...
}
@Override
public void paint(Graphics g) {
super.paint(g);
}
}
并尝试将您的 TextArea
更改为 JTextArea
,因为您只使用了 swing 组件。
我在 Java 从事这个项目,需要显示图像、简历和播放 song/sound 的按钮。我完成了按钮和简介,但我可以弄清楚如何让图像显示在布局的北部部分以及中心部分的按钮,任何帮助都会很棒。
这是我的错误:类型 Container 中的方法 add(String, Component) 不适用于参数 (Image, String)
public void init() {
// image
myPicture = getImage(getCodeBase(), "sample.jpg");
// THIS IS WHERE MY PROBLEM IS vvvvvvv
add(myPicture, BorderLayout.NORTH);
add(bio);
paneSouth.add(play);
getContentPane().add(paneSouth, BorderLayout.SOUTH);
mySound = getAudioClip(getDocumentBase(), "sample.wav");
play.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
mySound.play();
}});
}
public void paint(Graphics g){
g.drawImage(myPicture, 10, 10, this);
}
private JPanel paneSouth = new JPanel();
private TextArea bio = new TextArea("bio thing");
private JButton play = new JButton("Play");
private AudioClip mySound;
private Image myPicture;
}
编辑:
public void init() {
// image
ImageIcon icon = new ImageIcon(myPicture);
JLabel myLabelImage = new Image(icon);
add(myLabelImage, BorderLayout.NORTH);
// bio
add(bio);
add(bio, BorderLayout.CENTER);
// sound
paneSouth.add(play);
getContentPane().add(paneSouth, BorderLayout.SOUTH);
mySound = getAudioClip(getDocumentBase(), "sample.wav");
play.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
mySound.play();
}});
}
private JPanel paneSouth = new JPanel();
private TextArea bio = new TextArea("bio.");
private JButton play = new JButton("Play");
private AudioClip mySound;
private Image myPicture;
}
我猜你正在使用 JApplet
因为你正在使用 Swing 组件。尝试这样的事情:
public class ImageApplet extends JApplet {
private JPanel paneSouth = new JPanel();
private JTextArea bio = new JTextArea("bio thing");
private JButton play = new JButton("Play");
private Image myPicture;
private ImageIcon icon;
private JLabel label;
public void init() {
try {
URL pic = new URL(getDocumentBase(), "sample.jpg");
myPicture = ImageIO.read(pic);
icon = new ImageIcon(myPicture);
label = new JLabel(icon);
} catch (Exception e) {
e.printStackTrace();
}
// add image
add(label, BorderLayout.NORTH);
// bio
add(bio, BorderLayout.CENTER);
// sound
paneSouth.add(play);
add(paneSouth, BorderLayout.SOUTH);
// here add your sound declaration and button event...
}
@Override
public void paint(Graphics g) {
super.paint(g);
}
}
并尝试将您的 TextArea
更改为 JTextArea
,因为您只使用了 swing 组件。