gpt4 book ai didi

java - 使背景图像通过顶部的面板显示

转载 作者:行者123 更新时间:2023-12-02 02:52:57 26 4
gpt4 key购买 nike

当我在面板中添加面板和对象时,我希望将图像设置为背景并通过放在其顶部的面板显示。我读到您可以通过设置 panel.setOpaque(false) 选项来进行此投注,但这似乎不起作用。

这是我的编译器目录及其设置方式(使用 Intellij)...

enter image description here

这是背景图片...

enter image description here

这是代码...

BackgroundPanel 类 > http://www.camick.com/java/source/BackgroundPanel.java

/**
* Created by James Page on 4/16/2017.
*/
package Main;

import org.apache.commons.lang3.ArrayUtils;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;

public class TestGui extends JFrame {
private final String[] guiCharSelDefault = {"--- Select Character ---"};
private final String[] characters = {"charOne", "charTwo", "charThree", "charFour"};
private final String[] GuiCharSel = (String[]) ArrayUtils.addAll(guiCharSelDefault, characters);
private final String[] weapon = {"Weapon"};
private final String[][] allWeapons = {
{
"weakWeaponOne", "strongWeaponOne", "shortWeaponOne", "longWeaponOne"
},
{
"weakWeaponTwo", "strongWeaponTwo", "shortWeaponTwo", "longWeaponTwo"
},
{
"weakWeaponThree", "strongWeaponThree", "shortWeaponThree", "longWeaponThree"
},
{
"weakWeaponFour", "strongWeaponFour", "shortWeaponFour", "longWeaponFour"
}
};
private JComboBox charCombo = new JComboBox(GuiCharSel);
private JComboBox weaponsCombo = new JComboBox(weapon);
private BackgroundPanel backgroundFrame = createBackgroundFrame("../images/Background.png");
private JPanel topFrame = createTopFrame();
private JPanel centerFrame = createCenterFrame();

//**************************************************************************************

private static GridBagConstraints setGbc(int gridx, int gridy, int gridWidth, int gridHeight, int ipadx, int ipady, String anchorLocation, double weightx, double weighty, Insets insets){
GridBagConstraints gbc = new GridBagConstraints();

if (anchorLocation.toUpperCase().equals("NORTHWEST")){
gbc.anchor = GridBagConstraints.NORTHWEST;
} else if (anchorLocation.toUpperCase().equals("NORTH")){
gbc.anchor = GridBagConstraints.NORTH;
} else if (anchorLocation.toUpperCase().equals("NORTHEAST")){
gbc.anchor = GridBagConstraints.NORTHEAST;
} else if (anchorLocation.toUpperCase().equals("WEST")){
gbc.anchor = GridBagConstraints.WEST;
} else if (anchorLocation.toUpperCase().equals("EAST")){
gbc.anchor = GridBagConstraints.EAST;
} else if (anchorLocation.toUpperCase().equals("SOUTHWEST")){
gbc.anchor = GridBagConstraints.SOUTHWEST;
} else if (anchorLocation.toUpperCase().equals("SOUTH")){
gbc.anchor = GridBagConstraints.SOUTH;
} else if (anchorLocation.toUpperCase().equals("SOUTHEAST")){
gbc.anchor = GridBagConstraints.SOUTHEAST;
} else {
gbc.anchor = GridBagConstraints.CENTER;
}

gbc.gridx = gridx; // column
gbc.gridy = gridy; // row
gbc.gridwidth = gridWidth; // number of columns
gbc.gridheight = gridHeight; // number of rows
gbc.ipadx = ipadx; // width of object
gbc.ipady = ipady; // height of object
gbc.weightx = weightx; // shifts rows to side of set anchor
gbc.weighty = weighty; // shifts columns to side of set anchor
gbc.insets = insets; // placement inside cell
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.fill = GridBagConstraints.VERTICAL;

return gbc;
}

private Insets setInsets(int top, int left, int bottom, int right){
Insets insets = new Insets(top,left,bottom,right);
return insets;
}

//**************************************************************************************

private BackgroundPanel createBackgroundFrame(String imgLocName){
Image backgroundImg = null;
try {
backgroundImg = ImageIO.read(getClass().getResource(imgLocName));
System.out.println("File: " + imgLocName.toString());
} catch (Exception e) {
System.out.println("Cannot read file: " + e);
}
BackgroundPanel bgPanel = new BackgroundPanel(backgroundImg, BackgroundPanel.SCALED, 0.0f, 0.0f);
return bgPanel;
}

private JPanel createTopFrame(){
JPanel pnl = new JPanel();
Border raisedBevel = BorderFactory.createRaisedBevelBorder();
Color lineColor = new Color(224,224,224);
Border lineBorder = BorderFactory.createMatteBorder(5, 5, 5, 5, lineColor);
Border loweredBevel = BorderFactory.createLoweredBevelBorder();
Border compoundSetup = BorderFactory.createCompoundBorder(raisedBevel, lineBorder);
Border compoundFinal = BorderFactory.createCompoundBorder(compoundSetup, loweredBevel);
TitledBorder topFrameTitle = BorderFactory.createTitledBorder(compoundFinal, "Character");
topFrameTitle.setTitleJustification(TitledBorder.CENTER);

JLabel lineSplitter = new JLabel();
Border lineSplitterBoarder = BorderFactory.createMatteBorder(0, 0, 0, 5, new Color(224,224,224));
lineSplitter.setBorder(lineSplitterBoarder);

pnl.setBorder(topFrameTitle);
pnl.setLayout(new GridBagLayout());
pnl.setPreferredSize(new Dimension(200, 60));

charCombo.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
String charName = ((JComboBox)(e.getSource())).getSelectedItem().toString();
if (charName.equals("charOne")){
weaponsCombo.removeAllItems();
weaponsCombo.setModel(new DefaultComboBoxModel(allWeapons[1]));
}
}
}
);
pnl.add(charCombo, setGbc(0,0, 1,1, 0,0, "WEST", 0, 0, setInsets(0, 0, 0, 0)));
pnl.add(lineSplitter, setGbc(1,0, 1,1, 0,0, "WEST", 0, 0, setInsets(0, 10, 0, 0)));
pnl.add(weaponsCombo, setGbc(2,0, 1,1, 0,0, "WEST", 1, 1, setInsets(0, 10, 0, 0)));

