2013-05-01 2 views
1

J'ai donc regardé à travers des tonnes de sujets à ce sujet, mais aucun ne semble résoudre mon problème! Lorsque j'essaie d'utiliser setBounds() ou setLocation() sur JLabel ou JButton, ça ne marche pas! Il semble placer les deux juste au hasard. Voici mon code:.setLocation et .setBounds ne fonctionnant pas sur JLabel! (et JButton)

public static void main(String[] args) { 
    final JFrame frame = new JFrame(TITLE); 
    final JLabel fpsLabel = new JLabel("FPS: ERROR"); 
    final JLabel fpsDone = new JLabel("FPS done: ERROR"); 
    final JPanel contentPanel = new JPanel(); 

    frame.setSize(WIDTH, HEIGHT); 
      frame.setContentPane(contentPanel); 
    frame.setLocationRelativeTo(null); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    fpsLabel.setLocation(WIDTH/2, HEIGHT/6); 
    fpsDone.setLocation(200, HEIGHT/6); 
    frame.setJMenuBar(menubar); 
    frame.setResizable(false); 
    frame.add(fpsLabel); 
    frame.add(fpsDone); 
    frame.setVisible(true); 
} 

Si besoin, je peux ajouter l'image.

SSCCE:

public static void main(String[] args) { 
      final JFrame frame = new JFrame("Example SSCCE"); 
      final JLabel fpsLabel = new JLabel("FPS: ERROR"); 
      final JLabel fpsDone = new JLabel("FPS done: ERROR"); 
      final JPanel contentPanel = new JPanel(); 
      final int HEIGHT = 400/16 * 9; 

      frame.setSize(400, HEIGHT); 
      frame.setContentPane(contentPanel); 
      fpsDone.setLocation(200, HEIGHT/2); 
      fpsLabel.setLocation(200, HEIGHT/2 + 50); 
      contentPanel.add(fpsDone); 
      contentPanel.add(fpsLabel); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      frame.setVisible(true); 
    } 

Ce que je veux qu'il ressemble (art ASCII?):

__________________________________ 
|        | 
|        | 
|        | 
|        | 
|        | 
|   FPS: ERROR   | 
|   FPS done: ERROR  | 
|        | 
|        | 
|        | 
|        | 
|        | 
__________________________________ 
+0

1) Pour être Pour obtenir de l'aide plus rapidement, postez un [SSCCE] (http://sscce.org/). 2) *** Utilisez les mises en page! *** –

+0

Avez-vous essayé de faire 'frame.setLayout (null);' ?? – Evans

+0

@Sanz Oui, j'ai essayé ça. – tambre

Répondre

4

Essayez de régler cet exemple besoin:

Single Column Frame

import java.awt.*; 
import javax.swing.*; 
import javax.swing.border.EmptyBorder; 

public class SingleColumnFrame { 

    public static void main(String[] args) { 
     Runnable r = new Runnable() { 

      @Override 
      public void run() { 
       // the GUI as seen by the user (without frame) 
       // the values of '20' are for gaps between components 
       JPanel gui = new JPanel(new GridLayout(0,1,20,20)); 
       // adjust numbers as required 
       gui.setBorder(new EmptyBorder(40, 200, 40, 200)); 

       gui.add(new JLabel("FPS: ERROR",SwingConstants.CENTER)); 
       gui.add(new JLabel("FPS done: ERROR",SwingConstants.CENTER)); 

       JFrame f = new JFrame("Demo"); 
       f.add(gui); 
       // Ensures JVM closes after frame(s) closed and 
       // all non-daemon threads are finished 
       f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
       // See http://stackoverflow.com/a/7143398/418556 for demo. 
       f.setLocationByPlatform(true); 

       // ensures the frame is the minimum size it needs to be 
       // in order display the components within it 
       f.pack(); 
       f.setResizable(false); 
       // should be done last, to avoid flickering, moving, 
       // resizing artifacts. 
       f.setVisible(true); 
      } 
     }; 
     // Swing GUIs should be created and updated on the EDT 
     // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html 
     SwingUtilities.invokeLater(r); 
    } 
} 
+0

Merci, cela fonctionne et a l'air vraiment sympa! – tambre

+2

De rien. Toujours heureux d'amener un programmeur du côté «brillant» de la force. :) –

Questions connexes