2012-11-03 52 views
3

吸引我,吸引了隨機三角形中的程序。我想添加一個按鈕,點擊後會擦除並重新生成另一個隨機三角形。我測試了,看看我是否可以做一個按鈕,在另一個程序,SwingButtonTest.java,顯示出來(這是我的風格,我正在學習),它成功了。添加一個Swing按鈕與AWT

然後,我基本上是複製了所有我認爲有必要讓一個按鈕從程序到我的三角形程序顯示的代碼。不幸的是,程序不顯示按鈕...

再次,任何幫助表示讚賞,感謝一噸!

import javax.swing.*; 

import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 


@SuppressWarnings({ "serial" }) 
public class TriangleApplet extends JApplet implements ActionListener { 

    int width, height; 
    int[] coords = new int[6]; 
    JButton refresh; 

    public void init() { 
     //This stuff was added from SwingButtonTest.java 
     Container container = getContentPane(); 
     container.setLayout(null); 

     container.setLayout(null); 

     refresh= new JButton("I like trains"); 
     refresh.setBackground(Color.yellow); 
     refresh.setSize(140, 20); 
     refresh.addActionListener(this); 

     container.add(refresh); 
     //End of SwingButtonTest.java stuff 

     coords = randomTri(); 
    } 


    public static int[] randomTri() { 
     int[] pointArray = new int[6]; 
     int x; 
     for (int i = 0; i < 6; i++) { 
      x = ((int)(Math.random()*40)*10); 
      pointArray[i] = x; 
     } 
     return pointArray; 
    } 


    public void paint(Graphics g) { 
     g.setColor(Color.BLUE); 

     g.drawLine(coords[0], coords[1], coords[2], coords[3]); 
     g.drawString("A: " + coords[0]/10 + ", " + coords[1]/10, coords[0], coords[1]); 
     g.drawLine(coords[2], coords[3], coords[4], coords[5]); 
     g.drawString("B: " + coords[2]/10 + ", " + coords[3]/10, coords[2], coords[3]); 
     g.drawLine(coords[4], coords[5], coords[0], coords[1]); 
     g.drawString("C: " + coords[4]/10 + ", " + coords[5]/10, coords[4], coords[5]); 
     //Math for AB 
     int abXDif = Math.abs(coords[0]/10 - coords[2]/10); 
     int abYDif = Math.abs(coords[1]/10 - coords[3]/10); 
     int abLength = (abXDif*abXDif) + (abYDif*abYDif); 
     g.drawString("AB: Sqrt of "+ abLength, 0, 10); 
     //Math for BC 
     int bcXDif = Math.abs(coords[2]/10 - coords[4]/10); 
     int bcYDif = Math.abs(coords[3]/10 - coords[5]/10); 
     int bcLength = (bcXDif*bcXDif) + (bcYDif*bcYDif); 
     g.drawString("BC: Sqrt of "+ bcLength, 0, 20); 
     //Math for AC 
     int acXDif = Math.abs(coords[4]/10 - coords[0]/10); 
     int acYDif = Math.abs(coords[5]/10 - coords[1]/10); 
     int acLength = (acXDif*acXDif) + (acYDif*acYDif); 
     g.drawString("AC: Sqrt of "+ acLength, 0, 30); 


    } 

@Override 
public void actionPerformed(ActionEvent e) { 
    // TODO Auto-generated method stub 

} 
} 

回答

6

按鈕沒有出現的原因是,你不叫:

super.paint(g); 
paint()方法

。此調用將導致按鈕和其他添加的組件被繪製。

custom painting in Swing更好的方法是將您的自定義繪畫放在組件中,擴展JComponent並覆蓋paintComponent以利用Swing的優化繪製模型。

還可以考慮使用一個layout manager大小&地方你的組件。避免使用絕對定位(null佈局)。從Doing Without a Layout Manager

儘管可以在沒有佈局管理器的情況下執行,但應儘可能使用佈局管理器。佈局管理器可以更輕鬆地調整到與外觀相關的組件外觀,不同的字體大小,容器的不斷變化的大小以及不同的區域設置。

+0

它沒有一個網站,僅僅是Eclipse applet瀏覽器上運行。雖然我想知道如何用JComponent做到這一點 - 有人可以給我一個演練嗎?謝謝! – liboan1997

+1

1)這是一個瀏覽器標籤/插件的問題,值得一個新的職位。 2)使用'JComponent'方法並返回任何特定問題。使用上面的自定義繪畫鏈接。 3)如果你對這個和[其他問題](http://stackoverflow.com/questions/13168078/java-applet-displays-itself-multiple-times)我很滿意,那麼請點擊'接受答案左側的'勾號'。 :) – Reimeus

+0

我跟着教程,其中一個例子有一個AWT按鈕,事件發生後,點擊它就會打印出一些東西。此按鈕顯示正常,所以我將按鈕從AWT按鈕切換到JButton。該按鈕不再顯示。有誰知道爲什麼? – liboan1997