使用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);
}
}
答案 0 :(得分:0)
这是在JPanel
Frame
上绘制椭圆的简单代码
import javax.swing.*;
import java.awt.*;
public class Test extends JFrame
{
public Test ()
{
setSize (300,300);
JPanel panel=new JPanel (){
@Override
public void paint (Graphics g){
g.setColor (Color.BLUE);
g.fillOval(100, 100, 100, 100);
}
};
add(panel);
setVisible (true);
}
public static void main (String args[]){
new Test();
}
}