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? 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
You could make your class in which your main method exists, implement ActionListener, and then override the actionPerformed method. 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. :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.