elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Los 10 CVE más críticos (peligrosos) de 2020


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  Java
| | | |-+  expandir jframe?
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: expandir jframe?  (Leído 2,384 veces)
owen

Desconectado Desconectado

Mensajes: 12


Ver Perfil
expandir jframe?
« en: 13 Febrero 2016, 05:21 am »

hola a todos, miren tengo un problema cuando tengo un jframe supongamos de 300x300 todo lo que esta en su interior se ve tal como lo deje (en su posición) pero al expandirlo todo se me va ala esquina superior izquierda y lo que sobra queda gris. como puedo hacerlo para que todo se adapte según la pantalla? y no se desforme el jframe


En línea

0xFer


Desconectado Desconectado

Mensajes: 400



Ver Perfil
Re: expandir jframe?
« Respuesta #1 en: 13 Febrero 2016, 16:46 pm »

Pues la forma más fácil que conozco es usando layout, te te dejo un ejemplo sacado de stackoverflow

Código
  1. import java.awt.BorderLayout;
  2. import java.awt.Color;
  3. import java.awt.Dimension;
  4. import java.awt.Graphics;
  5. import java.awt.GridBagConstraints;
  6. import java.awt.GridBagLayout;
  7. import java.awt.TextField;
  8. import java.awt.event.MouseEvent;
  9. import java.awt.event.MouseListener;
  10. import java.net.MalformedURLException;
  11. import java.net.URL;
  12.  
  13. import javax.swing.ImageIcon;
  14. import javax.swing.JFrame;
  15. import javax.swing.JPanel;
  16. import javax.swing.JScrollPane;
  17. import javax.swing.JTextPane;
  18. import javax.swing.SwingUtilities;
  19. import javax.swing.text.DefaultStyledDocument;
  20. import javax.swing.text.StyledDocument;
  21.  
  22. public class DraftGUI implements MouseListener {
  23.  
  24.    private static final String IMAGE_URL = "http://images.paramountbusinessjets.com/space/spaceshuttle.jpg";
  25.    private JPanel jpPack;
  26.    private JPanel jpCards;
  27.    private JPanel jpInfo;
  28.    private JPanel jpChat;
  29.    private TextField tf;
  30.    private StyledDocument doc;
  31.    private JTextPane tp;
  32.    private JScrollPane sp;
  33.  
  34.    @Override
  35.    public void mousePressed(MouseEvent e) {
  36.    }
  37.  
  38.    @Override
  39.    public void mouseReleased(MouseEvent e) {
  40.    }
  41.  
  42.    @Override
  43.    public void mouseEntered(MouseEvent e) {
  44.    }
  45.  
  46.    @Override
  47.    public void mouseExited(MouseEvent e) {
  48.    }
  49.  
  50.    @Override
  51.    public void mouseClicked(MouseEvent e) {
  52.    }
  53.  
  54.    public DraftGUI() throws MalformedURLException {
  55.  
  56.        final JFrame frame = new JFrame("Draft");
  57.        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  58.        // set the content pane, we'll add everything to it and then add it to the frame
  59.        JPanel contentPane = new JPanel();
  60.        contentPane.setLayout(new GridBagLayout());
  61.  
  62.        // creates some panels with some default values for now
  63.        JPanel jpCards = new JPanel(new BorderLayout());
  64.        jpCards.setBackground(Color.BLUE);
  65.  
  66.        JPanel jpInfo = new JPanel();
  67.        jpInfo.setBackground(Color.GREEN);
  68.  
  69.        JPanel jpPack = new JPanel(new GridBagLayout());
  70.        jpPack.setBackground(Color.RED);
  71.  
  72.        // grab some info to make the JTextPane and make it scroll
  73.        tf = new TextField();
  74.        doc = new DefaultStyledDocument();
  75.        tp = new JTextPane(doc);
  76.        tp.setEditable(false);
  77.        sp = new JScrollPane(tp);
  78.  
  79.        // adding the quantities to the chat panel
  80.        JPanel jpChat = new JPanel();
  81.        jpChat.setLayout(new BorderLayout());
  82.        jpChat.add("North", tf);
  83.        jpChat.add("Center", sp);
  84.  
  85.        // a new GridBagConstraints used to set the properties/location of the panels
  86.        GridBagConstraints c = new GridBagConstraints();
  87.  
  88.        // adding some panels to the content pane
  89.        // set it to start from the top left of the quadrant if it's too small
  90.        c.anchor = GridBagConstraints.FIRST_LINE_START;
  91.        c.fill = GridBagConstraints.BOTH; // set it to fill both vertically and horizontally
  92.        c.gridx = 0; // set it to quadrant x=0 and
  93.        c.gridy = 0; // set it to quadrant y=0
  94.        c.weightx = 0.7;
  95.        c.weighty = 0.3;
  96.        contentPane.add(jpCards, c);
  97.  
  98.        c.gridx = 1;
  99.        c.gridy = 0;
  100.        c.weightx = 0.3;
  101.        c.weighty = 0.3;
  102.        contentPane.add(jpInfo, c);
  103.  
  104.        c.gridx = 0;
  105.        c.gridy = 1;
  106.        c.weightx = 0.7;
  107.        c.weighty = 0.7;
  108.        contentPane.add(jpPack, c);
  109.  
  110.        c.gridx = 1;
  111.        c.gridy = 1;
  112.        c.weightx = 0.3;
  113.        c.weighty = 0.7;
  114.        contentPane.add(jpChat, c);
  115.  
  116.        // set some necessary values
  117.        frame.setContentPane(contentPane);
  118.        frame.setLocationByPlatform(true);
  119.  
  120.        // This code works for adding an Image
  121.        // need to learn how to specify a path not dependent on the specific users's machine
  122.        // this is not a high priority for now though
  123.        GridBagConstraints d = new GridBagConstraints();
  124.        d.weightx = 1.0;
  125.        d.weighty = 1.0;
  126.        d.fill = GridBagConstraints.BOTH;
  127.        d.gridx = 0;
  128.        d.gridy = 0;
  129.        ImageLabel imageLabel1 = new ImageLabel(new ImageIcon(new URL(IMAGE_URL)));
  130.        jpPack.add(imageLabel1, d);
  131.        ImageLabel imageLabel2 = new ImageLabel(new ImageIcon(new URL(IMAGE_URL)));
  132.        d.gridx++;
  133.        jpPack.add(imageLabel2, d);
  134.  
  135.        ImageLabel imageLabel3 = new ImageLabel(new ImageIcon(new URL(IMAGE_URL)));
  136.        d.gridy++;
  137.        jpPack.add(imageLabel3, d);
  138.  
  139.        imageLabel1.addMouseListener(this);
  140.        imageLabel2.addMouseListener(this);
  141.        imageLabel3.addMouseListener(this);
  142.        frame.pack();
  143.        // 223, 310 are the aspect values for a card image, width, height
  144.        // these need to be maintained as the GUI size changes
  145.            frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
  146.        frame.setVisible(true);
  147.    }
  148.  
  149.    public static void main(String[] args) {
  150.        SwingUtilities.invokeLater(new Runnable() {
  151.            @Override
  152.            public void run() {
  153.                try {
  154.                    new DraftGUI();
  155.                } catch (MalformedURLException e) {
  156.                    e.printStackTrace();
  157.                }
  158.            }
  159.        });
  160.    }
  161.  
  162.    public static class ImageLabel extends JPanel {
  163.        private static int counter = 1;
  164.        private ImageIcon icon;
  165.        private int id = counter++;
  166.  
  167.        public ImageLabel(ImageIcon icon) {
  168.            super();
  169.            setOpaque(false);
  170.            this.icon = icon;
  171.        }
  172.  
  173.        @Override
  174.        public Dimension getPreferredSize() {
  175.            return new Dimension(icon.getIconWidth(), icon.getIconHeight());
  176.        }
  177.  
  178.        @Override
  179.        protected void paintComponent(Graphics g) {
  180.            super.paintComponent(g);
  181.            double zoom = Math.min((double) getWidth() / icon.getIconWidth(), (double) getHeight() / icon.getIconHeight());
  182.            int width = (int) (zoom * icon.getIconWidth());
  183.            int height = (int) (zoom * icon.getIconHeight());
  184.            g.drawImage(icon.getImage(), (getWidth() - width) / 2, (getHeight() - height) / 2, width, height, this);
  185.            g.setFont(g.getFont().deriveFont(36.0f));
  186.            g.drawString(String.valueOf(id), getWidth() / 2, getHeight() / 2);
  187.        }
  188.    }
  189. }

