gpt4 book ai didi

java - JPanel 背景图像不适用于其中的 JPanel

转载 作者:搜寻专家 更新时间:2023-11-01 01:41:52 29 4
gpt4 key购买 nike

我正在开发一个简单的注册窗口,它会在 Java 应用程序打开时出现。

这是一个 JFrame,里面有一个 JPanel,它有文本字段、标签,另一个面板也包含文本字段和标签。我的问题是外部面板有背景图像,但它不适用于内部面板,如下所示:

这是整个窗口代码:

public void start() {

try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
ex.printStackTrace();
}

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
frame = new JFrame("Chat");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setMaximumSize(new Dimension((int)screenSize.getWidth()-1000, (int)screenSize.getHeight()-1000));
frame.setMinimumSize(new Dimension((int)screenSize.getWidth()/2-200,(int) screenSize.getHeight()/2));
frame.setResizable(false);

welcome = new LoginPanel();
welcome.setLayout(new BoxLayout(welcome, BoxLayout.Y_AXIS));
welcome.setBorder(BorderFactory.createEmptyBorder(50, welcome.getWidth()/2-500, 50, welcome.getWidth()/2 -500));

//repaintThread = new Thread(new RepaintRunnable(frame, welcome));
//repaintThread.start();


request = new JLabel("Please fill the required fields below:");
request.setFont(new Font("Serif", Font.BOLD, 20));
request.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0));
request.setAlignmentX(Component.CENTER_ALIGNMENT);

userInfo = new JPanel();
userInfo.setLayout(new BoxLayout(userInfo, BoxLayout.Y_AXIS));
userInfo.setAlignmentX(Component.CENTER_ALIGNMENT);

Font fieldType = new Font("Serif", Font.PLAIN, 15);

PlainDocument doc = new PlainDocument();
doc.setDocumentFilter(new NameDocument());

enterFirstName = new JLabel("First name:");
enterFirstName.setAlignmentX(Component.LEFT_ALIGNMENT);
enterFirstName.setFont(fieldType);
firstName = new JTextField(20);
firstName.setMaximumSize(firstName.getPreferredSize());
firstName.setDocument(NameDocument.getNewNameDocument(NameDocument.NO_SPACE));
firstName.getDocument().addDocumentListener(new ChangeDocumentListener());
firstName.addActionListener(new ConfirmListener());
firstName.setAlignmentX(Component.LEFT_ALIGNMENT);

enterSecName= new JLabel("Surname:");
enterSecName.setAlignmentX(Component.LEFT_ALIGNMENT);
enterSecName.setFont(fieldType);
secName = new JTextField(30);
secName.setMaximumSize(secName.getPreferredSize());
secName.setDocument(NameDocument.getNewNameDocument(NameDocument.HAS_SPACE));
secName.getDocument().addDocumentListener(new ChangeDocumentListener());
secName.addActionListener(new ConfirmListener());
secName.setAlignmentX(Component.LEFT_ALIGNMENT);

enterNickname = new JLabel("Nickname (how other people will see you in chat):");
enterNickname.setAlignmentX(Component.LEFT_ALIGNMENT);
enterNickname.setFont(fieldType);
nickname = new JTextField(30);
nickname.setMaximumSize(nickname.getPreferredSize());
nickname.setDocument(NameDocument.getNewNameDocument(NameDocument.NO_SPACE));
nickname.addActionListener(new ConfirmListener());
nickname.setAlignmentX(Component.LEFT_ALIGNMENT);

userInfo.add(enterFirstName);
userInfo.add(firstName);
userInfo.add(enterSecName);
userInfo.add(secName);
userInfo.add(enterNickname);
userInfo.add(nickname);

confirm = new JButton("Submit");
confirm.setAlignmentX(Component.CENTER_ALIGNMENT);
confirm.setEnabled(false);
confirm.addActionListener(new ConfirmListener());

welcome.add(request);
welcome.add(userInfo);
welcome.add(new Box.Filler(new Dimension(10, 10), new Dimension(10, 10), new Dimension(10, 10)));
welcome.add(confirm);

frame.getContentPane().add(BorderLayout.CENTER, welcome);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

这是 LoginPanel 代码(外部 JPanel):

public class LoginPanel extends JPanel {
public void paintComponent(Graphics g) {
try {
super.paintComponent(g);
BufferedImage background = ImageIO.read(new File("Background.jpg"));
g.drawImage(background, 0, 0, getWidth(), getHeight(), null);
} catch(Exception ex) {
ex.printStackTrace();
}
}
}

因为我是 Java 的初学者,如果有人能给我建议如何使这段代码更好,那就太好了。

最佳答案

请记住在任何覆盖显示图像的 JPanel 的内部 JPanel(以及一些其他组件——尽管不是全部)上调用 setOpaque(false);。这将允许背景图像显示出来。您不必对 JLabel 执行此操作,因为默认情况下它们是透明的(非不透明的),但您可以对 JPanel 执行此操作。

所以对你来说它将是:

userInfo = new JPanel();
userInfo.setOpaque(false);

另一个问题是您不应该永远从 paintComponent 方法中读取图像。这个方法可能会经常被调用,为什么在图像可以而且应该只读一次的时候重新读取图像。更重要的是,这种方法应该尽可能快,因为不必要地减慢它会减慢程序的感知响应速度。一次读取图像,并将其存储在一个变量中,然后在 paintComponent 中显示该变量。

例如,

public class LoginPanel extends JPanel {
private BufferedImage background;

public LoginPanel(BufferedImage background) {
this.background = background;
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
if (background != null) {
g.drawImage(background, 0, 0, getWidth(), getHeight(), this);
}
}
}

读入图像,然后将其传递给 LoginPanel 类的构造函数。

关于java - JPanel 背景图像不适用于其中的 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32936942/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com