简体   繁体   English

Java Swing-MIDI钢琴的第一步

[英]Java Swing - MIDI Piano First Steps

I'm only in my first stages of creating this project but I'm already lost. 我只是在创建这个项目的最初阶段,但是我已经迷路了。 In my class we mainly focus on AWT, but for this project I was told to learn swing. 在我的课堂上,我们主要专注于AWT,但是对于这个项目,我被告知要学习挥杆。

Right now I am experimenting with Layouts to position the keys in the right positions as a normal keyboard would. 现在,我正在尝试使用“布局”将按键像普通键盘那样放置在正确的位置。 I just really need help getting my layout on point. 我只是真的需要帮助来使我的布局正确。 If someone could explain and provide code that would be fantastic. 如果有人可以解释并提供代码,那就太好了。 Thanks 谢谢

Code: 码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.File;
import java.util.ArrayList;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;

public class main extends JFrame implements ActionListener
{
private JButton a1,a1s,b1,c1,c1s,d1,d1s,e1,f1,f1s,g1,g1s;
private JPanel panel;
private int x_pos = 50;
String title = "INTERACTIVE PIANO";    


private void createGUI()
{
  setDefaultCloseOperation(EXIT_ON_CLOSE);
  Container window = getContentPane();
  window.setLayout(new FlowLayout() );

  /*  panel = new JPanel();   
  panel.setPreferredSize(new Dimension(800, 700));
  panel.setBackground(Color.white);
  panel.setLayout(null);
  panel.setBackground(Color.gray);
  window.add(panel); */
  addKeys(); 
  }

  public void addKeys()
  {
    a1 = new JButton("A"); 
    {
      a1.setBackground(Color.white);
      a1.setPreferredSize(new Dimension(50, 150));
      a1.setLayout(null);
      a1.addActionListener(this);
      add(a1);

    } 


    a1s = new JButton("A#");
    {
      a1s.setBackground(Color.black);
      a1s.setPreferredSize(new Dimension(50, 100));
      a1s.setLayout(null);
      a1s.addActionListener(this);
      add(a1s);
    } 


    b1 = new JButton("B"); 
    {
      b1.setBackground(Color.white);
      b1.setPreferredSize(new Dimension(50, 150));
      b1.setLayout(null);
      b1.addActionListener(this);
      add(b1);
    }  

    c1 = new JButton("C"); 
    {
      c1.setBackground(Color.white);
      c1.setPreferredSize(new Dimension(50, 150));
      c1.setLayout(null);
      c1.addActionListener(this);
      add(c1);
    }  

    c1s = new JButton("C#");
    {
      c1s.setBackground(Color.black);
      c1s.setPreferredSize(new Dimension(50, 100));
      c1s.setLayout(null);
      c1s.addActionListener(this);
      add(c1s);
    } 

    d1 = new JButton("D"); 
    {
      d1.setBackground(Color.white);
      d1.setPreferredSize(new Dimension(50, 150));
      d1.setLayout(null);
      d1.addActionListener(this);
      add(d1);
    }  

    d1s = new JButton("D#");
    {
      d1s.setBackground(Color.black);
      d1s.setPreferredSize(new Dimension(50, 100));
      d1s.setLayout(null);
      d1s.addActionListener(this);
      add(d1s);
    } 

    e1 = new JButton("E"); 
    {
      e1.setBackground(Color.white);
      e1.setPreferredSize(new Dimension(50, 150));
      e1.setLayout(null);
      e1.addActionListener(this);
      add(e1);
    } 

    f1 = new JButton("F"); 
    {
      f1.setBackground(Color.white);
      f1.setPreferredSize(new Dimension(50, 150));
      f1.setLayout(null);
      f1.addActionListener(this);
      add(f1);
    }  

    f1s = new JButton("F#");
     {
      f1s.setBackground(Color.black);
      f1s.setPreferredSize(new Dimension(50, 100));
      f1s.setLayout(null);
      f1s.addActionListener(this);
      add(f1s);
    }  

    g1 = new JButton("G"); 
    {
      g1.setBackground(Color.white);
      g1.setPreferredSize(new Dimension(50, 150));
      g1.setLayout(null);
      g1.addActionListener(this);
      add(g1);
    }  

    g1s = new JButton("G#");
    {
      g1s.setBackground(Color.black);
      g1s.setPreferredSize(new Dimension(50, 100));
      g1s.setLayout(null);
      g1s.addActionListener(this);
      add(g1s);
    }  

 }
  public void actionPerformed(ActionEvent event)
  {
   //  Graphics paper = panel.getGraphics(); 
   if (event.getSource() == a1)
   {

   }


  } 
 public static void main(String[] args)
 {
  main frame = new main();
  frame.setTitle("Interactive Piano v1.0");
  frame.setSize(800, 700);
  frame.setMinimumSize(new Dimension(800, 700));
  frame.createGUI();
  frame.setVisible(true);
  frame.setLayout(null);
 }
}

