[英]Java how to add image as background in layout
我有一個使用CardLayout的類,它有兩張卡片,上面有一個按鈕。 我希望能夠做的是放置一個像背景一樣的圖像,例如Windows中的桌面背景。 該程序最終將有幾張不同的卡片,我希望能夠在每張卡片上放置不同的背景。 我已嘗試過在本網站上提出的其他類似問題中提出的許多建議,以及通過Google搜索我能找到的任何建議,但我似乎無法讓它發揮作用。 我知道使用CardLayout我不能將面板放在面板上,因此將圖像放在JPanel上是行不通的。 所以我的問題是,根據下面發布的代碼,我有更好的方法來設置我的布局,以便它更好地工作,而且,我應該如何處理顯示圖像,使其在按鈕的背景中? 任何幫助將不勝感激。
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Cards implements ActionListener {
private JPanel cards;
private JButton button1;
private JButton button2;
private Image backgroundImage;
public void addComponentToPane(Container pane) throws IOException {
backgroundImage = ImageIO.read(new File("background.jpg"));
//create cards
JPanel card1 = new JPanel()
{
@Override
public void paintComponent(Graphics g)
{
g.drawImage(backgroundImage, 0, 0, null);
}
};
JPanel card2 = new JPanel();
button1 = new JButton("Button 1");
button2 = new JButton("Button 2");
button1.addActionListener(this);
button2.addActionListener(this);
card1.add(button1);
card2.add(button2);
//create panel that contains cards
cards = new JPanel(new CardLayout());
cards.add(card1, "Card 1");
cards.add(card2, "Card 2");
pane.add(cards, BorderLayout.SOUTH);
}
public void itemStateChanged(ItemEvent evt) {
CardLayout cl = (CardLayout)(cards.getLayout());
cl.show(cards, (String)evt.getItem());
}
public static void createAndShowGUI() throws IOException {
//create and setup window
JFrame frame = new JFrame("Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//create and setup content pane
Cards main = new Cards();
main.addComponentToPane(frame.getContentPane());
//display window
frame.pack();
frame.setSize(800, 600);
frame.setResizable(false);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == button1) {
CardLayout cl = (CardLayout) (cards.getLayout());
cl.show(cards, "Card 2");
} else if (ae.getSource() == button2) {
CardLayout cl = (CardLayout) (cards.getLayout());
cl.show(cards, "Card 1");
}
}
public static void main(String[] args) {
//set look and feel
try {
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
} catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
//schedule job for the event dispatch thread creating and showing GUI
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
createAndShowGUI();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
@ Eng.Fouad的答案很好,但圖像可能在布局中占主導地位。 您可能希望覆蓋getPreferredSize()
,如下所示。 然后你可以pack()
窗口,大小將是正確的。 另請參閱使用getResource加載圖像 。
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Cards implements ActionListener {
private JPanel cards;
private JButton button1;
private JButton button2;
private Image backgroundImage;
public void addComponentToPane(Container pane) {
try {
backgroundImage = ImageIO.read(new File("background.jpg"));
} catch (IOException ex) {
ex.printStackTrace(System.err);
}
//create cards
JPanel card1 = new JPanel() {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(backgroundImage, 0, 0, null);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(
backgroundImage.getWidth(null),
backgroundImage.getHeight(null));
}
};
JPanel card2 = new JPanel();
button1 = new JButton("Button 1");
button2 = new JButton("Button 2");
button1.addActionListener(this);
button2.addActionListener(this);
card1.add(button1);
card2.add(button2);
//create panel that contains cards
cards = new JPanel(new CardLayout());
cards.add(card1, "Card 1");
cards.add(card2, "Card 2");
pane.add(cards, BorderLayout.SOUTH);
}
public void itemStateChanged(ItemEvent evt) {
CardLayout cl = (CardLayout) (cards.getLayout());
cl.show(cards, (String) evt.getItem());
}
public static void createAndShowGUI() {
//create and setup window
JFrame frame = new JFrame("Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//create and setup content pane
Cards main = new Cards();
main.addComponentToPane(frame.getContentPane());
//display window
frame.pack();
frame.setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == button1) {
CardLayout cl = (CardLayout) (cards.getLayout());
cl.show(cards, "Card 2");
} else if (ae.getSource() == button2) {
CardLayout cl = (CardLayout) (cards.getLayout());
cl.show(cards, "Card 1");
}
}
public static void main(String[] args) {
//schedule job for the event dispatch thread creating and showing GUI
EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.