1 import java.awt.*; 2 import javax.swing.*; 3 4 public class BeatBox { 5 JFrame theFrame; 6 JPanel mainPanel; 7 String[] instrumentNames = {"Bass Drum", "Closed Hi-Hat", 8 "Open Hi-Hat", "Acoustic Snare", 9 "Crash Cymbal", "Hand Clap", 10 "High Tom", "Hi Bongo", 11 "Maracas", "Whistle", 12 "Low Conga", "Cowbell", 13 "Vibraslap", "Low-mid Tom", 14 "High Agogo", "Open Hi Conga"}; 15 16 public static void main(String[] args) { 17 new BeatBox().buildGUI(); 18 } 19 20 public void buildGUI() { 21 theFrame = new JFrame("Cyber BeatBox"); 22 theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 23 24 JPanel background = new JPanel(new BorderLayout()); 25 background.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); 26 27 Box buttonBox = new Box(BoxLayout.Y_AXIS); 28 29 JButton start = new JButton("Start"); 30 buttonBox.add(start); 31 32 JButton stop = new JButton("Stop"); 33 buttonBox.add(stop); 34 35 JButton upTempo = new JButton("Tempo Up"); 36 buttonBox.add(upTempo); 37 38 JButton downTempo = new JButton("Tempo Down"); 39 buttonBox.add(downTempo); 40 41 background.add(BorderLayout.EAST, buttonBox); 42 43 Box nameBox = new Box(BoxLayout.Y_AXIS); 44 for (String instrument: instrumentNames) { 45 nameBox.add(new Label(instrument)); 46 } 47 background.add(BorderLayout.WEST, nameBox); 48 49 GridLayout grid = new GridLayout(16, 16); 50 mainPanel = new JPanel(grid); 51 52 for (int i = 0; i < 256; i++) { 53 JCheckBox c = new JCheckBox(); 54 c.setSelected(false); 55 mainPanel.add(c); 56 } 57 background.add(BorderLayout.CENTER, mainPanel); 58 59 theFrame.setContentPane(background); 60 theFrame.setBounds(50, 50, 300, 300); 61 theFrame.pack(); 62 theFrame.setVisible(true); 63 } 64 }
原文:http://www.cnblogs.com/ren-yu/p/6357600.html