-2
如何使用揮杆在java(桌面應用程序)中繪製箭頭?我主要在那裏看箭頭的畫布。當我們創建一個畫布時,已經存在的容器被畫布取代,那麼只有箭頭顯示。如何解決這個問題?使用揮杆繪製箭頭
這是我的代碼。
public class last extends JFrame
{
public static JFrame frame = new JFrame("NoLayout Test");
ArrayList<Output> out;
public last()
{
JFrame.setDefaultLookAndFeelDecorated(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
frame.setSize(600, 400);
frame.setVisible(true);
}
public void Graph(ArrayList<Output> o) {
out=o;
int x1= 300-50, x2=300+25, y=34;
JLabel label = new JLabel(out.get(0).value);
label.setBounds(x1, y, x2, y);
addComponent(3, x1, y);
frame.add(label);
}
private void addComponent(int child, int x, int y)
{
Insets insets = frame.getInsets();
int x1, x2;
if (child ==1)
{
JLabel label = new JLabel("leaf");
label.setBounds(x, y*2, 100, 20);
frame.add(label);
}
if (child ==2)
{
JLabel label1 = new JLabel("nodes1");
x1=round(x/2-25);
System.out.println(x1+" "+ y);
label1.setBounds(x1, y*2, 100, 20);
frame.add(label1);
x1=round(x+(x/2+25));
System.out.println(x1+" "+ y);
JLabel label2 = new JLabel("nodes2");
label2.setBounds(x1, y*2, 100, 20);
frame.add(label2);
}
if (child ==3)
{
x1=x/2-25;
JLabel label = new JLabel("nodes1");
Dimension size = label.getPreferredSize();
label.setBounds(x1 + insets.left, y*2 + insets.top, size.width, size.height);
System.out.println(x1+" "+ y*2);
//label.setBounds(x1, y*2, 100, 20);
frame.add(label);
x1= x+(x/2+25);
JLabel label2 = new JLabel("nodes2");
size = label2.getPreferredSize();
label2.setBounds(x1 + insets.left, y*2 + insets.top, size.width, size.height);
frame.add(label2);
System.out.println(x1+" "+ y*2);
JLabel label3 = new JLabel("nodes3");
size = label3.getPreferredSize();
label3.setBounds(x + insets.left, y*2 + insets.top, size.width, size.height);
System.out.println(x1+" "+ y*2);
frame.add(label3);
}
}
}
我想繪製線條或箭頭來創建一棵樹。這些行添加在兩個節點之間,這樣就構成了一棵樹。
定位組件可能不是這裏的方法。除非你想'JTree',否則我會建議你自定義繪製樹的每個部分。 –