1

画像私は 3 つのオブジェクトを追加する JLayeredPane を持っています。問題は、アプリが実行されているときの backgroundImage です。jframe と上からの背景画像の間にギャップがあり、何を変更してもギャップはまだ残っています。そこに(ギャップをなくしたい)。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);
    }
}
}
4

1 に答える 1

0

NavigationMenu のレイアウトを BorderLayout に変更する

于 2012-07-11T01:12:29.757 に答える