JPanels Textfields GBC.
-
26-12-2019 - |
Domanda
Non capisco perché i miei campi di testo stanno arrivando come fessure invece della dimensione data?C'è qualcosa che mi manca, o quello che sto pensando è il problema è che i vincoli che ho messo sono troppo piccoli? Grazie in anticipo!
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class addEntry
{
public void addEntryFrame()
{
JFrame entryFrame = new JFrame("Passafe");
entryFrame.setSize(500,500);
entryFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel topPanel = new JPanel();
JLabel topHeader = new JLabel("Add New Entry: ");
topHeader.setFont(new Font("Ariel", Font.BOLD, 24));
topPanel.add(topHeader);
JPanel centerPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(15,15,15,15);
JLabel name = new JLabel("Name: ");
JLabel username = new JLabel("Username: ");
JLabel password = new JLabel("Password: ");
JLabel description = new JLabel("Description: ");
JTextField nametf = new JTextField(20);
JTextField usernametf = new JTextField(20);
JTextField passwordtf = new JTextField(20);
JTextField descriptiontf = new JTextField(50);
gbc.gridx = 0;
gbc.gridy = 0;
centerPanel.add(name, gbc);
gbc.gridx = 1;
gbc.gridy = 0;
centerPanel.add(nametf, gbc);
gbc.gridx = 0;
gbc.gridy = 2;
centerPanel.add(username, gbc);
gbc.gridx = 1;
gbc.gridy = 2;
centerPanel.add(usernametf, gbc);
gbc.gridx = 0;
gbc.gridy = 3;
centerPanel.add(password, gbc);
gbc.gridx = 1;
gbc.gridy = 3;
centerPanel.add(passwordtf, gbc);
gbc.gridx = 0;
gbc.gridy = 4;
centerPanel.add(description, gbc);
gbc.gridx = 1;
gbc.gridy = 4;
centerPanel.add(descriptiontf, gbc);
JPanel bottomPanel = new JPanel();
JButton saveEntryButton = new JButton("SAVE");
bottomPanel.add(saveEntryButton);
entryFrame.add(topPanel, BorderLayout.NORTH);
entryFrame.add(centerPanel, BorderLayout.CENTER);
entryFrame.add(bottomPanel, BorderLayout.SOUTH);
entryFrame.setVisible(true);
}
}
. Soluzione
Non utilizzare entryFrame.setSize(500, 500);
anziché tale utilizzo entryFrame.pack();
alla fine del metodo addEntryFrame()
.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow