按下按钮时读取java

时间:2016-06-05 08:20:40

标签: java jbutton actionevent

我无法弄清楚如何在按下按钮时让我的代码告诉我。我被教的很差,仅仅是因为我所以任何帮助都需要非常详细。此外,我是网站的新用户,所以如果帖子格式不正确,我很抱歉。

public static void main(String[] args) {
    final JFrame frame = new JFrame("JSlider Demo");
    final double odd = 50;
    final double bet = 1;
    boolean auto = false;
    double cash = 5.00;
    int cash1 = 0;
    JLabel jLabel1 = new JLabel("your cash: " + cash);

    JButton b1 = new JButton("GO!");
    b1.setVerticalTextPosition(AbstractButton.CENTER);
    b1.setHorizontalTextPosition(AbstractButton.LEADING); //aka LEFT, for left-to-right locales
    b1.setMnemonic(KeyEvent.VK_D);
    b1.setActionCommand("disable");

    // create odds slider
    JSlider odds = new JSlider(JSlider.HORIZONTAL, 0, 100, 50);
    odds.setMinorTickSpacing(5);
    odds.setMajorTickSpacing(25);
    odds.setPaintTicks(true);
    odds.setPaintLabels(true);
    odds.setLabelTable(odds.createStandardLabels(100));
    //Create the label table for the odds slider
    Hashtable labelTable1 = new Hashtable();
    labelTable1.put( new Integer( 50 ), new JLabel("Odds") );
    labelTable1.put( new Integer( 0 ), new JLabel("0") );
    labelTable1.put( new Integer( 100 ), new JLabel("100") );
    odds.setLabelTable( labelTable1 );
    odds.setPaintLabels(true);

    // create auto bet count slider
    JSlider count = new JSlider(JSlider.HORIZONTAL, 1, 101, 1);
    count.setMinorTickSpacing(5);
    count.setMajorTickSpacing(20);
    count.setPaintTicks(true);
    count.setPaintLabels(true);
    count.setLabelTable(count.createStandardLabels(50));
    //Create the label table for auto bet count
    Hashtable labelTable3 = new Hashtable();
    labelTable3.put( new Integer( 50 ), new JLabel("Auto-bet count") );
    labelTable3.put( new Integer( 1 ), new JLabel("1") );
    labelTable3.put( new Integer( 101 ), new JLabel("100") );
    count.setLabelTable( labelTable3 );
    count.setPaintLabels(true);

    // create auto bet speed slider
    JSlider speed = new JSlider(JSlider.HORIZONTAL, 0, 4, 0);
    speed.setMinorTickSpacing(20);
    speed.setMajorTickSpacing(1);
    speed.setPaintTicks(true);
    speed.setPaintLabels(true);
    speed.setLabelTable(speed.createStandardLabels(50));
    //Create the label table for speed 
    Hashtable labelTable4 = new Hashtable();
    labelTable4.put( new Integer( 2 ), new JLabel("Auto-bet speed") );
    labelTable4.put( new Integer( 0 ), new JLabel("1(BPS)") );
    labelTable4.put( new Integer( 4 ), new JLabel("5(BPS)") );
    speed.setLabelTable( labelTable4 );
    speed.setPaintLabels(true);

    //sets the GUI
    frame.setLayout(new FlowLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(650, 200);
    frame.getContentPane().add(odds);
    frame.getContentPane().add(count);
    frame.getContentPane().add(speed);
    frame.getContentPane().add(b1);
    frame.getContentPane().add(jLabel1);
    frame.setVisible(true);
}

2 个答案:

答案 0 :(得分:0)

您是否尝试过addActionListener方法?例如;

JButton b1 = new JButton("GO!");

b1.addActionListener(new ActionListener()
{
  public void actionPerformed(ActionEvent e)
  {
    // execute this method when the button is pressed
  }
});

文档: https://docs.oracle.com/javase/8/docs/api/java/awt/event/ActionListener.html

答案 1 :(得分:0)

您可以创建存在main方法的类,实现ActionListener,然后覆盖actionPerformed方法。例如:

public class A implements ActionListener {    

   JButton b1 = new JButton("Hello");
   b1.addActionListener(this);

   public void actionPerformed(ActionEvent e) {
       if (e.getSource == b1) {
         // do stuff when button b1 is clicked.
       }
   }
...
}

您可以对班级中的所有按钮执行此操作。我不确定推荐哪种方式,但我还是想加入它。 :)