Results 1 to 12 of 12
Thread: JPanels and JFrames
- 01-14-2011, 06:30 AM #1
Member
- Join Date
- Sep 2010
- Posts
- 18
- Rep Power
- 0
JPanels and JFrames
I have trouble displaying the Underlying class onto the main frame.
It works fine with my DnD class, any solutions? Here are codes below
DnD.java codes:
MainFrame.java code:Java Code:public class DnD extends JPanel { static String imageFile = "bgfinal.png"; // @jve:decl-index=0: private static final long serialVersionUID = 1L; private BufferedImage buffImg2; // @jve:decl-index=0: private JLabel jLabelderivative = null; private JLabel jLabelfutures = null; private JLabel jLabellong = null; private JLabel jLabeloptions = null; private JLabel jLabelshort = null; private JLabel jLabelsinglestockfutures = null; private JLabel jLabelstockindex = null; private JLabel jLabelstockindexfutures = null; /** * This is the default constructor */ public DnD() { super(); initialize(); } /** * This method initializes this * @return * @return * * @return void */ private void initialize() { String filePath = getClass().getResource("images").toString(); filePath = filePath.substring(6); File f = new File(filePath + "/bgfinal" + ".png"); try { buffImg2 = ImageIO.read(f); } catch (IOException e2) { e2.printStackTrace(); } this.setSize(720, 480); this.setLayout(null); this.setBackground(Color.white); } public void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(buffImg2, 0, 0, null); }//end paintComponent } // @jve:decl-index=0:visual-constraint="16,7"
Underlying.java code:Java Code:public class MainFrame extends JFrame { private static final long serialVersionUID = 1L; private JPanel jContentPane = null; /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub SwingUtilities.invokeLater(new Runnable() { public void run() { MainFrame thisClass = new MainFrame(); thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); thisClass.setVisible(true); } }); } /** * This is the default constructor */ public MainFrame() { super(); initialize(); JPanel DnD = new DnD(); JPanel Underlying = new Underlying(); this.getContentPane().add(DnD, Underlying); this.setVisible(true); } /** * This method initializes this * * @return void */ private void initialize() { this.setSize(720, 480); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setContentPane(getJContentPane()); this.setTitle("Drag and Drop"); this.setVisible(true); } /** * This method initializes jContentPane * * @return javax.swing.JPanel */ private JPanel getJContentPane() { if (jContentPane == null) { jContentPane = new JPanel(); jContentPane.setLayout(null); } return jContentPane; } }
Java Code:public class Underlying extends JPanel implements MouseListener, MouseMotionListener { static String imageFile = "underlyingtest.png"; // @jve:decl-index=0: private static final int w = 84; private static final int h = 15; private BufferedImage buffImg2; private boolean _canDrag = false; private int _labelX = 505; // x coord - set from drag private int _labelY = 77; // y coord - set from drag private int _dragFromX = 0; private int _dragFromY = 0; public Underlying() { super(); initialize(); this.addMouseListener(this); this.addMouseMotionListener(this); } private void initialize() { String filePath = getClass().getResource("images").toString(); filePath = filePath.substring(6); File f = new File(filePath + "/underlyingtest" + ".png"); try { buffImg2 = ImageIO.read(f); } catch (IOException e2) { e2.printStackTrace(); } } public void paintComponent(Graphics g) { super.paintComponent(g); // Required for background. g.drawImage(buffImg2, _labelX, _labelY, w, h, null); }//end paintComponent @Override public void mouseDragged(MouseEvent e) { if (_canDrag) { // True only if button was pressed inside label. //--- label pos from mouse and original click displacement _labelX = e.getX() - _dragFromX; _labelY = e.getY() - _dragFromY; //--- Don't move the label off the screen sides _labelX = Math.max(_labelX, 0); _labelX = Math.min(_labelX, getWidth() - w); //--- Don't move the label off top or bottom _labelY = Math.max(_labelY, 0); _labelY = Math.min(_labelY, getHeight() - h); this.repaint(); } } @Override public void mouseMoved(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { _canDrag = false; } @Override public void mousePressed(MouseEvent e) { int x = e.getX(); // Save the x coord of the click int y = e.getY(); // Save the y coord of the click if (x >= _labelX && x <= (_labelX + w) && y >= _labelY && y <= (_labelY + h)) { _canDrag = true; _dragFromX = x - _labelX; // how far from left _dragFromY = y - _labelY; // how far from top } else { _canDrag = false; } } @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } }
-
Hopefully you'll get an answer soon, but if not, you may wish to place debugging statements in your code to try to isolate the problem, and then failing that, create a much smaller, compilable program that demonstrates the problem because you're currently asking us to go through a bit of code without showing us where to look. Luck.
- 01-14-2011, 12:59 PM #3
Member
- Join Date
- Sep 2010
- Posts
- 18
- Rep Power
- 0
Basically, it's under the mainframe for i cant get the image from underlying to show up. do i have to use a layeredpane? or is there any way i could overlap the 2 panels(DnD and underlying)?
-
This confuses the heck out of me, and I'm surprised that it even compiles:
What are you trying to do here and where have you seen this done like this before?Java Code:JPanel DnD = new DnD(); JPanel Underlying = new Underlying(); this.getContentPane().add(DnD, Underlying);
- 01-14-2011, 01:35 PM #5
Member
- Join Date
- Sep 2010
- Posts
- 18
- Rep Power
- 0
it compiles, i was doing it by trial and error, how should i go about it then? since i'm not using array list to store the images, is there any way i could overlap these 2 panels and display them onto JFrame at the same time? the image i used in underlying.java is just a small rectangle. the image in DnD however is the background.
-
Ah, I see now why it compiles. Container has an add method that takes a Component and an Object as parameters, the Object which should be a constraint of some sort for the add method. So the compiler thinks that your second parameter is the constraint, which it obviously is not.
This will never work when trying to code a complex library such as Swing. You need to study the tutorials and the API to see how things are done, not guess.i was doing it by trial and error,
First off, you must add components one at a time in your container, and the tutorials will explain this to you. One way to overlap JPanels by adding them to a JLayeredPane, another is by adding them to a container that uses null layout, and for both you'd need to specify the size and position of the components. I favor the former since JLayeredPanes have nice abilities for moving components about in the different layers.how should i go about it then? since i'm not using array list to store the images, is there any way i could overlap these 2 panels and display them onto JFrame at the same time? the image i used in underlying.java is just a small rectangle. the image in DnD however is the background.
-
Edit: if all you want is to have a background image for your GUI, then ignore all of this as there are much simpler ways of doing this. Google JPanel background image or search this forum for many examples of this.
- 01-14-2011, 01:48 PM #8
Member
- Join Date
- Sep 2010
- Posts
- 18
- Rep Power
- 0
I felt quite restricted as well, my college project limits me 1 month to create an interactive platform with java. In addition, they have not given really little information on java GUI. My teammates and I have been doing our own research all these while. My bad for not learning the fundamentals right. However, it is interesting for me and i hope in the near future i could really get them right.This will never work when trying to code a complex library such as Swing. You need to study the tutorials and the API to see how things are done, not guess.
Thanks for advice, i'll work my way aroundFirst off, you must add components one at a time in your container, and the tutorials will explain this to you. One way to overlap JPanels by adding them to a JLayeredPane, another is by adding them to a container that uses null layout, and for both you'd need to specify the size and position of the components. I favor the former since JLayeredPanes have nice abilities for moving components about in the different layers.
- 01-14-2011, 01:49 PM #9
Member
- Join Date
- Sep 2010
- Posts
- 18
- Rep Power
- 0
ignore the "not"In addition, they have not given really little information on java GUI.
- 01-16-2011, 03:36 PM #10
Member
- Join Date
- Sep 2010
- Posts
- 18
- Rep Power
- 0
Fubarable are you there? i need some help
-
Just ask your question whether I'm here or not. But if it is a new question, you may wish to start a new thread.
- 01-16-2011, 03:39 PM #12
Member
- Join Date
- Sep 2010
- Posts
- 18
- Rep Power
- 0
Similar Threads
-
need help with jframes
By glomic in forum New To JavaReplies: 13Last Post: 01-08-2011, 11:16 AM -
JFrames and JPanels with ActionListeners
By atomant in forum New To JavaReplies: 3Last Post: 11-30-2010, 07:26 PM -
Problems regarding JPanels in JPanels
By ColtonPhillips in forum AWT / SwingReplies: 2Last Post: 07-19-2010, 08:33 PM -
two JFrames
By kirtesh4u in forum New To JavaReplies: 0Last Post: 11-17-2008, 08:26 PM -
Adding JPanels to JFrames based on x-y co-ordinates
By aneesahamedaa in forum AWT / SwingReplies: 4Last Post: 07-24-2008, 10:41 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks