我想我找到了原因。我在 Netbeans 中添加了一个 bean,它是一个小面板。当我从框架中删除它时,似乎问题已解决。(我正在使用 frame.repaint() 方法,如下所示)
感谢您的帮助和评论。在您的评论的帮助下,我正在改进我的代码。
在这里,我发布了 MyPanel(那个 bean)的代码:
package beans;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.geom.Path2D;
public class MyParallelPanel extends javax.swing.JPanel {
int x1=300;
int x2=400;
int h=110;
Path2D path = new Path2D.Double();
Line2D line1 = new Line2D.Double(0, h, (x2-x1)/2, 0);
Line2D line2 = new Line2D.Double((x2-x1)/2, 0, x2-((x2-x1)/2), 0);
Line2D line3 = new Line2D.Double(x2-((x2-x1)/2), 0, x2, h);
Line2D line4 = new Line2D.Double(x2, h, 0, h);
Color color = new Color(237, 236, 235);
/** Creates new form MyParallelPanel */
public MyParallelPanel(int x1, int x2, int h,Color color) {
this.x1=x1;
this.x2=x2;
this.h=h;
this.color=color;
setSize(x2,h);
setPreferredSize(new Dimension(x2, h));
}
public void setColor(Color color) {
this.color = color;
}
public void setH(int h) {
this.h = h;
}
public void setX1(int x1) {
this.x1 = x1;
}
public void setX2(int x2) {
this.x2 = x2;
}
public MyParallelPanel() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
}// </editor-fold>
// Variables declaration - do not modify
// End of variables declaration
@Override
protected void paintComponent(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
path.append(line1,true);
path.append(line2,true);
path.append(line3,true);
path.append(line4,true);
g2d.setColor(color);
g2d.fill(path);
}
}