gpt4 book ai didi

java - 无法禁用 DateEditor 的 JTextField 的编辑?

转载 作者:行者123 更新时间:2023-12-02 06:39:12 25 4
gpt4 key购买 nike

我过去一直使用以下技术来禁用 JSpinner 的编辑。但是,它似乎不适用于 DateEditor 中的 JTextField。这个 editor.getTextField().setEditable(false) 在这种情况下不起作用是否有原因?有没有办法让它发挥作用?

驱动程序类别

public class CLIClockDriver {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new ClockFrame();
}
});
}
}

Swing 类

public class ClockFrame extends JFrame implements ActionListener {

private static final long serialVersionUID = -7009835158496820266L;
public static final Dimension SIZE = new Dimension(400, 200);

private final Timer secondsTimer = new Timer(1000, this);
private final FluentGridBagConstraints gbc = new FluentGridBagConstraints();
private JPanel mainPanel;
private JLabel clockLabel;
private JSpinner newAlarmHourSpinner, newAlarmMinuteSpinner, newAlarmAMPMSpinner;

public ClockFrame() {
super();
setLayout(new GridBagLayout());
initializeGUIComponents();
setPreferredSize(SIZE);
setResizable(false);
setTitle(Constants.APPLICATION_NAME);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
secondsTimer.start();
}



private void initializeGUIComponents() {
mainPanel = new JPanel(new GridBagLayout());

clockLabel = new JLabel();
updateClock();
mainPanel.add(clockLabel, gbc.setXY(0, 0));

newAlarmHourSpinner = new JSpinner();
newAlarmHourSpinner.setModel(new CyclingSpinnerNumberModel(9, 1, 12, 1));
DefaultEditor editor = new JSpinner.DefaultEditor(newAlarmHourSpinner);
editor.getTextField().setColumns(2);
editor.getTextField().setEditable(false);
newAlarmHourSpinner.setEditor(editor);
mainPanel.add(newAlarmHourSpinner, gbc.reset().setXY(0, 1));

newAlarmMinuteSpinner = new JSpinner();
SpinnerDateModel model = new SpinnerDateModel();
model.setCalendarField(Calendar.MINUTE);
newAlarmMinuteSpinner.setModel(model);
DateEditor dateEditor = new JSpinner.DateEditor(newAlarmMinuteSpinner, "mm");
editor.getTextField().setColumns(2);
editor.getTextField().setEditable(false);
newAlarmMinuteSpinner.setEditor(dateEditor);
mainPanel.add(newAlarmMinuteSpinner, gbc.reset().setXY(1, 1));

newAlarmAMPMSpinner = new JSpinner();
newAlarmAMPMSpinner.setModel(new CyclingSpinnerListModel("AM", "PM"));
editor = new JSpinner.DefaultEditor(newAlarmAMPMSpinner);
editor.getTextField().setColumns(2);
editor.getTextField().setEditable(false);
newAlarmAMPMSpinner.setEditor(editor);
mainPanel.add(newAlarmAMPMSpinner, gbc.reset().setXY(2, 1));

add(mainPanel, gbc.reset().setXY(0, 0));
}

private void updateClock() {
clockLabel.setText(Alarm.TIME_FORMAT.format(new Date()));
}

@Override
public void actionPerformed(ActionEvent event) {
if(event.getSource() == secondsTimer) {
updateClock();
}
}


}

最佳答案

确保您的 GUI 是在 EDT 上创建的。

当我在 How to Use Spinners 上运行 Swing 教程中的 SpinnerDemo 代码时,它对我来说工作得很好。进行以下更改:

    ftf = getTextField(spinner);
ftf.setEditable(false);

但是,当我没有在 EDT 上创建 GUI 时,我的简单旋转器测试程序也遇到了问题。

关于java - 无法禁用 DateEditor 的 JTextField 的编辑?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19330724/

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