Center buttons and TxtPane

0

I'm starting to use Eclipse to make a desktop application and I'm a bit of a fish.

I would like to know how I can find the way that when expanding the screen the rest of the objects also move. Is there any way to do it?

my code is as follows:

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import javax.swing.JToggleButton;
import javax.swing.JTextPane;
import java.awt.Font;
import java.awt.Color;
import java.awt.SystemColor;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.SwingConstants;

public class pantalla_frame extends JFrame {

    private JPanel contentPane;
    private JTextField txtUsuari2;
    private JTextField textField;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    pantalla_frame frame = new pantalla_frame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public pantalla_frame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 995, 670);
        contentPane = new JPanel();
        contentPane.setBackground(SystemColor.text);
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JTextPane txtUsuari = new JTextPane();
        txtUsuari.setText("USUARI");
        txtUsuari.setBackground(SystemColor.controlHighlight);
        txtUsuari.setFont(new Font("Tahoma", Font.PLAIN, 30));
        txtUsuari.setBounds(317, 71, 284, 96);
        txtUsuari.setEditable(false);       
        StyledDocument doc = txtUsuari.getStyledDocument();
        SimpleAttributeSet center = new SimpleAttributeSet();
        StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
        doc.setParagraphAttributes(0, doc.getLength(), center, false);
        contentPane.add(txtUsuari);

        txtUsuari2 = new JTextField();
        txtUsuari2.setHorizontalAlignment(SwingConstants.CENTER);
        txtUsuari2.setBounds(317, 178, 284, 38);
        contentPane.add(txtUsuari2);
        txtUsuari2.setColumns(10);

        JTextPane txtpnPassword = new JTextPane();
        txtpnPassword.setText("PASSWORD");
        txtpnPassword.setBackground(SystemColor.controlHighlight);
        txtpnPassword.setFont(new Font("Tahoma", Font.PLAIN, 30));
        txtpnPassword.setBounds(317, 227, 284, 96);
        txtpnPassword.setEditable(false); 
        contentPane.add(txtpnPassword);
        StyledDocument doc2 = txtpnPassword.getStyledDocument();
        SimpleAttributeSet center2 = new SimpleAttributeSet();
        StyleConstants.setAlignment(center2, StyleConstants.ALIGN_CENTER);
        doc2.setParagraphAttributes(0, doc2.getLength(), center2, false);
        contentPane.add(txtpnPassword);

        textField = new JTextField();
        textField.setHorizontalAlignment(SwingConstants.CENTER);
        textField.setBounds(317, 334, 284, 45);
        contentPane.add(textField);
        textField.setColumns(10);

        JButton btn_aceptar = new JButton("ACEPTAR");
        btn_aceptar.setFont(new Font("Tahoma", Font.PLAIN, 17));
        btn_aceptar.setBounds(317, 434, 284, 45);
        contentPane.add(btn_aceptar);

        JTextPane txtpnGamnificacioc = new JTextPane();
        txtpnGamnificacioc.setFont(new Font("Segoe Script", Font.PLAIN, 24));
        txtpnGamnificacioc.setText("GamnificacIOC");
        txtpnGamnificacioc.setBounds(0, 0, 208, 71);
        contentPane.add(txtpnGamnificacioc);
    }
}
    
asked by Montse Mkd 23.10.2018 в 23:16
source

1 answer

1

I try to explain a little better because my comment ...

I give the following example example:

We have a form of width 100 px, a Datagrid that starts at pixel 50 and width occupies 50 px.

If we want the datagrid to be enlarged, the following is done:

  

Datagrid.width = form.width - 50px

This has to be put in the resize method of the form. It would have to do the same with the height since with this code it would only grow in width.

It would be the same if what worries you, instead of width or height, is the position, instead of the width, you would have to modify the Left attribute instead of the width.

I hope you have explained me well.

    
answered by 24.10.2018 / 08:19
source