我似乎不能使用两个继承为我的java,扩展一个类在另一个类 [英] I can't seem to use two inheritance for my java, extending a class in another class

查看:214
本文介绍了我似乎不能使用两个继承为我的java,扩展一个类在另一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 包图; 

import java.awt.Graphics;
import java.awt.Polygon;
import javax.swing.JFrame;
import javax.swing.JPanel;

下面是我的类,覆盖所有其他类,并扩展JFrame, Netbeans中的new> Java Frame选项在一个名为Graph的包中创建,这个文件名是GraphFrame.Java。

  public class GraphFrame extends javax.swing.JFrame {


public GraphFrame(){

}
pre>

以下行来自我的JFrame自动创建代码。

  / ** 
*此方法从构造函数中调用以初始化窗体。
*警告:请勿修改此代码。此方法的内容总是
*由表单编辑器重新生成。
* /
@SuppressWarnings(unchecked)
//< editor-fold defaultstate =collapseddesc =Generated Code>

以下这些行是我的主要方法

  public static void main(String args []){


/ *创建并显示表单* /
// java .awt.EventQueue.invokeLater(new Runnable(){
// public void run(){
GraphFrame Frame = new GraphFrame();
Frame.setVisible(true);
Frame.setTitle(Functional Dependency);
Frame.setSize(500,500);

//}
//});
}



这些行是我的DrawGraph类,我在GraphFrame类中创建了INSIDE。除此之外,它扩展了我的JPanel,我通过拖动从摇摆容器的JPanel图标(对不起,如果这是错误的方式添加JPanel)

  class DrawGraph extends JPanel {
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);

g.drawLine(10,100,380,100);
g.drawLine(200,30,200,190);

g.drawLine(380,100,370,90);
g.drawLine(380,100,370,110);
g.drawLine(200,30,190,40);
g.drawLine(200,30,210,40);

g.drawString(X,360,80);
g.drawString(Y,220,40);




}

}
//变量声明 - 不要修改
private javax.swing .Button jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JPanel jPanel1;
//变量声明结束
}

我的问题是,的图我已经编码,没有出现。我有谷歌,StackOverflowed,浏览我的实验室练习,我似乎找不到这个解决方案。是的,这可能需要更多的阅读,但老实说,我不太明白是什么问题。

解决方案

问题是你永远不会将你的JPanel添加到你创建的框架。



这应该做你想做的。

  public class GraphFrame extends JFrame {

private DrawGraph graph;

public GraphFrame(){
super();
graph = new DrawGraph();
this.add(graph);
}
class DrawGraph extends JPanel {
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.drawLine(10,100,380,100);
g.drawLine(200,30,200,190);

g.drawLine(380,100,370,90);
g.drawLine(380,100,370,110);
g.drawLine(200,30,190,40);
g.drawLine(200,30,210,40);

g.drawString(X,360,80);
g.drawString(Y,220,40);
}
}
}

注意this.add graph)可以被调用,因为 GraphFrame 扩展JFrame。 .add()方法是JFrame类的一部分,因此可以从 GraphFrame



您还应该考虑为DrawGraph类创建一个新文件,因为如果您想向 GraphFrame或DrawGraph


package Graph;

import java.awt.Graphics;
import java.awt.Polygon;
import javax.swing.JFrame;
import javax.swing.JPanel;

Below here is my class which covers all other classes, and extends JFrame, which I have created by selecting "new > Java Frame" options in Netbeans.I created this in a package called Graph, and this filename is GraphFrame.Java.

public class GraphFrame extends javax.swing.JFrame {


public GraphFrame() {

}

The lines below is from my JFrame auto creating codes.

/**
 * 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">                          

These lines below here is my main method

public static void main(String args[]) {


    /* Create and display the form */
    //java.awt.EventQueue.invokeLater(new Runnable() {
        //public void run() {
            GraphFrame Frame=new GraphFrame();
            Frame.setVisible(true);
            Frame.setTitle("Functional Dependency");
            Frame.setSize(500, 500);

        //}
   // });
}

These lines is my DrawGraph class, which I created INSIDE the GraphFrame class. Besides that, it extends my JPanel which I made by dragging the JPanel icon from the Swing Containers (Sorry if that is the wrong way to add JPanel)

class DrawGraph extends JPanel{
@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    g.drawLine(10, 100, 380, 100);
    g.drawLine(200, 30, 200, 190);

    g.drawLine(380, 100, 370, 90);
    g.drawLine(380, 100, 370, 110);
    g.drawLine(200, 30, 190, 40);
    g.drawLine(200, 30, 210, 40);

    g.drawString("X", 360, 80);
    g.drawString("Y", 220, 40);




    }

}
// Variables declaration - do not modify                     
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JPanel jPanel1;
// End of variables declaration                   
}

My problem is, the lines of graph which I have coded, did not appear.I have googled, StackOverflowed, browsed through my lab exercises, and I can't seem to find the solution to this. Yes, this may need more reading, but to be honest, I couldn't quite understand what's wrong.

解决方案

The problem is that you never add your JPanel to the frame you created.

This should do what you want.

public class GraphFrame extends JFrame {

private DrawGraph graph;

public GraphFrame(){
    super();
    graph = new DrawGraph();
    this.add(graph);
}
class DrawGraph extends JPanel{
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawLine(10, 100, 380, 100);
        g.drawLine(200, 30, 200, 190);

        g.drawLine(380, 100, 370, 90);
        g.drawLine(380, 100, 370, 110);
        g.drawLine(200, 30, 190, 40);
        g.drawLine(200, 30, 210, 40);

        g.drawString("X", 360, 80);
        g.drawString("Y", 220, 40);
        }
    }
}

Note that this.add(graph) can be called because GraphFrame extends JFrame. The .add() method is a part of the JFrame class and can thus be called from GraphFrame

You should also consider creating a new file for the DrawGraph class as it can get messy if you want to add more functionality to either GraphFrame or DrawGraph.

这篇关于我似乎不能使用两个继承为我的java,扩展一个类在另一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