gpt4 book ai didi

java - 不知道如何根据单选按钮运行图形?

转载 作者:行者123 更新时间:2023-11-30 03:36:44 25 4
gpt4 key购买 nike

所以我有 1 个类,带有一个单选按钮和 1 个类,它将根据单选按钮的结果创建一个小程序。我不知道如何使图形根据 if/else 语句运行。我们将不胜感激所有帮助。

单选按钮类:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class RadioButton extends JPanel {

static JFrame frame;

JLabel pic;
RadioListener myListener = null;
protected JRadioButton displacement;
protected JRadioButton accel;
protected JRadioButton time;
public RadioButton() {



// Create the radio buttons
displacement = new JRadioButton("Displacement");
displacement.setMnemonic(KeyEvent.VK_N);
displacement.setSelected(true);
//Displacement Button, set to automatically be clicked

accel = new JRadioButton("Acceleration");
accel.setMnemonic(KeyEvent.VK_A);
accel.setActionCommand("acceleration");
//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(accel);
group.add(time);

myListener = new RadioListener();
displacement.addActionListener(myListener);
accel.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(accel);
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"));
}
}

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

If/Else 语句类:

import java.lang.Object;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.*;


public class RadioButtonMain extends RadioButton {

public static void main(String [ ] args) {
new RadioButtonMain().doMain();
}

public void doMain() {
if ( displacement.isSelected()) {

//option 1 for applet
}



if ( accel.isSelected()) {
//Option 2 for applet
}

else {
//Option 3 for applet
}
}
}

如何根据是否按下变量 Accel 和 Displacement 来使图形运行?谢谢。

最佳答案

请记住,GUI 是一个事件驱动的环境,事物不会以线性方式运行。您不需要尝试自己运行方法,而是需要使用某种回调或监听器,它会告诉您程序/按钮的状态何时发生变化...

当引发 JRadioButton actionPerformed 事件时,您需要调用另一个方法来提供有关所发生情况的信息。然后,您可以在 RadioButtonMain 类中重写这些方法,并在调用它们时采取操作

这与 Observer Pattern 非常相似

关于java - 不知道如何根据单选按钮运行图形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27726971/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com