我正在尝试在NetBeans生成的JFrame上显示super.paintComponent。但是,paintcomponent就不会在那里工作。我希望paintComponent显示的JFrame称为“GameFrame”。 BTW这个绘画类是在我自己编写的JFrame上工作的。 我也是java的新手。
这是我的代码:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package MainCode;
public class DisplayFrame extends javax.swing.JFrame {
/**
* Creates new form DisplayFrame
*/
public DisplayFrame() {
initComponents();
GameFrame.setVisible(true);
} /**
*
* 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")
_2D_Shapes_ Draw = new _2D_Shapes_();
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
GameFrame = new javax.swing.JFrame();
PlayButton = new javax.swing.JButton();
GameFrame.setExtendedState(MAXIMIZED_BOTH);
GameFrame.setUndecorated(true);
javax.swing.GroupLayout GameFrameLayout = new javax.swing.GroupLayout(GameFrame.getContentPane());
GameFrame.getContentPane().setLayout(GameFrameLayout);
GameFrameLayout.setHorizontalGroup(
GameFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 1086, Short.MAX_VALUE)
);
GameFrameLayout.setVerticalGroup(
GameFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 703, Short.MAX_VALUE)
);
GameFrame.setVisible(true);
setExtendedState(MAXIMIZED_BOTH);
PlayButton.setFont(new java.awt.Font("Al-seana", 0, 36)); // NOI18N
PlayButton.setText("PLAY!");
PlayButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
PlayButtonActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(PlayButton, javax.swing.GroupLayout.DEFAULT_SIZE, 467, Short.MAX_VALUE)
.addGap(0, 0, 0))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(216, Short.MAX_VALUE)
.addComponent(PlayButton, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE))
);
pack();
}// </editor-fold>
private void PlayButtonActionPerformed(java.awt.event.ActionEvent evt) {
GameFrame.setVisible(true);
GameFrame.add(Draw);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Windows".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(DisplayFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(DisplayFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(DisplayFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(DisplayFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new DisplayFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JFrame GameFrame;
private javax.swing.JButton PlayButton;
// End of variables declaration
}
注意:如果你看不到它,[GameFrame.add(Draw);]命令就在中间。