我创建了一个开始菜单,它启动了我制作的单独GUI。但是,当我尝试启动它时没有任何反应。我公开了初始化并调用了该函数,但没有任何效果。以下是我正在使用的内容:
这是应该启动另一个窗口的按钮。
// start the program from the first lesson
JButton btnStart = new JButton("Start");
btnStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
PianoGUI gui = new PianoGUI();
gui.initialize();
}
});
这是其他GUI的代码(显然不是整个代码,只是相关内容):
public void initialize()
{
int width = 1280;
int height = 720;
int wKeyWidth = width/17;
int bKeyWidth = wKeyWidth*3/4;
int bKeyPos = wKeyWidth*5/8;
frame = new JFrame();
frame.setBounds(0, 0, width, height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
Button btnDb3 = new Button("");
btnDb3.setBackground(Color.BLACK);
btnDb3.setBounds(bKeyPos+wKeyWidth*0, 0, bKeyWidth, 290);
frame.getContentPane().add(btnDb3);
我的问题是:显然
PianoGUI gui = new PianoGUI();
gui.Initialize();
不起作用。我做错了什么?
public void initialize()
{
int width = 1280;
int height = 720;
int wKeyWidth = width/17;
int bKeyWidth = wKeyWidth*3/4;
int bKeyPos = wKeyWidth*5/8;
frame = new JFrame();
frame.setBounds(0, 0, width, height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
Button btnDb3 = new Button("");
btnDb3.setBackground(Color.BLACK);
btnDb3.setBounds(bKeyPos+wKeyWidth*0, 0, bKeyWidth, 290);
frame.getContentPane().add(btnDb3);
JButton btnC3 = new JButton("");
btnC3.setBackground(Color.WHITE);
btnC3.setBounds(0, 0, wKeyWidth, 471);
frame.getContentPane().add(btnC3);
Button btnEb3 = new Button("");
btnEb3.setBackground(Color.BLACK);
btnEb3.setBounds(bKeyPos+wKeyWidth*1, 0, bKeyWidth, 290);
frame.getContentPane().add(btnEb3);
JButton btnD3 = new JButton("");
btnD3.setBackground(Color.WHITE);
btnD3.setBounds(wKeyWidth*1, 0, wKeyWidth, 471);
frame.getContentPane().add(btnD3);
JButton btnE3 = new JButton("");
btnE3.setBackground(Color.WHITE);
btnE3.setBounds(wKeyWidth*2, 0, wKeyWidth, 471);
frame.getContentPane().add(btnE3);
Button btnGb3 = new Button("");
btnGb3.setBackground(Color.BLACK);
btnGb3.setBounds(bKeyPos+wKeyWidth*3, 0, bKeyWidth, 290);
frame.getContentPane().add(btnGb3);
Button btnAb3 = new Button("sharp");
btnAb3.setBackground(Color.BLACK);
btnAb3.setBounds(bKeyPos+wKeyWidth*4, 0, bKeyWidth, 290);
frame.getContentPane().add(btnAb3);
Button btnBb3 = new Button("sharp");
btnBb3.setBackground(Color.BLACK);
btnBb3.setBounds(bKeyPos+wKeyWidth*5, 0, bKeyWidth, 290);
frame.getContentPane().add(btnBb3);
JButton btnF3 = new JButton("");
btnF3.setBackground(Color.WHITE);
btnF3.setBounds(wKeyWidth*3, 0, wKeyWidth, 471);
frame.getContentPane().add(btnF3);
JButton btnG3 = new JButton("");
btnG3.setBackground(Color.WHITE);
btnG3.setBounds(wKeyWidth*4, 0, wKeyWidth, 471);
frame.getContentPane().add(btnG3);
JButton btnA3 = new JButton("");
btnA3.setBackground(Color.WHITE);
btnA3.setBounds(wKeyWidth*5, 0, wKeyWidth, 471);
frame.getContentPane().add(btnA3);
Button btnDb4 = new Button("");
btnDb4.setBackground(Color.BLACK);
btnDb4.setBounds(bKeyPos+wKeyWidth*7, 0, bKeyWidth, 290);
frame.getContentPane().add(btnDb4);
JButton btnB3 = new JButton("");
btnB3.setBackground(Color.WHITE);
btnB3.setBounds(wKeyWidth*6, 0, wKeyWidth, 471);
frame.getContentPane().add(btnB3);
JButton btnC4 = new JButton("");
btnC4.setBackground(Color.WHITE);
btnC4.setBounds(wKeyWidth*7, 0, wKeyWidth, 471);
frame.getContentPane().add(btnC4);
Button btnEb4 = new Button("sharp");
btnEb4.setBackground(Color.BLACK);
btnEb4.setBounds(bKeyPos+wKeyWidth*8, 0, bKeyWidth, 290);
frame.getContentPane().add(btnEb4);
JButton btnD4 = new JButton("");
btnD4.setBackground(Color.WHITE);
btnD4.setBounds(wKeyWidth*8, 0, wKeyWidth, 471);
frame.getContentPane().add(btnD4);
JButton btnE4 = new JButton("");
btnE4.setBackground(Color.WHITE);
btnE4.setBounds(wKeyWidth*9, 0, wKeyWidth, 471);
frame.getContentPane().add(btnE4);
Button btnGb4 = new Button("sharp");
btnGb4.setBackground(Color.BLACK);
btnGb4.setBounds(bKeyPos+wKeyWidth*10, 0, bKeyWidth, 290);
frame.getContentPane().add(btnGb4);
JButton btnF4 = new JButton("");
btnF4.setBackground(Color.WHITE);
btnF4.setBounds(wKeyWidth*10, 0, wKeyWidth, 471);
frame.getContentPane().add(btnF4);
Button btnAb4 = new Button("sharp");
btnAb4.setBackground(Color.BLACK);
btnAb4.setBounds(bKeyPos+wKeyWidth*11, 0, bKeyWidth, 290);
frame.getContentPane().add(btnAb4);
Button btnBb4 = new Button("sharp");
btnBb4.setBackground(Color.BLACK);
btnBb4.setBounds(bKeyPos+wKeyWidth*12, 0, bKeyWidth, 290);
frame.getContentPane().add(btnBb4);
JButton btnG4 = new JButton("");
btnG4.setBackground(Color.WHITE);
btnG4.setBounds(wKeyWidth*11, 0, wKeyWidth, 471);
frame.getContentPane().add(btnG4);
JButton btnA4 = new JButton("");
btnA4.setBackground(Color.WHITE);
btnA4.setBounds(wKeyWidth*12, 0, wKeyWidth, 471);
frame.getContentPane().add(btnA4);
JButton btnB4 = new JButton("");
btnB4.setBackground(Color.WHITE);
btnB4.setBounds(wKeyWidth*13, 0, wKeyWidth, 471);
frame.getContentPane().add(btnB4);
Button btnDb5 = new Button("sharp");
btnDb5.setBackground(Color.BLACK);
btnDb5.setBounds(bKeyPos+wKeyWidth*14, 0, bKeyWidth, 290);
frame.getContentPane().add(btnDb5);
Button btnEb5 = new Button("sharp");
btnEb5.setBackground(Color.BLACK);
btnEb5.setBounds(bKeyPos+wKeyWidth*15, 0, bKeyWidth, 290);
frame.getContentPane().add(btnEb5);
JButton btnC5 = new JButton("");
btnC5.setBackground(Color.WHITE);
btnC5.setBounds(wKeyWidth*14, 0, wKeyWidth, 471);
frame.getContentPane().add(btnC5);
JButton btnD5 = new JButton("");
btnD5.setBackground(Color.WHITE);
btnD5.setBounds(wKeyWidth*15, 0, wKeyWidth, 471);
frame.getContentPane().add(btnD5);
JButton btnE5 = new JButton("");
btnE5.setBackground(Color.WHITE);
btnE5.setBounds(wKeyWidth*16, 0, wKeyWidth, 471);
frame.getContentPane().add(btnE5);
JTextArea lessonText = new JTextArea();
lessonText.setEditable(false);
lessonText.setBounds(10, 537, 1380, 201);
frame.getContentPane().add(lessonText);
JButton btnNewButton_2 = new JButton("New button");
btnNewButton_2.setFont(new Font("Gill Sans MT Condensed", Font.BOLD, 24));
btnNewButton_2.setBackground(new Color(210, 180, 140));
btnNewButton_2.setBounds(0, 471, 176, 65);
frame.getContentPane().add(btnNewButton_2);
JButton button_2 = new JButton("New button");
button_2.setFont(new Font("Gill Sans MT Condensed", Font.BOLD, 24));
button_2.setBackground(new Color(210, 180, 140));
button_2.setBounds(173, 471, 176, 65);
frame.getContentPane().add(button_2);
JButton button_3 = new JButton("New button");
button_3.setFont(new Font("Gill Sans MT Condensed", Font.BOLD, 24));
button_3.setBackground(new Color(210, 180, 140));
button_3.setBounds(345, 471, 176, 65);
frame.getContentPane().add(button_3);
JButton button_4 = new JButton("New button");
button_4.setFont(new Font("Gill Sans MT Condensed", Font.BOLD, 24));
button_4.setBackground(new Color(210, 180, 140));
button_4.setBounds(520, 471, 176, 65);
frame.getContentPane().add(button_4);
JButton btnNext = new JButton("Next");
btnNext.setFont(new Font("Gill Sans MT Condensed", Font.BOLD, 24));
btnNext.setBackground(new Color(210, 180, 140));
btnNext.setBounds(1224, 471, 176, 65);
frame.getContentPane().add(btnNext);
JButton btnBack = new JButton("Back");
btnBack.setFont(new Font("Gill Sans MT Condensed", Font.BOLD, 24));
btnBack.setBackground(new Color(210, 180, 140));
btnBack.setBounds(1050, 471, 176, 65);
frame.getContentPane().add(btnBack);
}