用swing做了一个游戏初始界面。效果如下:
代码为:
1 import javax.swing.*; 2 import java.awt.*; 3 import java.util.*; 4 5 public class StartWindow { 6 7 public static void main(String[] args) { 8 9 // create the frame 10 JFrame frame = new JFrame(); 11 12 // create the host panel 13 JPanel panelHost = new JPanel(); 14 BoxLayout layoutHost = new BoxLayout(panelHost, BoxLayout.Y_AXIS); 15 panelHost.setLayout(layoutHost); 16 17 // labelTop is added into the host panel 18 JLabel labelTop = new JLabel("Dancing with the stars"); 19 panelHost.add(labelTop); 20 21 // panelSlider is for the two JSlider components 22 JPanel panelSlider = new JPanel(); 23 24 BoxLayout layoutSlider = new BoxLayout(panelSlider, BoxLayout.Y_AXIS); 25 panelSlider.setLayout(layoutSlider); 26 27 // JSlider slider1 28 JLabel labelDifficulty = new JLabel("Select the difficulty level: "); 29 panelSlider.add(labelDifficulty); 30 JSlider slider1 = new JSlider(1, 10, 4); 31 slider1.setMajorTickSpacing(1); 32 slider1.setPaintTicks(true); 33 slider1.setSnapToTicks(true); 34 slider1.setPaintTrack(true); 35 slider1.setPaintLabels(true); 36 37 panelSlider.add(slider1); 38 39 // JSlider slider2 40 JLabel labelBalance = new JLabel("Assign your skill points to looks and dancing skills"); 41 panelSlider.add(labelBalance); 42 JSlider slider2 = new JSlider(1, 10, 5); 43 44 Hashtable<Integer, JLabel> labelsSlider2 = new Hashtable<>(); 45 labelsSlider2.put(1, new JLabel("LOOKS")); 46 labelsSlider2.put(10, new JLabel("DANCING SKILLS")); 47 48 slider2.setLabelTable(labelsSlider2); 49 slider2.setPaintLabels(true); 50 panelSlider.add(slider2); 51 52 panelHost.add(panelSlider); 53 54 // panelButton is for the two JButton components 55 JPanel panelButton = new JPanel(); 56 JButton button1 = new JButton("High Scores"); 57 JButton button2 = new JButton("START"); 58 JButton button3 = new JButton("Exit"); 59 panelButton.add(button1); 60 panelButton.add(button2); 61 panelButton.add(button3); 62 FlowLayout layoutButton = new FlowLayout(); 63 panelButton.setLayout(layoutButton); 64 panelHost.add(panelButton); 65 66 // labelBottom is added into the host panel 67 JLabel labelBottom = new JLabel("Dance your way to the TOP!"); 68 panelHost.add(labelBottom); 69 70 // panelHost and frame settings 71 frame.setTitle("Dance With Stars 0685182"); 72 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 73 frame.setContentPane(panelHost); 74 frame.pack(); 75 frame.setVisible(true); 76 frame.setResizable(false); 77 frame.setLocationRelativeTo(null); 78 79 } 80 81 }
请发表评论