Layout managers are really designed for 2 dimensional positioning of components. 布局管理器实际上是为组件的二维定位而设计的。

For a piano you want the black keys on top of the white keys so this is one case where you would probably want to use a null layout so you can control the positioning of each component. 对于钢琴,您希望黑键位于白键之上,因此在这种情况下,您可能希望使用空布局,以便可以控制每个组件的位置。 This means you are now responsible for setting the size/location of each component. 这意味着您现在负责设置每个组件的大小/位置。

You would add the black keys to the panel first and then add the white keys. 您需要先将黑键添加到面板,然后再添加白键。

I'll give you a little hint. 我会给你一点提示。 Your main panel would look something like this: 您的主面板如下所示:

JPanel contentPane = new JPanel(null)
{
    @Override
    public Dimension getPreferredSize()
    {
        int count = getComponentCount();
        Component last = getComponent(count - 1);
        Rectangle bounds = last.getBounds();
        int width = 10 + bounds.x + bounds.width;
        int height = 10 + bounds.y + bounds.height;

        return new Dimension(width, height);
    }

    @Override
    public boolean isOptimizedDrawingEnabled()
    {
        return false;
    }
};

Since the white keys are added last, the last component on the panel will be a white key. 由于白键是最后添加的,因此面板上的最后一个组件将是白键。 So this component is used to determine the preferred size of the panel to fit all the white keys. 因此,此组件用于确定适合所有白色键的面板的首选大小。

The isOptimizedDrawingEnabled() method is overridden since components will be stacked on top of one another. 由于组件将被堆叠在另一个之上,因此isOptimizedDrawingEnabled()方法将被覆盖。 This will ensure that the black keys are always painted on top of the white keys. 这将确保黑键始终涂在白键的顶部。

Actually JLayeredPane is designed to do what you want: Overlap widgets with a prescribed z-order. 实际上,JLayeredPane旨在满足您的要求:具有指定z顺序的重叠小部件。 Something like this: 像这样:

package experimental;

import static experimental.Experimental.KeyColor.BLACK;
import static experimental.Experimental.KeyColor.WHITE;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.SwingUtilities;

class Experimental {
  static final int WHITE_KEY_WIDTH = 48;
  static final int WHITE_KEY_HEIGHT = 300;
  static final int BLACK_KEY_WIDTH = 32;
  static final int BLACK_KEY_HEIGHT = 210;

  enum KeyColor { WHITE, BLACK };

  enum PitchClass {
    A("A", WHITE),
    A_SHARP("A#", BLACK),
    B("B", WHITE),
    C("C", WHITE),
    C_SHARP("C#", BLACK),
    D("D", WHITE),
    D_SHARP("D#", BLACK),
    E("E", WHITE),
    F("F", WHITE),
    F_SHARP("F#", BLACK),
    G("G", WHITE),
    G_SHARP("G#", BLACK),
    ;

    final String name;
    final KeyColor color;

    PitchClass(String name, KeyColor color) {
      this.name = name;
      this.color = color;
    }
  }

  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
        JFrame frame = new JFrame("Keyboard");
        JLayeredPane keyboard = new JLayeredPane();
        frame.add(keyboard);
        addKeys(keyboard);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(2000, 500);
        frame.setVisible(true);
      }

      private void addKeys(JLayeredPane keyboard) {
        int x = 0;
        int keyCount = 0;
        for (int octave = 1; ; ++octave) {
          for (PitchClass note : PitchClass.values()) {
            JButton key = new JButton(note.name);
            key.setOpaque(true);
            key.setActionCommand(String.format("%d/%s", octave, note.name));
            if (note.color == WHITE) {
              key.setBounds(x, 0, WHITE_KEY_WIDTH, WHITE_KEY_HEIGHT);
              keyboard.add(key, new Integer(0));
              x += WHITE_KEY_WIDTH;
            } else {
              key.setBounds(x - BLACK_KEY_WIDTH / 2, 0, BLACK_KEY_WIDTH, BLACK_KEY_HEIGHT);
              keyboard.add(key, new Integer(1));
            }
            key.addMouseListener(new MouseAdapter() {
              @Override
              public void mousePressed(MouseEvent e) {
                super.mousePressed(e); 
                if (e.getButton() == 1) {
                  System.out.println("Press: "+ ((JButton) e.getSource()).getActionCommand());
                }
              }
              @Override
              public void mouseReleased(MouseEvent e) {
                super.mouseReleased(e);
                if (e.getButton() == 1) {
                  System.out.println("Release: "+ ((JButton) e.getSource()).getActionCommand());
                }
              }
            });
            if (++keyCount == 40) return;
          }
        }
      }
    });
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM