【发布时间】: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