我想讓用戶點擊一個btnGenerate,然後生成一個隨機數字,該數字被分配給一個可以在eclipse中的應用程序窗口中的按鈕下面的框中打印出短語的短語。如何在GUI文本框而非Eclipse控制檯上打印輸出?
問題是隨機語句出現在Eclipse控制檯上,而不是我的GUI上的文本框。
任何幫助表示讚賞。這是我的代碼到目前爲止:
//generate crime button
JButton generateBtn = new JButton("Generate Crime");
generateBtn.setBackground(Color.LIGHT_GRAY);
generateBtn.setFont(new Font("HGHeiseiKakugothictaiW3", Font.BOLD, 20));
GridBagConstraints gbc_generateBtn = new GridBagConstraints();
gbc_generateBtn.fill = GridBagConstraints.BOTH;
gbc_generateBtn.insets = new Insets(0, 0, 5, 5);
gbc_generateBtn.gridx = 15;
gbc_generateBtn.gridy = 5;
frmHeroVillains.getContentPane().add(generateBtn, gbc_generateBtn);
generateBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
/*random number generator that generates a number between 1-4 and outputs a random crime to the updatePane depending on which
number was generated */
int number = ((int) (Math.random()*4)+1);
switch (number) {
case 1:
System.out.println("Jewelry Heist on main street!");
break;
case 2:
System.out.println("Mugging in China town!");
break;
case 3:
System.out.println("Boeing 247 - Hijacked!");
default:
System.out.println("Nothing to Report.");
break;
}
;
JLabel updateLabel = new JLabel("UPDATE ALERT.... " + number);
GridBagConstraints gbc_updateLabel = new GridBagConstraints();
gbc_updateLabel.gridheight = 3;
gbc_updateLabel.insets = new Insets(0, 0, 5, 5);
gbc_updateLabel.gridx = 15;
gbc_updateLabel.gridy = 12;
frmHeroVillains.getContentPane().add(updateLabel, gbc_updateLabel);} }
);
那究竟是什麼問題呢? – Mureinik
隨機語句出現在Eclipse控制檯上,而不是我的GUI上的文本框 –
你希望更新什麼「文本框」? – MadProgrammer