首页 > 解决方案 > 在哪里放置 JOptionPaneShowInputDialog 代码?

问题描述

在哪里放置 JOptionPaneShowInputDialog 代码?在哪里放置 JOptionPaneShowInputDialog 代码?在哪里放置 JOptionPaneShowInputDialog 代码?在哪里放置 JOptionPaneShowInputDialog 代码?在哪里放置 JOptionPaneShowInputDialog 代码?我想在下面的代码中添加一个 JoptionPaneShowInputDialog()(尚未在代码中)。

我可以,但在递归之前,我不能让对话框出现在黑色面板中。

绘图后出现对话框...

public class FractalTree extends JPanel implements ActionListener
     {

      int x1=350;  
      int y1=600;
      int angle=-90;
      int depth=11;          
      int k=10;

      JLabel label_1;

      private void drawTree(Graphics g, int x1, int y1, double angle, int depth, int k)
         {
          if(depth==11)

          g.setColor(Color.GRAY);
          g.fillRect(100, 600, 1160, 10);
          g.setColor(Color.white);
          if (depth == 0) return;

          ((Graphics2D) g).setStroke(new BasicStroke((float) (k*0.9)));
          int x2 = x1 + (int) (Math.cos(Math.toRadians(angle)) * depth * 11*Math.random()+1);
          int y2 = y1 + (int) (Math.sin(Math.toRadians(angle)) * depth * 11*Math.random()+1);
          if(depth<3)
           {
             g.setColor(Color.green);
           }

          g.drawLine(x1, y1, x2, y2);
          drawTree(g, x2, y2, angle - 19, depth-1,k-1);
          drawTree(g, x2, y2, angle + 19, depth-1,k-1);

         }


    public void paintComponent(Graphics g)
     {       
            super.paintComponent(g);
            g.setColor(Color.white);

            drawTree(g,x1,y1,angle,depth,k);
            drawTree(g,x1+600,y1,angle,depth,k);    
     }


    public FractalTree()
     {      
       this.setBackground(Color.black);  
     }

    public static void gui()
          {
           JFrame f=new JFrame("fractal tree");
           JLabel label_1=new JLabel("<html>RANDOM TREE<br><center>FRACTAL");
           label_1.setForeground(Color.red);
           Font font = new Font("Courier", Font.BOLD,25);
           label_1.setFont(font);
           FractalTree ft=new FractalTree();
           f.getContentPane().add(ft);
           f.setSize(1500, 1000);
           JButton button = new JButton("Close Me");
           button.addActionListener(ft);    
           f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         
           ft.add(label_1);
           ft.add(button);
           f.setUndecorated(true);
           f.setVisible(true);
          }

    public static void main(String[] args)
        {               
        gui();      
        }
    @Override
    public void actionPerformed(ActionEvent e)
        {
        System.exit(0);     
        }
}

我可以请一些帮助吗?谢谢

标签: javaswingjoptionpane

解决方案


问题和意图有些不清楚。对话的目的是什么?

我假设您想收集一些信息,然后可以将其传递给FractalTree以更改其渲染

问题和意图有些不清楚。对话的目的是什么?

我假设您想收集一些信息,然后可以将其传递给FractalTree以更改其渲染,在这种情况下,您可能需要在创建对话框之前放置对话框FractalTree

public static void gui()
{
    // You can put the dialog here...
    JFrame f=new JFrame("fractal tree");
    //...

如果您想FractalTree在显示后更改属性,那么您可能需要使用 aJButton和 aActionListener并将对话框放在那里......或者提供第二个视图来直接收集属性

实际上,对话框的目的是询问递归级别。如果我按照您的建议放置它,它可以,但是对话框单独出现,而不是在黑色面板中,我希望它出现在面板中......

让我们明确一点,SO 不是教程或指导网站。你的问题不是技术问题,而是经验问题。您应该花更多时间阅读教程,例如使用 Swing 创建 GUI并尝试一些东西。这就是您将如何成为更好的开发人员并学会解决自己的问题的方法。

根据您的反馈,JOptionPane这不是您需要的。

相反,您需要采用稍微不同的方法并提供自己的输入组件。

首先,您需要进行更改FractalTree,以便depth更轻松地修改属性(并将初始深度设置为0停止绘制)

public class FractalTree extends JPanel implements ActionListener {
    private int depth = 0; // set to 0 to stop it from rendering

    public void setDepth(int depth) {
        this.depth = depth;
        repaint();
    }

    public int getDepth() {
        return depth;
    }

接下来,您需要创建自己的输入组件,它可以从用户那里获取输入并更新树

public class InputPane extends JPanel {
    private FractalTree fractalTree;

    private JTextField depthField;

    public InputPane(FractalTree fractalTree) {
        this.fractalTree = fractalTree;
        depthField = new JTextField(10);
        depthField.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                String text = depthField.getText().trim();
                try {
                    int value = Integer.parseInt(text);
                    fractalTree.setDepth(value);
                } catch (NumberFormatException exp) {
                    JOptionPane.showMessageDialog(InputPane.this, text + " is not a valid numerical value");
                }
            }
        });
    }

}

然后你想创建一个可以将两者结合起来的新入口点......

import java.awt.EventQueue;
import javax.swing.JFrame;

public class Main {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                FractalTree tree = new FractalTree();
                InputPane input = new InputPane(tree);
                JFrame frame = new JFrame();
                frame.add(tree);
                frame.add(input, BorderLayout.SOUTH);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

是的,这将产生一个小窗口,因为FractalTree没有定义任何尺寸提示,这可以通过覆盖它的getPreferredSize方法并返回更合适的尺寸来解决。

这会让你拥有一条“更好的道路”,仍然有一些问题你需要解决,因为为你做这一切对你没有帮助


推荐阅读