簡體   English   中英

嘗試將ActionListener放在按鈕上,然后使其繪制文本

[英]Trying to put an ActionListener on a button then make it paint text

我對Java還是很陌生,不久前我寫了一個神奇的8球程序。 好吧,我剛剛了解了JFrames和JPanels,所以我想對其進行更新。 我在兩件事上遇到麻煩,我不知道這是否因為因為從未使用過此網站而要求太多。 我對動作偵聽器不太了解,我需要幫助,將其放在b按鈕上以使其繪制新的JPanel,以繪制我的隨機選擇器語句。 那是另一回事,我如何使其繪制我的數組元素之一?

window

   package pack;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import javax.swing.*;

 public class window extends ball  {
 public static void main (String []args) {
    JFrame f = new JFrame("hello");
    JPanel p = new JPanel();
    JLabel l = new JLabel("What do you ask the magic conch shell?");
    JTextField t = new JTextField(25);
    JButton b = new JButton();

    b.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {                        }
           });
        //panel
    b.setText("Ask");
    p.add(l);
    p.add(b);
    p.add(t);

    //frame
    f.add(p);
    f.setSize(300, 400);
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
    }}

ball

     package pack;

    import java.util.Scanner;
    import java.util.Random;

    public class ball  {
    public static void main(String[]args) {
     String question = ("What do you ask the Magic Conch shell?");
      Scanner userInput =new Scanner(System.in);
       String answer = userInput.nextLine();

    String[] array;
     array = new String[8];
     array[0] = ("yes");
     array[1] = ("no");
     array[2] = ("maybe");
     array[3] = ("never");
     array[4] = ("ask again later");
     array[5] = ("positive");
     array[6] = ("unlikely");
     array[7] = ("yes");

     Random dice = new Random();
     int n = dice.nextInt(8);
     System.out.println(array[n]);
    }
    }

從您的ball類中獲取代碼,使用它創建一個返回擲骰結果的方法,這為您提供了重用點

public class Ball {

    private String[] results = new String[] {
        "yes", "no", "maybe", "never", "ask again later", "postive", "unlikely", "yes"
    };

    private Random random = new Random();

    public String roll() {
        int n = random.nextInt(results.length);
        return results[n];
    }

}

現在,在按鈕ActionListener ,您需要“滾動”球

Ball ball = new Ball();
b.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {                        
        String result = ball.roll();
    }
});

現在,您的下一個問題是,實際上沒有任何地方可以顯示結果。

如果可以的話,您真的不想在這個階段引入一個新組件,而是希望一個組件可以使用,因此,讓我們添加另一個JLabel作為“答案”

Ball ball = new Ball();
JLabel answer = new JLabel("...");
b.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        String result = ball.roll();
        answer.setText(result); 
    }
});
//panel
b.setText("Ask");
p.add(l);
p.add(b);
p.add(t);
p.add(answer);

在運行時添加組件並不困難,但是它確實引入了您需要注意的許多其他問題。 在此,我們通過引入一個組件可以限制某些問題,我們可以根據需要簡單地對其進行更新

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM