내가 디자인하고있는 게임의 배경 이미지를 만들고 싶지만 원하는 효과를 얻으려면 올바른 방법을 찾아 낼 수 없다. 텍스트 뒤에있는 배경 등을 원한다. 기본적으로 전체를 배경으로하고있다. JFrame/JPanel은 레이아웃의 한 섹션 만이 아닙니다 (예 : BorderLayout.Center). 어쨌든이 작업을 수행한다고 생각합니다. 그렇다면 투명하게 배경을 볼 수 있도록 배경을 투명하게 만드는 방법은 무엇입니까?배경 이미지 도움말, JPanel, JFrame, 뭐?
혼란 스러워요.하지만 저는 여기있는 누군가가 내가하려고하는 것을 이해하고 도움을 줄 수 있기를 바랍니다. 이것은 제 현재 코드입니다. 나는 배경을 가지고 놀았으므로 내가 어떻게 그것을 쓰는지 많이 읽지는 않는다.
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
public class GamePanel extends JPanel {
private JTextPane playertext;
private JTextField wealthstring, currentwealth;
public GamePanel() {
super();
setLayout(new BorderLayout());
setBackground(Game.getBackgroundColor());
Border raised = BorderFactory.createRaisedBevelBorder();
Border lowered = BorderFactory.createLoweredBevelBorder();
setBorder(BorderFactory.createCompoundBorder(new EmptyBorder(4, 4, 4, 4), (BorderFactory.createCompoundBorder(raised, lowered))));
add(northpanel(), BorderLayout.NORTH);
add(eastpanel(), BorderLayout.EAST);
}
private JLabel northpanel() {
Font northfont = new Font("Engravers MT", Font.BOLD, 12);
ImageIcon banner = new ImageIcon("images/banner.png", "North Background");
playertext = new JTextPane();
playertext.setFont(northfont);
playertext.setEditable(false);
playertext.setText("Player: \n" + Game.getName());
playertext.setBackground(Game.getBackgroundColor());
playertext.setBorder(new EmptyBorder(10,10,10,10));
wealthstring = new JTextField("Money: ");
wealthstring.setFont(northfont);
wealthstring.setEditable(false);
wealthstring.setHorizontalAlignment(wealthstring.RIGHT);
wealthstring.setBorder(null);
wealthstring.setBackground(Game.getBackgroundColor());
currentwealth = new JTextField();
currentwealth.setFont(northfont);
currentwealth.setEditable(false);
currentwealth.setHorizontalAlignment(wealthstring.RIGHT);
currentwealth.setBackground(Game.getBackgroundColor());
currentwealth.setBorder(null);
String wealthrounded = String.format("%.2f", Game.getMoney());
currentwealth.setText(wealthrounded);
JPanel wealthtext = new JPanel();
wealthtext.setLayout(new GridLayout(2, 1));
wealthtext.setBackground(Game.getBackgroundColor());
wealthtext.setBorder(new EmptyBorder(10,10,10,10));
wealthtext.add(wealthstring);
wealthtext.add(currentwealth);
JLabel northpanel = new JLabel();
northpanel.setLayout(new BorderLayout());
northpanel.setIcon(banner);
northpanel.add(new JSeparator(), BorderLayout.PAGE_END);
northpanel.add(playertext, BorderLayout.WEST);
northpanel.add(wealthtext, BorderLayout.EAST);
return northpanel;
}
private JPanel eastpanel() {
JButton tab1 = new JButton("Tab 1");
JButton tab2 = new JButton("Tab 2");
JButton tab3 = new JButton("Tab 3");
JPanel easttabs = new JPanel();
easttabs.setLayout(new GridLayout(1, 3));
easttabs.add(tab1);
easttabs.add(tab2);
easttabs.add(tab3);
JPanel eastpanels = new JPanel();
eastpanels.setLayout(new BorderLayout());
eastpanels.setBackground(Game.getBackgroundColor());
eastpanels.add(easttabs, BorderLayout.NORTH);
return eastpanels;
}
}
JLayeredPane을 사용해 보셨습니까? 기본 배경 대신 이미지를 표시하기 위해 JPanel의 paintComponent 메소드를 대체 할 수도 있습니다. – StairZ
'JLabels '은 다른 것과 마찬가지로 구성 요소이므로 콘텐츠 창을 만들고 다른 구성 요소를 배치 할 수 있습니다. 최상위 구성 요소는 원할 경우 배경을 투명하게 유지해야 할 수 있습니다. (그들은 또한이 경우 opaqueness 계약에 복종해야한다). – kiheru
JLayeredPane을 사용하지 않았지만 동일한 문제가 발생하지 않았습니까? 내가 전에 말했듯이 배경을 투명하게 설정하는 방법은 무엇입니까? –