Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8009647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:30:27+00:00 2026-06-04T18:30:27+00:00

I have a class with two JFrames and am trying to draw a line

  • 0

I have a class with two JFrames and am trying to draw a line on a particular frame .

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.

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);
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T18:30:30+00:00Added an answer on June 4, 2026 at 6:30 pm

    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.
    • Your drawing directly in another component’s paint(…) method is not kosher at all. Why not simply share the data between classes, and use the data (the ints) to draw where desired.
    • You would rarely want to have a GUI display more than one JFrame at a time. Usually one window is the main window (the JFrame), and it often owns any other windows which would be dialog windows such as JDialogs.
    • Read the graphics tutorials to learn the correct way to do Swing Graphics
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The code below plots some simple x-y data, but it has two problems that
I have a class with two constructors, one that takes no arguments and one
I have a class with two constructors that look like this: public MyClass(SomeOtherClass source)
Given I have a class with two constructors: public class TestClass { ObjectOne o1;
I'm have a class with two methods, A QuickSort() and a ReverseArray(). When I
I'd like to have a class with two array subscript operator overloads: one used
I have an example class containing two data points: public enum Sort { First,
I have a class A containing two pointers to objects of another class B.
I have a 'Contact' class with two properties : firstName and lastName. When I
I have a class which has two HashSet<String> collections as private members. Other classes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.