简体   繁体   English

在扩展JFrame的类中的另一个JFrame上绘制一条线

[英]Drawing a line on another JFrame in a class that extends JFrame

I have a class with two JFrames and am trying to draw a line on a particular frame . 我有一个有两个JFrame的类,我试图在特定的帧上绘制一条线。

I tried the code below but it only appears in the first frame which is the success frame. 我尝试了下面的代码,但它只出现在第一帧,即成功帧。

It also appears above all the other components of the success frame thus making all other 它也出现在成功框架的所有其他组件之上,从而使其他所有组件成为可能

components invisible. 组件不可见。 It does not appear in the comp Frame. 它没有出现在comp框架中。

How do I correct this. 我该如何纠正这个问题。

Here is the code I have so far : 这是我到目前为止的代码:

import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;

public class lineGUI{
public static void main(String []args){
Success s=new Success();
s.setVisible(true);
  }
}

class Success extends JFrame{

JPanel alas =new JPanel();
JFrame comp =new JFrame();
public Success(){
JPanel panel=new JPanel();
getContentPane().add(panel);
setSize(450,450);

JButton button =new JButton("press");
panel.add(button);

  comp.setSize(650,500);
  comp.setTitle("View Report");

  JRootPane compPane=comp.getRootPane();
  Container contePane=compPane.getContentPane();
  contePane.add(alas);


    ActionListener action =new ActionListener(){
      public void actionPerformed(ActionEvent e){
        if (e.getSource()==button){
          comp.setVisible(true);
        }
      }
    };
    button.addActionListener(action);

  JButton button2=new JButton("access");
  alas.add(button2);
 }

public void paint(Graphics g) {
comp.paint(g);
Graphics2D g2 = (Graphics2D) g;
Line2D lin = new Line2D.Float(100, 100, 250, 260);
g2.draw(lin);
  }
}

You've got some crazy code up there. 你那里有一些疯狂的代码。 Suggestions: 建议:

  • Don't draw directly in a JFrame, but in a the paintComponent method of an object derived from JComponent such as JPanel or JComponent itself. 不要直接在JFrame中绘制,而是在从JComponent派生的对象的paintComponent方法中绘制,例如JPanel或JComponent本身。
  • Your drawing directly in another component's paint(...) method is not kosher at all. 直接在另一个组件的paint(...)方法中绘制的图形根本不是犹太图像。 Why not simply share the data between classes, and use the data (the ints) to draw where desired. 为什么不简单地在类之间共享数据,并使用数据(int)来绘制所需的位置。
  • You would rarely want to have a GUI display more than one JFrame at a time. 您很少希望GUI一次显示多个JFrame。 Usually one window is the main window (the JFrame), and it often owns any other windows which would be dialog windows such as JDialogs. 通常一个窗口是主窗口(JFrame),它通常拥有任何其他窗口,这些窗口可以是对话窗口,例如JDialogs。
  • Read the graphics tutorials to learn the correct way to do Swing Graphics 阅读图形教程,了解正确的Swing Graphics方法

Two things: 两件事情:

If you want to draw in the "comp" frame, then you should extend that frame explicitly to overload its paint method. 如果要绘制“comp”框架,则应明确扩展该框架以重载其paint方法。 Right now you're overloading the paint method of "Success" frame. 现在你正在重载“成功”框架的绘制方法。 The line comp.paint(g) is using the paint method of comp (a standard JFrame) to draw on the Graphics object of the "Success" frame. comp.paint(g)行使用comp(标准JFrame)的paint方法绘制“Success”框架的Graphics对象。 You probably want to make that into super.paint(g) instead, then put this paint function into it's own JFrame and create comp from that. 您可能希望将其转换为super.paint(g) ,然后将此paint函数放入其自己的JFrame中并从中创建comp。

http://pastebin.com/ZLYBHpmj http://pastebin.com/ZLYBHpmj

(Sorry, first post, couldn't figure out how to get Stackoverflow to quit complaining about format) (对不起,第一篇文章,无法弄清楚如何让Stackoverflow退出抱怨格式)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM