[英]Where to place JOptionPaneShowInputDialog code?
在哪里放置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);
}
}
我可以幫忙嗎? 謝謝
問題和意圖尚不清楚。 對話的目的是什么?
我假設您想收集一些信息,然后可以將這些信息傳遞給FractalTree
來更改其呈現方式
問題和意圖尚不清楚。 對話的目的是什么?
我假設您想收集一些信息,然后將這些信息傳遞給FractalTree
以更改其呈現方式,在這種情況下,您可能需要在創建FractalTree
之前放置對話框
public static void gui()
{
// You can put the dialog here...
JFrame f=new JFrame("fractal tree");
//...
如果要在顯示FractalTree
之后更改其屬性,則可能需要使用JButton
和ActionListener
並將對話框放置在其中……或提供第二個視圖以直接收集屬性
實際上,該對話框的目的是詢問遞歸級別。 如果我按照您的建議放置它,就可以了,但是對話框是單獨出現的,而不是出現在黑色面板中,我希望它出現在面板中...
讓我們清楚一點,所以它不是教程或指導網站。 您的問題不是技術問題,而是經驗問題。 您應該花更多的時間閱讀教程,例如使用Swing創建GUI並嘗試一些事情。 這就是您將成為更好的開發人員並學會解決自己的問題的方式。
根據您的反饋, JOptionPane
不是您所需要的。
相反,您需要采取稍微不同的方法並提供自己的輸入組件。
首先,您需要更改FractalTree
以便可以更輕松地修改depth
屬性(並將初始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
方法並返回更合適的大小來解決。
這將使您擁有一條“更好的道路”,仍然需要解決一些問題,因為為您做所有這一切都不會幫助您
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.