Java 重置切换按钮
我正在为我的软件工程课设计电梯。我使用电梯轿厢内部面板上的切换按钮选择楼层。如果当前楼层等于正在切换的按钮,我不知道如何关闭按钮。我已经包括了一个代码片段Java 重置切换按钮,java,swingutilities,jtogglebutton,Java,Swingutilities,Jtogglebutton,我正在为我的软件工程课设计电梯。我使用电梯轿厢内部面板上的切换按钮选择楼层。如果当前楼层等于正在切换的按钮,我不知道如何关闭按钮。我已经包括了一个代码片段 package test; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.AbstractButton; import javax.swing.JFrame; import javax.swing.JPan
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();
}
});
}
}
要禁用按钮或将其返回“未选择”状态?请避免使用
null
layouts,像素完美的布局在现代ui设计中是一种错觉。影响零部件单个尺寸的因素太多,您无法控制。Swing的设计初衷是与布局管理器一起工作,丢弃它们将导致无止境的问题,您将花费越来越多的时间试图纠正这些问题。考虑提供一个演示您的问题的解决方案。这将减少混乱,提高效率responses@MadProgrammer我想将按钮和面板返回到“未选择”状态以反映该更改。floor3.setSelected(false)
应该这样做