pnl.setOpaque(false);
return pnl;
}

private JPanel createCenterFrame() {
JPanel pnl = new JPanel();
Border raisedBevel = BorderFactory.createRaisedBevelBorder();
Color lineColor = new Color(224, 224, 224);
Border lineBorder = BorderFactory.createMatteBorder(5, 5, 5, 5, lineColor);
Border loweredBevel = BorderFactory.createLoweredBevelBorder();
Border compoundSetup = BorderFactory.createCompoundBorder(raisedBevel, lineBorder);
Border compoundFinal = BorderFactory.createCompoundBorder(compoundSetup, loweredBevel);
TitledBorder topFrameTitle = BorderFactory.createTitledBorder(compoundFinal, "Stuff");
topFrameTitle.setTitleJustification(TitledBorder.CENTER);

pnl.setBorder(topFrameTitle);
pnl.setLayout(new GridBagLayout());

pnl.setOpaque(false);
return pnl;
}

TestGui(){
add(backgroundFrame);
add(topFrame, BorderLayout.NORTH);
add(centerFrame, BorderLayout.CENTER);

setSize(800,600);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

//**************************************************************************************

public static void main(String[] args) {
new TestGui();
}
}

我知道图像背景被添加到 gui 中,因为如果我注释掉行 //add(topFrame, BorderLayout.NORTH);//add(centerFrame, BorderLayout .CENTER); 在构造函数中,图像显示在 GUI 上。有人知道如何让背景图像通过这两个面板显示吗?

最佳答案

您要将 backgroundFrame 替换为 centerFrame

add(backgroundFrame);
add(topFrame, BorderLayout.NORTH);
add(centerFrame, BorderLayout.CENTER);

这是 BorderLayout 的功能,它在 5 个可用位置中的每个位置只能支持一个组件

相反,您想要做的是将其他面板添加到 backgroundFrame 或将 backgroundFrame 设置为框架的 contentPane,例如

setContentPane(backgroundFrame);
add(topFrame, BorderLayout.NORTH);
add(centerFrame, BorderLayout.CENTER);

Background

此外,作为一般建议,请避免使用 setPreferredSize,有太多事情可能会导致错误

Sorry to bother you again, it looks like it is not showing through the JScrollPane. Should I open this up as a new question?

JScrollPane是一个比较复杂的组件,它由两个组件组成,用于提供滚动的功能

JScrollPane

您需要使 JScrollPane 及其 JViewport 透明(以及您包装到其中的 View )

因此,这是 createCenterFrame 方法的修改版本,它创建一个 JTextArea,包装到 JScrollPane 中并将其添加到 pnl

private JPanel createCenterFrame() {
JPanel pnl = new JPanel();
Border raisedBevel = BorderFactory.createRaisedBevelBorder();
Color lineColor = new Color(224, 224, 224);
Border lineBorder = BorderFactory.createMatteBorder(5, 5, 5, 5, lineColor);
Border loweredBevel = BorderFactory.createLoweredBevelBorder();
Border compoundSetup = BorderFactory.createCompoundBorder(raisedBevel, lineBorder);
Border compoundFinal = BorderFactory.createCompoundBorder(compoundSetup, loweredBevel);
TitledBorder topFrameTitle = BorderFactory.createTitledBorder(compoundFinal, "Stuff");
topFrameTitle.setTitleJustification(TitledBorder.CENTER);

pnl.setBorder(topFrameTitle);
pnl.setLayout(new BorderLayout());

JScrollPane scrollPane = new JScrollPane();
scrollPane.setOpaque(false);
scrollPane.getViewport().setOpaque(false);

JTextArea ta = new JTextArea();
ta.setOpaque(false);
ta.setText("This is an example of a transparent text area inside a transparent scroll pane, but you could just as easily wrap a transparent panel into it and get the same result");
ta.setWrapStyleWord(true);
ta.setLineWrap(true);

scrollPane.setViewportView(ta);

pnl.add(scrollPane);

pnl.setOpaque(false);
return pnl;
}

Transparent scroll pane and text area

关于java - 使背景图像通过顶部的面板显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43531992/

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