将图形无效扩展到Jframe?

时间:2018-03-15 07:00:14

标签: java graphic

我有Jframe Form并创建了从中绘制图形的类,为了做到这一点,我需要将该void类扩展为jframe格式

package grafiktest;

import java.awt.Color;
import java.awt.Graphics;


public class testoutput extends javax.swing.JFrame{
Graphics g;

public testoutput() {
    initComponents();
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    g=jPanel1.getGraphics();
test tod = new test(); 
tod.test();

}                                        

我创建的课程:

     public class test extends testoutput{
     void test(){ 
      testoutput tod = new testoutput(); 
      g=tod.jPanel1.getGraphics();
   g.setColor(Color.decode("#7D9AB3"));
    g.fillRect(3,3,3,3);
   }
    }

有什么办法可以扩展吗? 我是新手,谢谢

1 个答案:

答案 0 :(得分:1)

暂时忽略你的糟糕画作,你应该先仔细看看会为你提供更多信息的Inheritance trail

简而言之,不,这不是继承的工作原理。

我很欣赏这可能令人困惑,我强烈建议您将foreach从图片中删除并查看操作如何运作来简化您的示例

让我们从简单的例子开始......

trips.foreach((item) =>{
//here item is date1, date2
// then you can do 
item.id = 2; // every stuff you want !!!

还有一些儿童班

JFrame

现在,让我们来看看如果我们这样做会发生什么......

public class Parent {

    public Parent() {
        //...
    }

    public void performSomeAction() {
        System.out.println("I'm called from within parent");
    }
}

这输出......

public class Child extends Parent {

    @Override
    public void performSomeAction() {
        // Action overridden from parent
        // You can call super.performSomeAction()
        // if you wish to maintain the functionality the parent class was
        // performing as needed
        System.out.println("I'm called from within child");
    }

}

不是很令人兴奋,但你明白了。

但是,等一下,让我们改变Parent parent = new Parent(); Child child = new Child(); parent.performSomeAction(); child.performSomeAction();

I'm called from within parent
I'm called from within child

现在,如果我们重新运行测试代码,我们就会得到

Child

额外的"父母"来电来自!?它来自public class Child extends Parent { @Override public void performSomeAction() { super.performSomeAction() System.out.println("I'm called from within child"); } } ,这只是调用父方法 - 这就是如何重写方法可以用来扩展它的功能甚至完全覆盖(如第一个例子中所做的那样)

现在,让我们做一些非常奇怪的事情......

I'm called from within parent
I'm called from within parent
I'm called from within child

您认为输出是什么?!

我会留给你测试,但你可能还想看看Polymorphism并准备好

最后......去阅读Performing Custom PaintingPainting in AWT and Swing,以便更好地了解绘画是如何运作的,以及你需要做些什么才能使用它