简体   繁体   English

在按下按钮时读取Java

[英]reading when a button is pressed java

i cannot figure out how to get my code to tell when the button is pressed. 我无法弄清楚如何让我的代码知道何时按下按钮。 i was taught poorly, just FYI so any help will need to be greatly detailed. 我受的教育很差,仅供参考,所以任何帮助都需要非常详细。 also, i am new to the site so if the post isnt formatted correctly i am sorry. 另外,我是该网站的新手,所以,如果帖子格式不正确,对不起。

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);
}

Did you tried addActionListener method? 您是否尝试过addActionListener方法? For example; 例如;

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

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

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

You could make your class in which your main method exists, implement ActionListener, and then override the actionPerformed method. 您可以将您的主要方法存在于其中的类中,实现ActionListener,然后重写actionPerformed方法。 For example: 例如:

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.
       }
   }
...
}

You can do this for all the buttons in the class. 您可以对课程中的所有按钮执行此操作。 I'm not sure which way of doing this is recommended though, thought I'd add it anyway. 不过我不确定建议使用哪种方法,因为我还是会添加它。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM