我正在为我的软件工程课程设计电梯。我正在使用面板内电梯轿厢上的切换按钮进行地板选择。如果当前楼层等于正在切换的按钮,我无法弄清楚如何关闭按钮。我已经包含了一个代码段。
package test;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.SwingUtilities;
public class test {
public JPanel createContentPane() {
// We create a bottom JPanel to place everything on.
JPanel totalGUI = new JPanel();
totalGUI.setLayout(null);
// Creation of a Panel to contain the buttons
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(null);
buttonPanel.setLocation(10, 80);
buttonPanel.setSize(500, 500);
totalGUI.add(buttonPanel);
final JToggleButton floor3 = new JToggleButton("3");
floor3.setSize(50, 50);
floor3.setLocation(68, 100);
buttonPanel.add(floor3);
floor3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (3 == 3) {
AbstractButton abstractButton = (AbstractButton) e
.getSource();
boolean selected = abstractButton.getModel().isSelected();
System.out.println("Action - selected=" + selected + "\n");
floor3.setSelected(!selected);
// code to toggle the third floor button off
}
}
});
totalGUI.setOpaque(true);
return totalGUI;
}
static void createAndShowGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Elevator");
// Create and set up the content pane.
test Welp = new test();
frame.setContentPane(Welp.createContentPane());
frame.setSize(250, 500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
// Schedule a job for the event-dispatching thread:
// creating and showing this application's GUI.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}