我創建了一個自定義的JPNEL,它的背景有固定的或影片的對角線。 我想知道的是,如果我在面板上添加任何jcomponent,它根本就不顯示。 我知道這個問題是因為我在組件上畫畫,但我不知道如何解決這個問題。 這是我的完整代碼。
希望能得到一些幫助:)
import java.awt.BasicStro;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.LayoutManager;
import java.awt.Paint。
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Shape;
import javax.swing.Jpanel;
import javax.swing.Timer;
public class JStripedPanel extends JPanel {
int space = 20;
float thickness = 2f;
int xpos = 0;
float progress = 0f;
Float th = 2f;
int dir = 1;
定時器定時器。
Timer cbt = null;
Paint lineColor = new Color(255, 50,50,50)。)
public JStripedPanel() {
super()。
}
public JStripedPanel(LayoutManager l) {
super(l)。
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g)。
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON) 。
g2.setPaint(getBackground())。
g2.fill(getShape())。
//g2.setClip(getShape());
if( thickness > 0) {
//繪制對角線。
g2.setPaint(getLineColor())。
g2.setStroke(new BasicStroke( thickness))。
int h = 0;
int max = Math. max(this.getSize().width, this.getSize().height) getSpace()。
int x = - max - getSpace() 。
while( x < this.getSize().width getSpace() ) {
g2.drawLine(xpos x, max, xpos x max, h)。
x = getSpace()。
}
}
//g2.setColor(Color.black);
//g2.setStroke(new BasicStroke(5));
//g2.drawLine(0, this.getSize().height/2, this.getSize().width, this.getSize().height/2);
g2.dispose()。
}
public void setLineColor(畫線顏色) {
this.lineColor = lineColor;
}
private Paint getLineColor() {
return lineColor。
}
public void setStripe(int space, float thikness) {
this.space = space;
this. thickness = thikness;
}
public int getSpace() {
return space。
}
private void setXpos(int xpos) {
if(xpos > getSpace())
xpos = 0;
this.xpos = xpos;
}
private int getXpos() {
return xpos。
}
private Shape getShape() {
Shape s = new Rectangle(getBounds() )。
return s;
}
public void startAnim(float thickness) {
this. thickness = thickness;
timer = new Timer(100, (e) -> {
setXpos(getXpos() 1) 。
repaint()。
setStripe(20, th);
th = th = .5f * dir;
if(th > 35.5f)
dir = -1;
else if(th < 1.5f)
dir = 1;
});
Timer.start();
}
public void stopAnim(boolean now) {
if(now) {
stopNow()。
return;
}
try {
cbt = new Timer(50, (e) -> {
timer.setDelay(timer.getDelay() 150) 。
if( thickness > 0) {
thickness -= 0.5f;
}
repaint()。
if(timer.getDelay() >= 2500) {
cbt.stop()。
cbt = null;
return;
}
});
cbt.start()。
} catch (Exception e) { }
stopNow()。
}
public void stopNow() {
if(timer != null)
timer.stop()。
timer = null;
thickness = 0;
System.gc()。
}
public static voidmain(String[] args) throws InterruptedException {
//這個面板是有效的! throws InterruptedException
// JPanel t = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 5));
//我的面板不
JStripedPanel t = new JStripedPanel(new FlowLayout(FlowLayout. LEADING, 5, 5)。)
t.setBackground(Color.pink)。
t.setOpaque(true)。
t.setBorder(
javax.swing.BorderFactory.createCompoundBorder(
new javax.swing.border。 EmptyBorder(1, 1, 1, 1) 。
new javax.swing.border.LineBorder(Color.blue, 2, true))
);
t.add(new javax.swing.JLabel("test") ) 。
t.add(new javax.swing.JTextField("test2") )。
javax.swing.JFrame f = new javax.swing.JFrame()。
f.setBounds(10,10,400,300) 。
//f.getContentPane().setLayout(new java.awt.GridLayout(3,3));
// f.getContentPane().add(javax.swing.Box.createHorizontalGlue());
//f.getContentPane().add(javax.swing.Box.createHorizontalGlue());
//f.getContentPane().add(javax.swing.Box.createHorizontalGlue());
//f.getContentPane().add(javax.swing.Box.createHorizontalGlue());
f.getContentPane().add(t, java.awt.BorderLayout.CENTER)。
// f.getContentPane().add(javax.swing.Box.createHorizontalGlue());
//f.getContentPane().add(javax.swing.Box.createHorizontalGlue());
f.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE)。
f.setVisible(true)。
t.startAnim(2)。
Thread.sleep(10000)。
t.stopAnim(false)。
Thread.sleep(5000)。
t.startAnim(2)。
}
uj5u.com熱心網友回復:
移除對g2.dispose()的呼叫,它將像一個魅力一樣作業。你不應該處置它,因為同樣的Graphics物件將進一步被用來繪制面板內的組件。
主要的繪畫方法是這樣作業的:
public void paint(Graphics g) {
paintComponent(g);
paintBorder(g);
paintChildren(g);
}
因此,在paintComponent中處置圖形使得它在接下來的paintxxx方法中沒有功能。
在一個更普遍的思維方式中,你不應該清除/處置/關閉任何你沒有所有權的物件。有人創建了Graphics,把它傳遞給你,你可以使用它(這就是目的),但不能處置它。這是創建它的人的責任。
。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/310260.html
標籤:
上一篇:大的csv檔案。MemoryError。無法為形狀為(7,62388743)、資料型別為object的陣列分配3.25GiB。
