2013-01-04 50 views
0

使用Java圖形,我試圖繪製一個簡單的矩形。JFrame上的Swing圖形

由於Applet它運行良好,但是當我用它來顯示一個JFrame,該矩形來,但有一些不尋常的背景

這裏被編碼:

package graphicsex; 

import java.awt.Graphics; 

public class Graphics2 extends javax.swing.JFrame { 

    public static void main(String[] args) { 
     Graphics2 inst = new Graphics2(); 
     inst.setVisible(true); 
    } 

    public Graphics2() { 
     super(); 
     initGUI(); 
    } 

    private void initGUI() { 
     try { 
      setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); 
      pack(); 
      setSize(400, 300); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public void paint(Graphics g) { 
     g.drawRect(10, 20, 30, 40); 
    } 
} 

然後我試圖使用JTextArea使用這兩個類,但在這種情況下,矩形根本不顯示。

GraphicsEx1.java: 

package graphicsex; 

import javax.swing.JTextArea; 
import java.awt.Graphics; 

public class GraphicsEx1 extends javax.swing.JFrame { 

    private JTextArea jTextArea1; 

    { 
     //Set Look & Feel 
     try { 
      javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public static void main(String[] args) { 
     GraphicsEx1 inst = new GraphicsEx1(); 
     inst.setVisible(true); 
    } 

    public GraphicsEx1() { 
     super(); 
     initGUI(); 
    } 

    private void initGUI() { 
     try { 
      setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); 
      getContentPane().setLayout(null); 
      { 
       jTextArea1 = new JTextArea(); 
       getContentPane().add(jTextArea1); 
       jTextArea1.setBounds(7, 7, 371, 245); 
       jTextArea1.setEnabled(false); 
      } 
      pack(); 
      setSize(400, 300); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     postInitGUI(); 
    } 

    public void postInitGUI() { 
     DisplayItems dp = new DisplayItems(); 
     jTextArea1 = dp; 
     dp.setVisible(true); 
     this.add(jTextArea1); 
    } 
} 

而且DisplayItems.java:

package graphicsex; 

import java.awt.Dimension; 
import java.awt.Graphics; 

public class DisplayItems extends javax.swing.JTextArea { 

    public DisplayItems() { 
     super(); 
     initGUI(); 
    } 

    private void initGUI() { 
     try { 
      setPreferredSize(new Dimension(400, 300)); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public void paint(Graphics g) { 
     g.drawRect(10, 20, 50, 100); 
     g.drawString("Kalai", 90, 150); 
    } 
} 

任何一個可以幫助我顯示圖形組件上的任何擺動容器,如JFrame,的JPanel or JTextarea`等

回答

4

是不可取的覆蓋paint頂級容器的方法...

JFrame包含一些impo其上許多其它組件被放置,買這樣rtant層...

public void paint(Graphics g){ 
    g.drawRect(10,20,30,40); 
} 

您已經成功地阻止任何子組件從開始畫,或事實上,任何其他那麼你的矩形。

enter image description here

(頂級搖擺容器的祕密生活 - 從How to use Root Panes拍攝圖片)

雖然有些人可能認爲簡單的增加super.paint(g)打電話,我不會推薦它。

更好地使用類似於JFrame的玻璃窗格......這將允許您繪製駐留在框架內的組件。如果您需要在組件下繪畫,請替換JFrame的內容窗格。

您可能會發現...

使用的...

更新

我想我開始看到你在嘗試在文本區域上執行自定義繪畫的問題。

問題是,paintComponent在一次犯規中描繪背景和文字,這意味着您在調用super.paintComponent之前所做的任何繪畫都將被渲染,但是在其上完成的任何繪畫都會覆蓋文字。

您可以設置文本區域是非不透明和油漆自己的背景...

enter image description here

public class CustomTextArea extends JTextArea { 

    public CustomTextArea() { 
     setOpaque(false); 
    } 

    @Override 
    public Dimension getPreferredSize() { 
     return new Dimension(200, 200); 
    } 

    @Override 
    protected void paintComponent(Graphics g) { 
     g.setColor(getBackground()); 
     g.fillRect(0, 0, getWidth(), getHeight()); 
     g.setColor(Color.RED); 
     g.fillRect(0, 0, 100, 100); 
     super.paintComponent(g); 
    } 

} 

的問題是,雖然,它很容易爲人們休息不透明水平和摧毀你的工作。當然,您可以覆蓋setOpaquegetOpaque,但是您怎麼知道用戶實際上想要將組件設置爲透明,以便您可以停止填充背景?

+0

感謝您的信息,但我更喜歡使用第二種情況,即從主類調用jtextarea並在jtxtarea上繪製矩形。你能告訴我這件事情嗎? –

+0

@Mitu_Chennai你能突出顯示你正在努力實現什麼...... – MadProgrammer

+0

你可以在類'GraphicsEx1'的方法'postInitGUI()'中看到它的文本區域'jTextArea1'被引用到'DisplayItems'類中,該類可以擴展到JTextArea中。我已經完成了這個bcoz我認爲我可以很容易地在jtextarea上繪製矩形 –

0

使用的JPanel *使用的JPanel *

,圖形元素可以得出;

import java.awt.Graphics; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTextArea; 
import javax.swing.WindowConstants; 

public class GraphicsEx1 extends javax.swing.JFrame { 
private JScrollPane jsp1; 
private JTextArea jta1; 
private JPanel jpnl1; 

{ 
//Set Look & Feel 
try { 
javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel"); 
} catch(Exception e) {e.printStackTrace();} 
} 

public static void main(String[] args) { 
GraphicsEx1 inst = new GraphicsEx1(); 
inst.setLocationRelativeTo(null); 
inst.setVisible(true); 
} 

public GraphicsEx1() { 
super(); 
initGUI(); 
postInitGUI(); 
} 

private void initGUI() { 
try { 
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); 
getContentPane().setLayout(null); 
{ 
jsp1 = new JScrollPane(); 
getContentPane().add(jsp1); 
jsp1.setBounds(10, 32, 372, 223); 
{ 
jpnl1 = new JPanel(); //<---------------- 
jsp1.setViewportView(jpnl1); //<---------------- 
jpnl1.setBackground(new java.awt.Color(255,255,255)); 
jpnl1.setLayout(null); 
//jpnl1.setPreferredSize(new java.awt.Dimension(359, 327)); 
} 
} 
pack(); 
setSize(400, 300); 
} catch (Exception e) {e.printStackTrace();} 
} 

public void postInitGUI(){ 
frmGrpView gp=new frmGrpView(); 
jta1=new JTextArea(); 
jta1=gp; 
jta1.setBounds(0,0, 336, 197); 
jta1.setVisible(true); 
//jpnl1.setBounds(0, 0, 336, 197); 
jpnl1.add(jta1); //<---------------- 
jpnl1.revalidate(); 
jsp1.revalidate(); 
} 
} 
//----------------------- Second Class -------------------------- 
class frmGrpView extends JTextArea{ 
public frmGrpView() { 
super(); 
setEditable(false); 
} 
public void paint(Graphics g){ 
super.paint(g); 
g.drawRect(10, 10, 100, 100); 
} 
} 
+1

1.不推薦重寫'paint',因爲您刪除了Swing組件提供的任何雙緩衝支持,並且它通常在繪製鏈中處於高位。 2.這將在文本上繪製一個矩形 - 這可能是OP後面的內容,但值得指出;) – MadProgrammer