Java 为什么赢了';我的图像是否显示在我的布局中?
我正在用Java做这个项目,需要一个图像来显示,还有一个个人简历,还有一个播放歌曲/声音的按钮。我完成了按钮和bio,但我可以想出如何让图像显示在布局的北部以及中间部分的按钮,任何帮助都将是巨大的 这是我的错误:类型容器中的add(String,Component)方法不适用于参数(Image,String) } 编辑:Java 为什么赢了';我的图像是否显示在我的布局中?,java,image,applet,Java,Image,Applet,我正在用Java做这个项目,需要一个图像来显示,还有一个个人简历,还有一个播放歌曲/声音的按钮。我完成了按钮和bio,但我可以想出如何让图像显示在布局的北部以及中间部分的按钮,任何帮助都将是巨大的 这是我的错误:类型容器中的add(String,Component)方法不适用于参数(Image,String) } 编辑: 我猜您使用的是JApplet,因为您使用的是Swing组件。试着这样做: public class ImageApplet extends JApplet { pri
我猜您使用的是
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组件。是的,对不起,我添加了一些注释,您需要使用一个组件,例如包含图像的JLabel
。您无法转换,您将图像添加到JLabel
,可能名称混淆,我会重新命名它们…我把你的应用程序和桌面应用程序混淆了,而不是小程序。。。我再查一遍(对不起)
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;
}
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);
}
}