java中的钢琴布局

时间:2013-12-22 08:43:06

标签: java swing layout jlayeredpane

我正在尝试使用java创建虚拟钢琴。 我遇到了布局问题。

以下是应该感兴趣的片段:

初始化:

public class Piano extends JFrame implements KeyListener {
private PianoKeyboard keyboardPanel;    
    public Piano() {
        super();
        this.setTitle("Piano");
        this.setName("Piano");      
        this.setSize(850,200);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);
        keyboardPanel = new PianoKeyboard(0);
        keyboardPanel.addListener(this);
        this.getContentPane().add(keyboardPanel);
        this.setVisible(true);
    }
}

分层窗格:

public class PianoKeyboard extends JLayeredPane implements MouseListener{
    public PianoKeyboard(int tempo){
        super();
        this.setEnabled(true);
        this.setVisible(true);
        createKeyboard();
    }
privte void createKeyboard() {
        setLayout(new GridLayout(25, 0));           
        int loc = 0;
        for(int i = 0; i < 25; i++){
            PianoKey k = new PianoKey(i+1,tempo);
            k.addMouseListener(this);
            if(i ==0){
                k.setLocation(0);
            }else{
                if(PianoKey.isWhite(i+1) && PianoKey.isWhite(i)){
                    loc+=54;
                }else if(PianoKey.isWhite(i+1) && !PianoKey.isWhite(i)){
                    loc+=15;
                }else if(!PianoKey.isWhite(i+1) && PianoKey.isWhite(i)){
                    loc+=39;
                }else if(!PianoKey.isWhite(i+1) && !PianoKey.isWhite(i)){
                    // impossible
                } 
                k.setLocation(loc);
            }
            add(k,(PianoKey.isWhite(i+1))?new Integer(0):new Integer(1));
        }
    }
}

以下是个人密钥的组成部分:

public class PianoKey extends JComponent {
public PianoKey(int pitch, int tempo) {
        super();
        this.pitch = pitch;
        ClassLoader cl = this.getClass().getClassLoader();
        try {
            BufferedImage key;
            if (isWhite(this.pitch)) {
                key = ImageIO.read(cl.getResource(IMAGE_DIRECTORY
                        + "/key/white.png"));
                height = 150;
                width = 54;
            } else {
                key = ImageIO.read(cl.getResource(IMAGE_DIRECTORY
                        + "/key/black.png"));
                height = 89;
                width = 29;
            }
            Dimension sz = new Dimension(width, height);
            System.out.println("Setting to: "+width+"; "+height);
            setPreferredSize(sz);
            setMinimumSize(sz);
            setMaximumSize(sz);
            setBounds(0,0,width,height);
            setImage(key);
        } catch (IOException e) {
            Logger.getLogger(MusicNote.class).log(Level.WARNING,
                    "IO Error. Music Not duration. failed finding     image");
        }
    }
@Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        BufferedImage key = (BufferedImage) getImage();
        g.drawImage(key, offset, 0, width,height, null);

    }

    int offset = 0;
    public void setLocation(int x){
        offset = x;
        setBounds(offset,0,width,height);
    }
 }

Current Piano Layout

y偏移似乎在逐渐变化,高度完全错误。 图像具有以下尺寸:黑键(29x89),白键(54x150)

如果删除了分层窗格中的gridlayout,我相信的笔记全部堆叠在一起,只有一个笔记可见且可点击。

1 个答案:

答案 0 :(得分:2)

GridLayout被逆转了。

setLayout(new GridLayout(25, 0)); 

这将创建25个不同的行。这就是为什么你的钥匙“踩到” - 每个都在一个单独的线上

setLayout(new GridLayout(1, 25));

以上将为您提供一行25列