Java 单击按钮时如何启用文本字段?
在我开始之前,我是一个初学者程序员 单击按钮时如何启用文本字段 我有两个框架,一个包含JFields,另一个用于异常。 发生异常时>设置可编辑(false) 但是,当用户单击OK按钮(我在exception中所做的)时,我应该做出什么声明来启用JFields呢 我尝试将静态布尔值添加到异常帧中,在这个类执行的操作中,我将该布尔值初始化为true 在另一个类中,我添加了一个if语句,如果该布尔值为true,则为setEditable(true) -========- 这个程序的要点是,当异常发生时,用户在关闭异常窗口之前不能在字段中输入任何内容 我希望你能帮助我 带着全部的爱,程序员们 为异常窗口框架执行的操作代码(具有“确定”按钮)。Java 单击按钮时如何启用文本字段?,java,swing,jframe,field,jbutton,Java,Swing,Jframe,Field,Jbutton,在我开始之前,我是一个初学者程序员 单击按钮时如何启用文本字段 我有两个框架,一个包含JFields,另一个用于异常。 发生异常时>设置可编辑(false) 但是,当用户单击OK按钮(我在exception中所做的)时,我应该做出什么声明来启用JFields呢 我尝试将静态布尔值添加到异常帧中,在这个类执行的操作中,我将该布尔值初始化为true 在另一个类中,我添加了一个if语句,如果该布尔值为true,则为setEditable(true) -========- 这个程序的要点是,当异常发生时
public void actionPerformed(ActionEvent e){
{
allow=true; //static boolean
Container TheFrame = OKButton.getParent();
do TheFrame = TheFrame.getParent();
while (!(TheFrame instanceof JFrame));
((JFrame) TheFrame).dispose();
}
为主程序执行的操作代码(有三个字段,用户输入非数字时会出现异常)
我补充了一些评论来澄清
public void actionPerformed(ActionEvent event) {
try{
r =Double.parseDouble(RField.getText());
s=Double.parseDouble(SField.getText());
h=Double.parseDouble(HField.getText());
Cone C = new Cone(r,s,h);//class cone
if (event.getSource() instanceof JButton) {
JButton clickedButton = (JButton) event.getSource();
if (clickedButton == VolumeButton) {
Result.append("VOLUME = "+C.volume()+ "\n");
ifV= true;//this's for clearing the fields for new entries.
}
if (clickedButton == AreaButton) {
Result.append("SURFACE AREA = "+C.surfaceArea()+ "\n");
ifA= true;//this's for clearing the fields for new entries.
}
if(ifA&&ifV){ // clearing the fields for new entries.
SField.setText(CLEAR);
HField.setText(CLEAR);
RField.setText(CLEAR);
ifV=false; ifA= false;}
}
SList.addShape(C);
}
catch(NumberFormatException e){
//Object of type "Exception__" already created
Ex.setVisible(true);//class "Exception__" is the one i've made for Exception window
SField.setText(CLEAR);
HField.setText(CLEAR);
RField.setText(CLEAR);
SField.setEditable(false);
HField.setEditable(false);
RField.setEditable(false);
}/*here, if the user clicked on -that okay in Exception window-
and variable allow initialized to "true" those statements should extend. I guess?
- everything worked correctly except for this ?*/
if(Ex.allow){
SField.setEditable(true);
HField.setEditable(true);
RField.setEditable(true); }
}
谢谢大家,终于成功了。
我补充说
Ex.allow(SField,HField,RField);
顺其自然
并在类异常中添加了此方法:
public void allow(JTextField js,JTextField jh,JTextField jr){
HField =jh;
SField =js;
RField =jr;
}
最后,对于类异常执行的操作:
SField.setEditable(true);
HField.setEditable(true);
RField.setEditable(true);
哇哦。感觉真棒,哈哈。谢谢大家。我应该删除我的问题,还是将其留给可能面临与我相同问题的其他人P
你的问题需要更多的细节。但是,如果您只想显示一个“异常窗口”,并允许用户在关闭此窗口后执行任何其他操作,我认为您只需要一个MessageDialog: 看
如果需要显示更多详细信息,可以创建自己的模式请参见通过写入使文本字段隐藏: jTextfield.setVisible(fasle) 在表单代码的构造函数中。然后使用按钮事件“Action->Action Performed”并编写代码: jTextfield.setVisible(true)
因此,您的文本字段只有在单击按钮后才可见 发布你的代码和异常的堆栈跟踪,有人会很乐意帮助你。如果你使用JDialog,它会自动禁用父窗口,直到它消失。非常感谢你,我已经添加了代码。很抱歉不够清晰。我尝试了JOptionPane,它成功地工作了,但我只是尝试了根据异常窗口禁用和启用字段的想法。非常感谢您的回答。我认为在我的情况下,setEditable更有用。我会用可编辑的方法试试你的建议。谢谢。