Java 带背景图片的卡片布局
Java Card Layout with Background Image
我正在为一个项目创建航班预订系统,但在使用该应用程序的 GUI 时遇到了问题。我正在使用 CardLayout 来管理该程序的多张卡片。
我想在登录卡中添加背景图片,但输入字段显示在图片下方。
程序的代码是
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import javax.imageio.*;
import java.net.*;
public class CardPanel {
public static void main(String[] args) {
try {
CardLayout cardLayout = null;
JFrame frame = new JFrame("Welcome");
JPanel contentPane = new JPanel(cardLayout);
URL url = new URL("https://i.stack.imgur.com/P59NF.png");
BufferedImage img = ImageIO.read(url);
ImageIcon imageIcon = new ImageIcon(img);
JLabel logo = new JLabel(imageIcon);
JPanel buttonsPanel = new JPanel();
JButton login = new JButton("Login");
buttonsPanel.add(login);
contentPane.setLayout(new BorderLayout(10, 15));
contentPane.add(logo, BorderLayout.NORTH);
contentPane.add(buttonsPanel, BorderLayout.SOUTH);
frame.add(contentPane, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.pack();
frame.setVisible(true);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
还附上了该应用程序的屏幕截图 (http://i.imgur.com/PkjblPu.png)。
我希望按钮位于背景图片上方。
测试表明卡片布局不能用于显示BG图像。似乎在内部它在交换组件时删除了一张卡并添加了另一张卡。使用自定义绘制的 JPanel
绘制 BG 图像。
这是证据。
那个红色是卡片布局的面板,按钮面板设置为透明。
import java.awt.*;
import javax.swing.*;
import java.net.URL;
public class CardPanel {
public static void main(String[] args) throws Exception {
CardLayout cardLayout = new CardLayout();
JFrame frame = new JFrame("Welcome");
JPanel contentPane = new JPanel(cardLayout);
contentPane.setBackground(Color.RED);
ImageIcon imageIcon = new ImageIcon(new URL("https://i.stack.imgur.com/OVOg3.jpg"));
JLabel logo = new JLabel(imageIcon);
JPanel buttonsPanel = new JPanel();
JButton login = new JButton("Login");
buttonsPanel.add(login);
buttonsPanel.setOpaque(false);
contentPane.add(logo, "logo");
contentPane.add(buttonsPanel, "button");
cardLayout.show(contentPane, "button");
frame.add(contentPane, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
我正在为一个项目创建航班预订系统,但在使用该应用程序的 GUI 时遇到了问题。我正在使用 CardLayout 来管理该程序的多张卡片。
我想在登录卡中添加背景图片,但输入字段显示在图片下方。
程序的代码是
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import javax.imageio.*;
import java.net.*;
public class CardPanel {
public static void main(String[] args) {
try {
CardLayout cardLayout = null;
JFrame frame = new JFrame("Welcome");
JPanel contentPane = new JPanel(cardLayout);
URL url = new URL("https://i.stack.imgur.com/P59NF.png");
BufferedImage img = ImageIO.read(url);
ImageIcon imageIcon = new ImageIcon(img);
JLabel logo = new JLabel(imageIcon);
JPanel buttonsPanel = new JPanel();
JButton login = new JButton("Login");
buttonsPanel.add(login);
contentPane.setLayout(new BorderLayout(10, 15));
contentPane.add(logo, BorderLayout.NORTH);
contentPane.add(buttonsPanel, BorderLayout.SOUTH);
frame.add(contentPane, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.pack();
frame.setVisible(true);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
还附上了该应用程序的屏幕截图 (http://i.imgur.com/PkjblPu.png)。
我希望按钮位于背景图片上方。
测试表明卡片布局不能用于显示BG图像。似乎在内部它在交换组件时删除了一张卡并添加了另一张卡。使用自定义绘制的 JPanel
绘制 BG 图像。
这是证据。
那个红色是卡片布局的面板,按钮面板设置为透明。
import java.awt.*;
import javax.swing.*;
import java.net.URL;
public class CardPanel {
public static void main(String[] args) throws Exception {
CardLayout cardLayout = new CardLayout();
JFrame frame = new JFrame("Welcome");
JPanel contentPane = new JPanel(cardLayout);
contentPane.setBackground(Color.RED);
ImageIcon imageIcon = new ImageIcon(new URL("https://i.stack.imgur.com/OVOg3.jpg"));
JLabel logo = new JLabel(imageIcon);
JPanel buttonsPanel = new JPanel();
JButton login = new JButton("Login");
buttonsPanel.add(login);
buttonsPanel.setOpaque(false);
contentPane.add(logo, "logo");
contentPane.add(buttonsPanel, "button");
cardLayout.show(contentPane, "button");
frame.add(contentPane, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}