处理 JDialog 的斗争

问题描述 投票:0回答:1

我尝试创建一个小 JDialog 作为弹出窗口,用于向用户请求两个值:宽度和高度。经过大量调整后,我想我可以在处理对话框时检索这些值,方法是使用窗口适配器的 windowClosed 方法将这些值与对话框已作为布尔值关闭的信息一起传递给对象。 main 方法只是初始化对话框并等待它发出信号,表明它已退出。我可以验证 windowClosed 方法是否已执行,但它似乎仍然没有结束我的 while 循环,等待对象布尔值更改为 false。谢谢您提前的帮助。

代码中删除了对话框和导入的格式化内容。

//dialogSubstitute.java 
 public class ResizeDialogSubstitute { 
     // I cant code plz help me, slopy solution for returning from a dialog ): 
     int Width, Height; 
     void SetWidth(int width){Width=width;} 
     void SetHeight(int height){Height=height;} 
     boolean stillrunning = true; 
     ResizeDialogSubstitute(){ 
         Width=0; 
         Height=0; 
     } 
 } 
  
 // test.java 
 public class test { 
     String NewWidth,NewHeight; //Terrible way of returning from a function 
     public static void main(String[] args) { 
         ResizeDialogSubstitute uwudaddy = new ResizeDialogSubstitute(); 
         ResizeDialog dialog = new ResizeDialog(100,100, uwudaddy); 
         dialog.setVisible(true); 
         while(uwudaddy.stillrunning); 
         System.out.println(uwudaddy.Height); 
         System.out.println(uwudaddy.Width); 
     } 
 } 
  
 // ResizeDialog.java 
 public class ResizeDialog extends JDialog 
 { 
  JTextArea x,y;   
   ResizeDialogSubstitute uwudaddy; 
     ResizeDialog(int widthNow, int heightNow, ResizeDialogSubstitute UwuDaddy) 
     { 
         uwudaddy=UwuDaddy; 
         this.setSize(300, 200); 
         this.setTitle("Resize"); 
         this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); 
         JLabel label1= new JLabel("Define new Imagesize"); 
         this.add(label1,c); 
         JLabel label2= new JLabel("Width:"); 
         this.add(label2,c); 
         x = new JTextArea(Integer.toString(widthNow)); 
         this.add(x,c); 
         JLabel label3 = new JLabel("Height:"); 
         this.add(label3,c); 
         y = new JTextArea(Integer.toString(heightNow)); 
         this.add(y,c); 
         this.addWindowListener(new WindowAdapter()  
         { 
           public void windowClosed(WindowEvent e) 
           { 
             System.out.println("At least I gotr so far"); 
             //Tell Parent the result of the request, plz kill me 
             uwudaddy.SetHeight(Integer.parseInt(GetNewHeight())); 
             uwudaddy.SetWidth(Integer.parseInt(GetNewWidth())); 
             uwudaddy.stillrunning=false; 
           } 
  
           public void windowClosing(WindowEvent e) 
           { 
           } 
         }); 
  
     } 
     String GetNewWidth(){ 
         return x.getText(); 
     } 
     String GetNewHeight(){ 
         return y.getText(); 
     } 
 }
java swing dialog return
1个回答
0
投票

您的对话处理有缺陷。但你可以采取完全不同的方法:

对 JPanel 进行子类化并添加标签、字段、按钮 - 无论您需要什么。 如果您愿意,可以使用 UI 构建器。为您感兴趣的字段添加 getter/setter 方法。

实例化此自定义面板并使用设置器填充字段。 然后使用 JOptionPane.showOptionDialog 显示面板(将其作为消息传递)。我更喜欢在常用对话框中使用 Ok_Cancel_Option。

showOptionDialog 返回后,检查返回码。如果是 OK_OPTION,则使用面板的 getter 来访问用户输入。

© www.soinside.com 2019 - 2024. All rights reserved.