2016-09-20 22 views
0

我努力嘗試在Java中創建一個記事本類型的東西,我掙扎了一下。到目前爲止,我有一個類如下,這是一個JComponentUtilityComponent延伸JComponent)。其中,您可以看到我正在渲染圖形以製作記事本形狀,現在我想在此形狀內輸入一個JTextArea以輸入內容。但是我找不到一種方法來使它工作。我已經嘗試,因爲它現在看起來,我已經嘗試將其添加到根JFrame(在一個案例中,它佔用了整個JFrame),我嘗試使用JPanel組件等,但不能完全正確!如何在JComponent中顯示JTextArea上方的圖形?

package jUtility; 

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics2D; 

import javax.swing.JScrollPane; 
import javax.swing.JTextArea; 

public class Note extends UtilityComponent { 

    private static final long serialVersionUID = 2890336222216437001L; 
    private static int width, height; 
    private static JScrollPane text; 

    public Note(int x, int y, ID id, int w, int h) { 
     super(x, y, id); 
     width = w; 
     height = h; 
     JTextArea t = new JTextArea(5,5); 
     t.setText("sdfsdaf"); 
     t.setBackground(Color.BLUE); 
     t.setLocation(50, 50); 
     t.setForeground(Color.RED); 
     t.setSize(new Dimension(50,50)); 
     text = new JScrollPane(t); 
     text.setVisible(true); 
    } 

    public void tick() { 

    } 


    public void render(Graphics2D g2) { 
     g2.setColor(Color.GRAY); 
     g2.fillRoundRect(x, y, width, height, 50, 50); 
     g2.setColor(Color.DARK_GRAY); 
     g2.fillRoundRect(x + 2, y + 2, width - 4, height - 4, 50, 50); 
     g2.setColor(Color.GRAY); 
     g2.fillRoundRect(x + 23, y + 23, width - 46, height - 46, 50, 50); 
     g2.setColor(Color.WHITE); 
     g2.fillRoundRect(x + 25, y + 25, width - 50, height - 50, 50, 50); 
    } 

} 
+1

爲了儘快提供更好的幫助,請發佈[MCVE]或[簡短,獨立,正確的示例](http://www.sscce.org/)。 –

+0

你想如何讓你的記事本版本看起來像? – user3437460

+0

一個帶有彎角的灰色輪廓,想象一下垂直ipad的輪廓也許,而它的中間只是一個白色區域,用戶可以在其中輸入東西。我可以用Graphics來完成概要,不用擔心,它只是讓JTextArea合作(儘管如果有更好的方法,它不一定是JTextArea)。 – Jottle4

回答

1

我已經想了很多心痛之後。我改變了它,所以我的窗口(Jframe事物)首先包含一個背景JLayeredPane,然後我先將圖形添加到JLayeredPane.DAFAULT_LAYER,然後將我的JTextArea添加到更高的東西。我幾個小時遇到的問題是我需要設置添加到JLayeredPane的任何組件的位置和大小!

+0

還有其他的方式,但很高興你能對它進行排序。 –