JFrame上的Swing图形

使用Java Graphics,我试图绘制一个简单的矩形。

作为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`等。

不建议覆盖顶级容器的paint方法……

JFrame包含许多其他组件放置在其上的重要层,请执行此操作…

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

您已经成功停止了任何子组件的开始绘制,或者事实上,除了您的矩形以外的任何其他组件。

在此处输入图像描述

(顶级摇摆容器的秘密生活 – 图片来自如何使用根窗格 )

虽然有些人可能会建议简单地添加对super.paint(g)的调用,但我不推荐它。

最好使用像JFrame的玻璃窗格……这将允许你在框架内的组件上绘画。 如果需要在组件下绘制,请替换JFrame的内容窗格。

你可能会发现……

  • 执行自定义绘画
  • 在AWT和Swing中绘画
  • 如何使用JLayer类装饰组件

使用…

更新

我想我开始发现你试图在文本区域上执行自定义绘画时遇到问题。

问题是, paintComponent在一次犯规中描绘背景和文本,这意味着在调用super.paintComponent之前你所做的任何绘画都将被渲染,但是在它上面完成的任何绘画都会在文本上绘制。

您可以将文本区域设置为非不透明并自己绘制背景…

在此处输入图像描述

 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 ,但是你怎么知道用户何时想要将组件设置为透明,那么你可以停止填充背景?

*使用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); } }