我有一个添加三个对象的 JLayeredPane,问题是 backgroundImage 当应用程序运行时,jframe 和顶部的背景图像之间存在间隙,我设置为(0,0)无论我改变什么间隙仍然那里(我想消除差距)。如果我复制 backgroundImage 类并将其放在另一个文件/类中,则间隙不存在。谁能帮忙?
public class NavigationMenu extends JPanel {
private ArrayList<String> Menu = new ArrayList();
private int MenuSize;
private int MenuChannel = 0;
private Font realFont;
private static Dimension screenSize = new Dimension(new ScreenDimensions().getScreenWidth(), new ScreenDimensions().getScreenHeight());
private static final int MENU_HEIGHT = (int)(new ScreenDimensions().getScreenHeight()*0.1);
private static final int MENU_WIDTH = new ScreenDimensions().getScreenWidth();
private static final int MENU_CENTER = (new ScreenDimensions().getScreenHeight() / 2) - (MENU_HEIGHT / 2);
public NavigationMenu() {
//Add Menu Channels
setPreferredSize(screenSize);
setBounds(0,0,MENU_WIDTH,screenSize.height);
this.Menu.add("MOVIES");
this.Menu.add("MUSIC");
this.Menu.add("PICTURES");
this.Menu.add("VIDEOS");
this.Menu.add("TV SHOWS");
this.Menu.add("WEATHER");
this.Menu.add("RADIO");
this.Menu.add("SETTINGS");
this.MenuSize = Menu.size() - 1;
JLayeredPane pfinal = new JLayeredPane();
JPanel _menuText = new menuText();
JPanel _backgroundImage = new backgroundImage("Backgrounds/curtains.png");
JPanel _bar = new bar();
pfinal.setPreferredSize(screenSize);
pfinal.setBounds(0,0,MENU_WIDTH,screenSize.height);
pfinal.add(_backgroundImage, new Integer(1));
pfinal.add(_bar, new Integer(2));
pfinal.add(_menuText, new Integer(3));
add(pfinal);
}
public class bar extends JPanel {
public bar() {
setBounds(0,MENU_CENTER,MENU_WIDTH, MENU_HEIGHT);
setOpaque(false);
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.8f));
g2d.setColor(Color.BLACK);
g2d.fillRect(0,0,screenSize.width,MENU_HEIGHT);
}
}
public class menuText extends JPanel {
public menuText() {
setBounds(0,MENU_CENTER,MENU_WIDTH,MENU_HEIGHT);
setOpaque(false);
setFocusable(true);
requestFocusInWindow();
try {
realFont = new RealFont(((int)(MENU_HEIGHT * 0.80))).getRealFont();
} catch (Exception e) {
System.out.println(e.toString());
}
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
//JOptionPane.showMessageDialog(null, "OK");
int keyCode = e.getKeyCode();
switch (KeyEvent.getKeyText(keyCode)) {
case "Up":
//JOptionPane.showMessageDialog(null, "Up");
if(MenuChannel < MenuSize) {
MenuChannel++;
repaint();
} else {
MenuChannel = 0; //reset
repaint();
}
break;
case "Down":
if(MenuChannel == 0) {
MenuChannel = MenuSize + 1;
MenuChannel--;
repaint();
} else {
MenuChannel--;
repaint();
}
break;
}
}
});
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
String MenuString = Menu.get(MenuChannel);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//g2d.setBackground(Color.PINK);
//g2d.fillRect(0,0,getWidth(),getHeight());
g2d.setColor(Color.WHITE);
g2d.setFont(realFont);
FontRenderContext context = g2d.getFontRenderContext();
java.awt.geom.Rectangle2D rect = realFont.getStringBounds(MenuString, context);
int textHeight = (int)rect.getHeight();
int textWidth = (int)rect.getWidth();
int panelHeight = getHeight();
int panelWidth = getWidth();
int x = (panelWidth - textWidth) / 2;
int y = (panelHeight - textHeight) / 2;
//System.out.println("f:"+ fontMetrics.gettH:" + textHeight + "\ntW:" + textWidth + "\npH:" + panelHeight + "\npW:" + panelWidth);
g2d.drawString(MenuString,x,(float)-rect.getY()+2);
}
}
public class backgroundImage extends JPanel {
private Image icon;
private int screenWidth = new ScreenDimensions().getScreenWidth();
private int screenHeight = new ScreenDimensions().getScreenHeight();
public backgroundImage(String background) {
icon = new ImageIcon(background).getImage();
Dimension size = new Dimension(screenWidth,screenHeight);
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setSize(size);
setLayout(null);
}
@Override
protected void paintComponent(Graphics g) {
g.drawImage(icon,0,0,screenSize.width,screenSize.height,null);
}
}
}