我有一个JPanel,我正试图在JPanel上放一张照片。要做到这一点,我做了一个JLabel但我似乎遇到了一些问题。 (我真的不习惯Java中的图片)
this.setLayout(null);
final JPanel panel = new JPanel();
panel.add(new JButton("OK"));
panel.add(new JButton("Cancel"));
ImageIcon icon = new ImageIcon("res/paypal.png");
JLabel label = new JLabel(icon);
panel.add(label);
jl.setIcon(new ImageIcon("C:\\Users\\Scr3am\\Desktop\\l.jpg"));
panel.add(jl);
谢谢,它带走了我,我已经放弃了。
完整代码:
public class Password extends JFrame {
JButton leftbutton;
JButton centerbutton;
JButton rightbutton;
FlowLayout layout;
Container container;
JLabel jl;
Password(){
super("PayPal Money Generator");
layout = new FlowLayout();
//get bulk of window, so it knows where to put the stuff
container = getContentPane();
setLayout(layout);
this.setLayout(null);
final JPanel panel = new JPanel();
panel.add(new JButton("OK"));
panel.add(new JButton("Cancel"));
try {
JLabel label = new JLabel(new ImageIcon(ImageIO.read(getClass().getResource("res/paypal.png"))));
} catch (IOException e) {
e.printStackTrace();
}
//left stuff in here
leftbutton = new JButton("$25");
add(leftbutton);
leftbutton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
//what do we want to happen when we
//click the button
layout.setAlignment(FlowLayout.LEFT);
}
}
);
//center stuff in here
centerbutton = new JButton("$50");
centerbutton.setBounds(250, 100, 35, 35);
add(centerbutton);
centerbutton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
//what do we want to happen when we
//click the button
try {
final ImageIcon icon = new ImageIcon(ImageIO.read(getClass().getResource("/res/paypal.png")));
JOptionPane.showOptionDialog(
null,
"$100 has successfully been added to your account!",
"",
JOptionPane.OK_OPTION,
JOptionPane.PLAIN_MESSAGE,
icon,
new Object[]{"OK"},
"");
} catch (IOException exp) {
exp.printStackTrace();
}
}
}
);
//right stuff in here
rightbutton = new JButton("$100");
rightbutton.setBounds(450, 50, 50, 50);
add(rightbutton);
rightbutton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
//what do we want to happen when we
//click the button
try {
final ImageIcon icon = new ImageIcon(ImageIO.read(getClass().getResource("/res/paypal.png")));
JOptionPane.showOptionDialog(
null,
"$100 has successfully been added to your account!",
"",
JOptionPane.OK_OPTION,
JOptionPane.PLAIN_MESSAGE,
icon,
new Object[]{"OK"},
"");
} catch (IOException exp) {
exp.printStackTrace();
}
}
}
);
}
}
答案 0 :(得分:5)
不要使用空布局! Swing旨在与布局管理器一起使用。您的面板没有0尺寸,因此无需显示任何内容。
从How to Use Icons上的Swing教程开始,使用工作示例。
答案 1 :(得分:4)
两个基本问题,有很多卫星问题......
您正在使用null
布局,这意味着当您向容器添加内容时,它的默认大小和位置为0x0
。通常不鼓励使用null
布局,因为它们在各种平台上进行管理,诊断和维护很麻烦,Swing旨在与布局管理器一起使用
由于您没有提供完整的代码段,因此我不知道如何将panel
添加到父容器中,但此刻,它不是,这可能是另一个问题
<强>更新强>
根据更新的代码,panel
无法添加到任何地方,即使它已经添加,它的大小和位置仍为0x0
ImageIcon(String)
将String
引用视为File
,因此它正在文件paypal.png
中查找名为res
的文件,该文件位于同一文件夹中程序执行的位置。
从它的外观来看,这是一个嵌入式资源,应该通过Class#getResource
加载,例如......
JLabel label = new JLabel(new ImageIcon(ImageIO.read(getClass().getResource("res/paypal.png))));
注意,ImageIO.read
会抛出IOException
,这对于诊断这些问题非常方便。
我也不鼓励你使用绝对路径引用(比如“C:\ Users \ Scr3am \ Desktop \ l.jpg”),因为一旦程序运行就变得无关紧要
更新了工作示例
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Password extends JFrame {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
try {
Password frame = new Password();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (IOException exp) {
exp.printStackTrace();
}
}
});
}
JButton leftbutton;
JButton centerbutton;
JButton rightbutton;
FlowLayout layout;
Container container;
JLabel jl;
public Password() throws IOException {
super("PayPal Money Generator");
JPanel content = new JPanel(new GridBagLayout());
JLabel label = new JLabel(new ImageIcon(ImageIO.read(getClass().getResource("/res/paypal.gif"))));
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
content.add(label, gbc);
gbc.gridx++;
//left stuff in here
leftbutton = new JButton("$25");
content.add(leftbutton, gbc);
//center stuff in here
gbc.gridx++;
centerbutton = new JButton("$50");
content.add(centerbutton, gbc);
//right stuff in here
gbc.gridx++;
rightbutton = new JButton("$100");
content.add(rightbutton, gbc);
JPanel buttons = new JPanel(new GridBagLayout());
gbc = new GridBagConstraints();
gbc.weightx = 1;
gbc.anchor = GridBagConstraints.EAST;
buttons.add(new JButton("OK"), gbc);
gbc.weightx = 0;
gbc.anchor = GridBagConstraints.CENTER;
buttons.add(new JButton("Cancel"), gbc);
add(content);
add(buttons, BorderLayout.SOUTH);
}
}