2016-06-28 43 views
0

我在Swing Designer中使用Java Eclipse,我試圖在JPanel上顯示一些圖形。 我是一個漂亮的業餘程序員,也是圖形編碼的新手。Java在JPanel上看不到圖形

基本上,有一個JFrame有2個JPanel(Stage和Setttings)。在Surface類中有一個paintTest方法。此方法應在舞臺JPanel上繪製字符串「This is a test」。 當我執行代碼時,沒有錯誤。 任何幫助,將不勝感激。

請參閱下面的代碼。

import java.awt.Color; 
import java.awt.Component; 
import java.awt.EventQueue; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.border.BevelBorder; 
import javax.swing.JLabel; 
import java.awt.Font; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.Window; 



public class TestSimulator extends JFrame{ 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 
    private JFrame frmTestSimulation; 
    private JPanel stagePanel = new JPanel(); 
    private JLabel stageLabel = new JLabel("Stage"); 
    private JPanel settingsPanel = new JPanel(); 
    private JLabel settingsLabel = new JLabel("Settings"); 


    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        TestSimulator window = new TestSimulator(); 
        window.frmTestSimulation.setVisible(true); 

       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    /** 
    * Create the application. 
    */ 
    public TestSimulator() { 
     initialize(); 
    } 


    /** 
    * Initialize the contents of the frame. 
    */ 
    private void initialize() { 
     frmTestSimulation = new JFrame(); 
     frmTestSimulation.setTitle("WAPP Simulation"); 
     frmTestSimulation.setBounds(100, 100, 727, 500); 
     frmTestSimulation.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frmTestSimulation.getContentPane().setLayout(null); 

     //JPanel stagePanel = new JPanel(); 
     stagePanel.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null)); 
     stagePanel.setBounds(262, 11, 439, 439); 
     frmTestSimulation.getContentPane().add(stagePanel); 
     stagePanel.setLayout(null); 
     stagePanel.add(new Surface()); //Show the string 'This is a test' 

     //JLabel stageLabel = new JLabel("Stage"); 
     stageLabel.setFont(new Font("Tahoma", Font.BOLD, 14)); 
     stageLabel.setBounds(183, 11, 53, 34); 
     stagePanel.add(stageLabel); 

     //JPanel settingsPanel = new JPanel(); 
     settingsPanel.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null)); 
     settingsPanel.setBounds(10, 11, 242, 439); 

     frmTestSimulation.getContentPane().add(settingsPanel); 
     settingsPanel.setLayout(null); 

     //JLabel settingsLabel = new JLabel("Settings"); 
     settingsLabel.setFont(new Font("Tahoma", Font.BOLD, 14)); 
     settingsLabel.setBounds(78, 11, 71, 22); 
     settingsPanel.add(settingsLabel); 

    } 

    class Surface extends JPanel{ 

     public void paintTest(Graphics g) 
     { 
      Graphics2D g2d = (Graphics2D) g; 
      g2d.drawString("This is a test", 50, 50); 

     } 

     @Override 
     public void paintComponent(Graphics g) { 

      super.paintComponent(g); 
      paintTest(g); 
     } 



    } 
} 
+2

我建議避免空佈局 - 選擇最適合您需要的[佈局管理器](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html),並記住您可以嵌套子組件內部的佈局 – copeg

+0

謝謝copeg。我改爲Box Layout作爲測試。我現在可以看到字符串。不能相信它。至少4個小時。 – RayG

+0

@copeg arr,你打敗了我,我一直在調試一段時間 – Jezor

回答

3

您的stagePanel缺少佈局,所以只會打印其中的一個組件。更改

stagePanel.setLayout(null); 

stagePanel.setLayout(new GridLayout(1, 2)); 

它應該顯示像

image of the resulting window

一些參考this website有關在Swing佈局的更多信息,並this answer更多地瞭解顯示自定義在JPanels中的東西。