用NetBeans编写界面画图程序的问题,望指教 我想用NetBeans编写界面画图程序,用户可在文本框中输入参数,点画图按钮后,程序会根据参数在面板上绘制矩形。绘图一般是在自定义的JPanel的派生类中重载paintComponent(Graphics comp)函数,可按钮和文本框在框架上而不在这个面板上, 想请教高手:1 怎样将用户输入的参数传给这个函数?2 怎样把这个面板的类型改为我定义的派生类名,而不是从组件面板中拖动过来后默认的JPanel类 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 如果你要自定义JPanel的派生类中重载paintComponent(Graphics comp)函数,就不可以直接定义paintComponent(Graphics comp,int x ,int y) 之类的函数;因为这样就不可以重载了;所以这里我是在JPanel的派生类MyPanel中定义属性 int x;int y;int width;int hieght;利用属性的get和set的方法来实现参数的传递;具体程序:MyPanel类:import java.awt.*;import javax.swing.*;public class MyPanel extends JPanel { private int x; private int y; private int height; private int width; public MyPanel(int x, int y, int width, int height) { this.x = x; this.y = y; this.width = width; this.height = height; } public void setheigth(int heigth) { this.height = heigth; } public void setwidth(int width) { this.width = width; } public void setx(int x) { this.x = x; } public void sety(int y) { this.y = y; } public int getx() { return this.x; } public int gety() { return this.y; } public int getheight() { return height; } public int getwidth() { return width; } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); g.drawRect(x, y, width, height); //g.drawr }} 实现窗体:import java.awt.*;import javax.swing.*;import java.awt.event.*;public class RectJFrame extends javax.swing.JFrame { public RectJFrame() { initComponents(); jp = new MyPanel(0, 0, 0, 0); jPanel1.add(jp); } @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//////////////////因为字数太多啦 唯有把设计窗体的函数删了 private void initComponents() {}private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {// TODO add your handling code here: //repaint(); Graphics g = jPanel1.getGraphics(); jp.setx(Integer.parseInt(jTextField1.getText())); jp.sety(Integer.parseInt(jTextField2.getText())); jp.setwidth(Integer.parseInt(jTextField3.getText())); jp.setheigth(Integer.parseInt(jTextField4.getText())); System.out.println(jp.getx() + " " + jp.gety() + " " + jp.getwidth() + " " + jp.getheight()); jp.paintComponent(g);}private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {// TODO add your handling code here:} /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new RectJFrame().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JPanel jPanel1; private javax.swing.JTextField jTextField1; private javax.swing.JTextField jTextField2; private javax.swing.JTextField jTextField3; private javax.swing.JTextField jTextField4; // End of variables declaration private MyPanel jp;} 问一个面对对象中的问题,在线等 看一下,这个是聊天的server端,一个地方,我不知道为什么错 java如何获得当前的网络传输速度 强制类型转换问题 这么写为什么产生不了死锁>? 一道编程题 (100呀)熟悉正则替换的请进:gm、gs、gmi这些是什么意思?(有小例子!) JavaAPI查找工具JawaAPIHelper发布了,这下学习查找API方便了。 Jave 真的什么都能做吗?它有没有Delphi 方便,我说的是搞数据库方面 如果没有指针,如何动态生成新的对象? this这个代表什么? 求助关于awt包,多谢
就不可以直接定义paintComponent(Graphics comp,int x ,int y) 之类的函数;
因为这样就不可以重载了;
所以这里我是在JPanel的派生类MyPanel中定义属性 int x;int y;int width;int hieght;
利用属性的get和set的方法来实现参数的传递;具体程序:
MyPanel类:
import java.awt.*;
import javax.swing.*;public class MyPanel extends JPanel { private int x;
private int y;
private int height;
private int width; public MyPanel(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
} public void setheigth(int heigth) {
this.height = heigth;
} public void setwidth(int width) {
this.width = width;
} public void setx(int x) {
this.x = x;
} public void sety(int y) {
this.y = y;
} public int getx() {
return this.x;
} public int gety() {
return this.y;
} public int getheight() {
return height;
} public int getwidth() {
return width;
} @Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawRect(x, y, width, height);
//g.drawr
}
}
import javax.swing.*;
import java.awt.event.*;public class RectJFrame extends javax.swing.JFrame { public RectJFrame() {
initComponents();
jp = new MyPanel(0, 0, 0, 0);
jPanel1.add(jp);
} @SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
//////////////////因为字数太多啦 唯有把设计窗体的函数删了
private void initComponents() {}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//repaint();
Graphics g = jPanel1.getGraphics();
jp.setx(Integer.parseInt(jTextField1.getText()));
jp.sety(Integer.parseInt(jTextField2.getText()));
jp.setwidth(Integer.parseInt(jTextField3.getText()));
jp.setheigth(Integer.parseInt(jTextField4.getText()));
System.out.println(jp.getx() + " " + jp.gety() + " " + jp.getwidth() + " " + jp.getheight());
jp.paintComponent(g);
}private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
} /**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new RectJFrame().setVisible(true);
}
});
} // Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JPanel jPanel1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
private javax.swing.JTextField jTextField4;
// End of variables declaration
private MyPanel jp;
}