我非常需要您的帮助,因为我不能独自解决这个问题。我正在尝试创建一个GUI,并希望在按下按钮后在其中绘制一些东西,但似乎存在某种刷新/重新验证或线程问题。绘画被绘制出来,但当我调整窗口大小时,绘画消失了。此外,当快速移动窗口时,部分图形会消失。我已经尝试过很多方法,但无法解决这个问题,也许您可以帮助我。我被告知不要写自己的代码,而是使用NetBeans设计功能生成按钮和面板等。也许这会影响绘图过程/功能,但我不知道。我将向您发布相关代码,并非常感谢您提供建议(注释掉的内容只是我以前尝试过的工件,所以不要注意它):
再次感谢您宝贵的时间。
public class NewJFrame extends JFrame {
public NewJFrame() { initComponents(); }
@SuppressWarnings("unchecked")
private void initComponents() {
jButton1 = new javax.swing.JButton();
jPanel1 = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setMinimumSize(new java.awt.Dimension(1200, 1000));
jButton1.setText("Draw");
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
GroupLayout jPanel1Layout = new GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGap(0, 1000, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, GroupLayout.DEFAULT_SIZE, 1000, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addComponent(jButton1)
.addGap(33, 33, 33))
);
layout.setVerticalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(0, 745, Short.MAX_VALUE)
.addComponent(jButton1)
.addGap(237, 237, 237))
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addContainerGap())))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(ActionEvent evt) {
Graphics g = jPanel1.getGraphics();
draw(jPanel1, g);
}
protected void paintComponent(Graphics g){
jPanel1.setSize(1000, 1000);
Dimension d = jPanel1.getSize();
g.setColor(Color.BLACK);
for (int i=0; i<=1000;i++){
if (i%100==0){
g.setColor(Color.RED);
g.drawLine(i, d.height/2, i, (d.height/2)+100);
}
else if(i%50==0 && i%100!=0){
g.setColor(Color.BLUE);
g.drawLine(i, d.height/2, i, (d.height/2)+100);
}
else {
g.setColor(Color.BLACK);
g.drawLine(i, d.height/2, i, (d.height/2)+100);
}
}
g.setColor(Color.green);
g.drawLine(0, d.height / 2, d.width, d.height / 2);
}
public void draw(JPanel Jpanel1, Graphics g) {
System.out.println("wuffkowski");
Jpanel1.setSize(1000,1000);
Dimension d = Jpanel1.getSize();
g.setColor(Color.BLACK);
for (int i=0; i<=1000;i++){
if (i%100==0){
g.setColor(Color.RED);
g.drawLine(i, d.height/2, i, (d.height/2)+100);
}
else if(i%50==0 && i%100!=0){
g.setColor(Color.BLUE);
g.drawLine(i, d.height/2, i, (d.height/2)+100);
}
else {
g.setColor(Color.BLACK);
g.drawLine(i, d.height/2, i, (d.height/2)+100);
}
}
g.setColor(Color.green);
g.drawLine(0, d.height / 2, d.width, d.height / 2);
Jpanel1.paintComponents(g);
}
public static void lala () {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
NewJFrame JF =new NewJFrame();
JF.setVisible(true);
}
});
}
private javax.swing.JButton jButton1;
private javax.swing.JPanel jPanel1;
}
再次感谢您宝贵的时间。