Java help ////// How do i turn my frame into a JFrame then add a JPanel object to the CENTER of the JFrame object? When

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Java help ////// How do i turn my frame into a JFrame then add a JPanel object to the CENTER of the JFrame object? When

Post by answerhappygod »

Java help
//////
How do i turn my frame into a JFrame then add aJPanel object to the CENTER ofthe JFrame object?
When you run this the top left corner should start at 0,0 rightnow it starts at 0,38
**every time you click the old number should disappear**
///////
/*File Proj07Runner.java
************************************************************/
import java.awt.*;import java.awt.event.*;import javax.swing.*;
class Proj07Runner { Proj07Runner() { // when you run main, display thetext System.out.println( "Terminal Text - Do notremove."); // when you run main it createsa new GUI object new GUI(); }}
// create class to display clicksclass MyFrame extends Frame { // create 2 ints for x and y int clickX; int clickY; // display to user the x and y points public void paint(Graphics g) { g.drawString(" " + clickX + ", " + clickY,clickX, clickY); } }
// create the GUIclass GUI { public GUI(){ // constructor - set up frame MyFrame frame = new MyFrame(); // set size, title, and visibility to frame that wasset up in previous line frame.setSize(300,100); frame.setTitle("Kassandra Tovar"); frame.setVisible(true);
// frame.setUndecorated(true); // window listent to allow users to closeframe frame.addWindowListener(new Close());
// mouse listener so we can print points for mouseclicks frame.addMouseListener(new MouseProc(frame));
} }
// monitors closing window and extends WindowAdapterclass Close extends WindowAdapter{ public void windowClosing(WindowEvent e){ System.exit(0); }}
// Monitor mouse clicks and gather x and y pointsclass MouseProc extends MouseAdapter { MyFrame reference; MouseProc(MyFrame winIn) { reference = winIn; } @Override public void mousePressed(MouseEvent e) { reference.clickX = e.getX(); reference.clickY = e.getY(); reference.repaint(); }}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply