所以我有一个叫做方程式的方法,应检查是否有任何值为0,但它们不是?据我所知,当我用单选按钮选择它们时,它们根本不运行。为什么是这样?它只是将位移值打印为0. Idk在这做什么?感谢所有帮助!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//import java.applet.Applet;
public class RadioButton extends JPanel {
//Values that can be entered at the beginning, the code will explain to you what this means and how you can find the other values
//If you don't know one of the values, make it 0
int xDisplacement = 0;
int xVAvg = 8;
int xTime = 8 ;
public void equations() {
if (xDisplacement == 0) {
//solve for displacement
int xDisplacement = xVAvg * xTime;
}
if (xVAvg == 0) {
int xVAvg = xDisplacement / xTime;
}
if (xTime == 0) {
int xTime = xDisplacement / xVAvg;
}
}
static JFrame frame;
JLabel pic;
RadioListener myListener = null;
protected JRadioButton displacement;
protected JRadioButton vAvg;
protected JRadioButton time;
public RadioButton() {
// Create the radio buttons
displacement = new JRadioButton("Displacement");
displacement.setMnemonic(KeyEvent.VK_N);
displacement.setActionCommand("displacement");
//Displacement Button, set to automatically be clicked
vAvg = new JRadioButton("Average Velocity");
vAvg.setMnemonic(KeyEvent.VK_A);
vAvg.setActionCommand("averagevelocity");
//Acceleration Button
time = new JRadioButton("Change in time");
time.setMnemonic(KeyEvent.VK_S);
time.setActionCommand("deltaT");
//The change in time button
// Creates the group of buttons
ButtonGroup group = new ButtonGroup();
group.add(displacement);
group.add(vAvg);
group.add(time);
myListener = new RadioListener();
displacement.addActionListener(myListener);
vAvg.addActionListener(myListener);
time.addActionListener(myListener);
// Set up the picture label
pic = new JLabel(new ImageIcon(""+"numbers" + ".jpg")); //Set the Default Image
pic.setPreferredSize(new Dimension(177, 122));
// Puts the radio buttons down
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(0, 1));
panel.add(displacement);
panel.add(vAvg);
panel.add(time);
setLayout(new BorderLayout());
add(panel, BorderLayout.WEST);
add(pic, BorderLayout.CENTER);
setBorder(BorderFactory.createEmptyBorder(40,40,40,40));
}
//Listening to the buttons
class RadioListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
pic.setIcon(new ImageIcon(""+e.getActionCommand() + ".jpg"));
equations();
running();
}
}
public static void main(String s[]) {
frame = new JFrame("∆x = Vavg * time");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
frame.getContentPane().add(new RadioButton(), BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
public void running() {
if ( displacement.isSelected()) {
//Option 1
System.out.println("The distance traveled on the x axis in meters is " + xDisplacement);
System.out.println("You can find the Average Velocity by dividing this number by time or find the time by dividing this number by the Average Velocity");
System.out.println("----------> ");
System.out.println("^ " + xDisplacement );
}
if ( vAvg.isSelected()) {
//Option 2
System.out.println("The average velocity in Meters per Second is " + xVAvg);
System.out.println("You can find the displacement by multiplying the time and this number together or to find the time, just divide the displacement by this number");
System.out.println("----------> ");
System.out.println("^ " + xVAvg );
}
if (time.isSelected()) {
//Option 3
System.out.println("The time in seconds is " + xTime);
System.out.println("You can find the displacement by multiplying the velocity times this number or you can find the average velocity by dividing the displacement by this number");
System.out.println("----------> ");
System.out.println("^ " + xTime );
}
}
}
答案 0 :(得分:5)
您正在创建新的块本地值,而不是修改RadioButton
的属性。
public class RadioButton extends JPanel {
int xDisplacement = 0;
public void equations() {
if (xDisplacement == 0) {
// This is a **NEW** `xDisplacement`, visible only
// within these curly brackets, "shadowing" the
// instance property `xDisplacement`
int xDisplacement = xVAvg * xTime;
}
// etc.
只需删除int
语句中的if
即可修改该属性。
您的IDE可能会警告您,您正在隐藏某个属性,顺便说一句。
public void equations() {
if (xDisplacement == 0) {
xDisplacement = xVAvg * xTime;
}
// etc.