【发布时间】:2015-05-04 14:29:34
【问题描述】:
在使用 Netbeans 8.0.2 开始使用 Java 2D 时遇到问题。如果有人可以为带有面板和简单形状的框架发布一些非常基本的代码,将不胜感激。
我经历了很多教程,覆盖了paintcomponent()、super.paintcomponent() 等,但我真的不确定如何或实际调用此方法,因为它没有在我的代码中被调用:(
package guitest2;
import java.awt.Color;
import java.awt.Graphics;
public class TablePanel extends javax.swing.JPanel {
public TablePanel() {
initComponents();
this.setSize(50, 50);
}
/**
* 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>
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.black);
g.drawRect(100, 100, 100, 100);
}
}
package guitest2;
public class TableFrame extends javax.swing.JFrame {
public TableFrame() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 529, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 393, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
/**
* @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 ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(TableFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(TableFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(TableFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(TableFrame.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 TableFrame().setVisible(true);
}
});
}
}
package guitest2;
import javax.swing.JFrame;
public class GUITest2 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
TableFrame tf = new TableFrame();
tf.setTitle("BJ");
tf.setSize(1200,800);
tf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
TablePanel p = new TablePanel();
tf.setVisible(true);
tf.add(p);
}
}
【问题讨论】:
-
您应该发布不起作用的代码以显示您尝试过的内容。
-
“它没有在我的代码中被调用”,但是你的代码在哪里?发布您的代码或解释您的尝试。
-
@Noob999 :您的项目中有两个
public static void main(String[] args) {。将一个内容放在另一个上并删除这个main ...。
标签: netbeans jframe jpanel 2d paintcomponent