Domanda

Ciao Grazie in anticipo per qualsiasi aiuto, sto cercando di costruire un programma semplice da imparare GUI, ma quando ho eseguito il codice qui sotto i miei JTextFields tutto show come una fessura quello non è abbastanza grande per anche un solo carattere.

sopraelevazione inviare un'immagine, ma sarebbe simile a: Label [|

dove [| è ciò che il campo di testo si presenta in realtà come

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


import javax.swing.*;



public class lab6start implements ActionListener
{
    JTextField custNameTxt;
    JTextField acctNumTxt;
    JTextField dateCreatedTxt;
    JButton checkingBtn;
    JButton savingsBtn;
    JTextField witAmountTxt;
    JButton withDrawBtn;
    JTextField depAmountTxt;
    JButton depositBtn;

    lab6start()
    {
        JFrame bankTeller = new JFrame("Welcome to Suchnsuch Bank");
        bankTeller.setSize(500, 280);
        bankTeller.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        bankTeller.setResizable(false);
        bankTeller.setLayout(new GridBagLayout());

        bankTeller.setBackground(Color.gray);

        //bankTeller.getContentPane().add(everything, BorderLayout.CENTER);

        GridBagConstraints c = new GridBagConstraints();

        JPanel acctInfo = new JPanel(new GridBagLayout());
        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 2;
        c.gridheight = 1;
        c.insets = new Insets(5,5,5,5);
        bankTeller.add(acctInfo, c);
        c.gridwidth = 1;

        //labels
        //name acct# balance interestRate dateCreated
        JLabel custNameLbl = new JLabel("Name");
        c.gridx = 0;
        c.gridy = 0;
        c.insets = new Insets(0,0,0,0);
        acctInfo.add(custNameLbl, c);

        custNameTxt = new JTextField("customer name",50);
        c.gridx = 1;
        c.gridy = 0;
        c.insets = new Insets(5,5,5,5);
        acctInfo.add(custNameTxt,c);
        custNameTxt.requestFocusInWindow();

        JLabel acctNumLbl = new JLabel("Account Number");
        c.gridx = 0;
        c.gridy = 1;
        c.insets = new Insets(5,5,5,5);
        acctInfo.add(acctNumLbl,c);

        acctNumTxt = new JTextField("account number");
        c.gridx = 1;
        c.gridy = 1;
        c.insets = new Insets(5,5,5,5);
        acctInfo.add(acctNumTxt,c);

        JLabel dateCreatedLbl = new JLabel("Date Created");
        c.gridx = 0;
        c.gridy = 2;
        c.insets = new Insets(5,5,5,5);
        acctInfo.add(dateCreatedLbl,c);

        dateCreatedTxt = new JTextField("date created");
        c.gridx = 1;
        c.gridy = 2;
        c.insets = new Insets(5,5,5,5);
        acctInfo.add(dateCreatedTxt,c);

        //buttons
        checkingBtn = new JButton("Checking");
        c.gridx = 0;
        c.gridy = 3;
        c.insets = new Insets(5,5,5,5);
        acctInfo.add(checkingBtn,c);

        savingsBtn = new JButton("Savings");
        c.gridx = 1;
        c.gridy = 3;
        c.insets = new Insets(5,5,5,5);
        acctInfo.add(savingsBtn,c);

//end of info panel

        JPanel withDraw = new JPanel(new GridBagLayout());
        c.gridx = 0;
        c.gridy = 1;
        c.insets = new Insets(5,5,5,5);
        bankTeller.add(withDraw, c);

        witAmountTxt = new JTextField("Amount to Withdraw:");
        c.gridx = 0;
        c.gridy = 0;
        c.insets = new Insets(5,5,5,5);
        withDraw.add(witAmountTxt,c);

        withDrawBtn = new JButton("Withdraw");
        c.gridx = 1;
        c.gridy = 0;
        c.insets = new Insets(5,5,5,5);
        withDraw.add(withDrawBtn,c);

        //add check balance

//end of withdraw panel

        JPanel deposit = new JPanel(new GridBagLayout());
        c.gridx = 1;
        c.gridy = 1;
        c.insets = new Insets(5,5,5,5);
        bankTeller.add(deposit, c);

        depAmountTxt = new JTextField("Amount to Deposit");
        c.gridx = 0;
        c.gridy = 0;
        c.insets = new Insets(5,5,5,5);
        deposit.add(depAmountTxt,c);

        depositBtn = new JButton("Deposit");
        c.gridx = 1;
        c.gridy = 0;
        c.insets = new Insets(5,5,5,5);
        deposit.add(depositBtn,c);      

        bankTeller.setVisible(true);

        // action/event 
        checkingBtn.addActionListener(this);
        savingsBtn.addActionListener(this);
        withDrawBtn.addActionListener(this);
        depositBtn.addActionListener(this);

    }

    public void actionPerformed(ActionEvent e) 
    {
        if (e.getSource()== checkingBtn)
        {
            witAmountTxt.requestFocusInWindow();
            //checking newcheck = new checking();
        }

    }
}


/*
        String accountType = null;
        accountType = JOptionPane.showInputDialog(null, "Checking or Savings?");

        if (accountType.equalsIgnoreCase("checking"))
        {
            checking c_Account = new checking();
        }
        else if (accountType.equalsIgnoreCase("savings"))
        {
        //  savings s_Account = new savings();
        }
        else
        {
            JOptionPane.showMessageDialog(null, "Invalid Selection");
        }

    */
È stato utile?

Soluzione

Prova a chiamare pack () sul JFrame dopo l'aggiunta di tutto e prima di setVisible (true)

Inoltre, si non si vuole dimenticare di impostare i GridBagConstraints weightx e campi pesanti. Almeno dare loro un valore non 0, come per la maggior parte dei campi 1.0 e 0 per i campi di dimensioni che non si desidera cambiato se l'interfaccia grafica cambia dimensione.

Altri suggerimenti

L'aggiunta di quelle opere per me:

    c.weightx=1.;
    c.fill=GridBagConstraints.HORIZONTAL;

C'è anche un bug in Swing, che potrebbe tradursi in un JTextArea appare come una fessura, anche se Sun / Oracle dice che "non è un bug, la sua caratteristica":

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4247013

Una possibile soluzione che qualcuno ha suggerito su quel thread è quello di impostare la dimensione minima del JTextField ... qualcosa di simile:

textField.setMinimumSize(textField.getPreferredSize());

sto cercando di indovinare dal nome LaB6 che non si può avere utilizzato GridBagLayout prima. Si tratta di uno dei maggior parte degli strumenti di layout difficili e temuti della Swing. Se non avete mai usato io suggerirei di lavorare attraverso esercitazioni, quali: http://download.oracle.com/javase/tutorial/uiswing/layout /gridbag.html e fino a portarsi ad esempio

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top