【问题标题】:Where to place JOptionPaneShowInputDialog code?在哪里放置 JOptionPane ShowInputDialog 代码?
【发布时间】:2019-01-16 05:40:43
【问题描述】:

在哪里放置 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);     
        }
}

我可以请人帮忙吗? 谢谢

【问题讨论】:

  • 您需要一个可以在paintComponent 中使用的标志来阻止它在您不希望它绘制树时阻止它
  • 谢谢...你的意思是我需要在paintComponent方法中添加一些代码?
  • 理解,你无法控制绘画过程,绘画可能出于任何原因随时完成,而且绘画应该尽可能快地运行。所以,如果你想“停止”绘画的发生,你需要添加一个标志来确定何时应该绘制树,这在paintComponent 中检查,但在外部设置。不要将JOptionPane 放入paintComponent
  • 谢谢,但我几乎是 Java 初学者
  • 好的,谢谢,但是“flag”的意思是我必须添加一些代码?抱歉,几乎是初学者...

标签: java swing joptionpane


【解决方案1】:

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

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

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

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

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

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

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

让我们明确一点,SO 不是教程或指导网站。你的问题不是技术问题,而是经验问题。你应该花更多时间阅读Creating a GUI With Swing 之类的教程并尝试一些东西。这样您将成为一名更好的开发人员并学会解决自己的问题。

根据您的反馈,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 方法并返回更合适的大小来解决。

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

【讨论】:

  • 其实对话框的目的就是询问递归级别。如果我按照您的建议放置它,可以,但是对话框单独出现,而不是在黑色面板中,我希望它出现在面板中......谢谢。弗朗西斯
  • @Francis JOptionPane 不会“出现”在面板中,它将显示为对话框,这就是重点。如果您想提供额外的视图,那么您将不得不创建一个新的JPanel 并添加到现有视图中——所有这些都超出了 SO 的范围——因为它现在使问题变得广泛跨度>
猜你喜欢
  • 1970-01-01
  • 2015-10-31
  • 2015-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多