la otra forma es que sepas el tamaño del Frame en tiempo de ejecución y dependiendo de ese tamaño cambiar el tamaño y la ubicación de todos los componentes, pero para eso hay que hacer muchos cálculos a mano.


« Última modificación: 13 Febrero 2016, 16:48 pm por 0xFer » En línea

Código
  1. int getRandomNumber(){
  2.    return 4; //chosen by fair dice roll
  3.              //guaranteed to be random
  4. }
owen

Desconectado Desconectado

Mensajes: 12


Ver Perfil
Re: expandir jframe?
« Respuesta #2 en: 21 Febrero 2016, 22:18 pm »

gracias por responder!
En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Expandir foros
Sugerencias y dudas sobre el Foro
JuszR 6 2,825 Último mensaje 9 Febrero 2006, 02:50 am
por JuszR
Expandir .bat en una red Local
Scripting
jaumem 2 3,357 Último mensaje 2 Febrero 2009, 14:33 pm
por Novlucker
Expandir matriz dinámica?
Programación C/C++
Metal-byte 2 5,717 Último mensaje 22 Enero 2012, 23:06 pm
por rir3760
Expandir Exe
Ingeniería Inversa
ClubIce 1 2,510 Último mensaje 29 Abril 2012, 10:00 am
por karmany
Ayuda expandir wifi ono
Hacking Wireless
JR18 5 9,307 Último mensaje 8 Abril 2014, 17:03 pm
por JR18
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines