2012-07-23 154 views
0

我從我的主類使用下面的調用創建另一個類的一個對象的Java漆:問題與框架

JSplash splash = new JSplash(); 

然而,當我創建這個對象它執行JSplash類的構造函數,並給出我的窗戶和我的按鈕。但它不會畫在畫框上。你能幫我解決這個問題嗎?

import java.awt.Color; 
import java.awt.Font; 
import java.awt.Graphics; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JFrame; 

/** 
* 
* @author Curtis 
*/ 
public class JSplash extends DFrame implements ActionListener { 
//declaration of variable objects 
    Font myFont = new Font("Arial", Font.BOLD, 20); 
    JButton myButton = new JButton("Click Me!"); 
    Color bgColor = new Color(0, 0, 255); 
    Color firstColor = new Color(255, 255, 255); 
    String first = "Welcome to DaemoDynamics!"; 
    String last = "Click the Button"; 
    String middle = ""; 
    String middle2 = ""; 
    private static int count = 1; 
    DFrame splash = new DFrame(); 
//Constructor 
    public JSplash() { 
     setDefaultLookAndFeelDecorated(true); 
     System.out.println("Hello"); 
     setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); 
     splash.add(myButton); 
     getContentPane().setBackground(bgColor); 
     //adds action listener 
     myButton.addActionListener(this); 
     splash.setVisible(true); 
    } 
//Paint method 
    @Override 
    public void paint(Graphics e) { 
     System.out.println("paint is being reached"); 
     super.paint(e); 
     e.setFont(myFont); 
     e.setColor(firstColor); 
     e.drawString(first, 14, 80); 
     e.drawString(last, 70, 240); 
     e.drawString(middle, 75, 150); 
     e.drawString(middle2, 60, 175); 
    } 

//Listener Method 
    @Override 
    public void actionPerformed(ActionEvent e) { 
     //First Time button hit 
     if (count == 1) { 
      middle = "Brighter Business"; 
      middle2 = "for A Brighter Future"; 
      last = "Click Again to Begin"; 
      repaint(); 
      //increases button count 
      count++; 
     } else//if button count is not 1 
     { 
      splash.setVisible(false); 
      FinalProject app = new FinalProject(); 
     } 
    } 
} 
+0

對不起,油漆正在達到線我試圖看到該方法它不是 – 2012-07-23 06:17:10

+0

添加了'splash.setLayout(新的BorderLayout());'然後改爲'add(myButton,BorderLayout.SOUTH);'沒有效果 – 2012-07-23 06:25:57

+0

爲了更好的幫助, [SSCCE](http://sscce.org/)。 – 2012-07-23 06:27:23

回答

1

您已在您的JSplash構造函數中創建了一個新的DFrame,然後將您的組件添加到了IT。這根本不是必需的。刪除參考的飛濺,並簡單地使用你已經擴展的DFrame。而當我在看,splash沒有佈局管理器,這不會幫助。因爲在屏幕上顯示的窗口永遠不會是JSplash,但您創建的DFrame(稱爲啓動)

+0

非常好。謝謝! – 2012-07-23 07:38:59

0

創建一個擴展JPanel的新類。確保您的按鈕已添加到該面板,並用您的邏輯覆蓋該面板的paintComponent()方法。這應該工作。

Refer here.

風俗畫應的paintComponent方法來完成。理想情況下,您不應該重寫JFrame的paint方法。