1
ボタンは私のプログラムでは掛け算されず、分割されないようです。私はこれを引き起こしているのか分かりませんが、助言をいただければ幸いです。あなたはをチェックし、唯一のJRadioButtonとJCheckBoxのを含めJToggleButtonsとその子供たちと一緒に働くことだJavaボタンが意図したとおりに機能しない
//View the buttom to be pushed
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MultDivide extends JFrame
{
private static final int FRAME_WIDTH = 300;
private static final int FRAME_HEIGHT = 100;
private double value;
private JButton multbutton;
private JButton divbutton;
private JLabel label;
public MultDivide()
{
super("I Multiply and Divide");
//get content pane and sets its layout
Color background = new Color(100,100,0);
Container contain = getContentPane();
contain.setLayout(new FlowLayout());
contain.setBackground(background);
//create multiple button
multbutton = new JButton("x5 ");
contain.add(multbutton);
//create divide button
divbutton = new JButton("/5");
contain.add(divbutton);
//initialize the value to 50
value = 50;
// create a label to display value
label = new JLabel("Value: " + Double.toString(value));
contain.add(label);
//creates listener and executes desired result
ButtonListener listener = new ButtonListener();
multbutton.addActionListener(listener);
divbutton.addActionListener(listener);
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setVisible(true);
}
privateclass ButtonListener implements ActionListener
{
//handle button event
public void actionPerformed(ActionEvent mult)
{
//updates counter when the button is pushed
if(divbutton.isSelected())
{
value = value/5.0;
label.setText("Value: " + Double.toString(value));
}
else
{
value = value* 5.0;
label.setText("Value: " + Double.toString(value));
}
}
}
}
@AnaviLucusソリューション。 +1 – DarkV1