JTextField在设置超过其初始大小的文本后调整大小?

时间:2015-12-10 21:31:01

标签: java swing netbeans

这是单击检查按钮之前文本字段的样子:

Before clicking check

点击检查后会发生这种情况。只有字符串比文本字段长时才会发生这种情况。

After clicking check

这是来自netbeans的代码:

package filevalidator;

import java.awt.Color;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;

/**
 *
 * @author Kosar
 */
public class Validator extends javax.swing.JFrame {
    static String path1 = null;
    static String path2 = null;
   /**
     * Creates new form Validator
     */
public Validator() {
    initComponents();
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    jLabel1 = new javax.swing.JLabel();
    file1path = new javax.swing.JTextField();
    select1 = new javax.swing.JButton();
    jLabel2 = new javax.swing.JLabel();
    file2path = new javax.swing.JTextField();
    select2 = new javax.swing.JButton();
    hash1 = new javax.swing.JLabel();
    hash2 = new javax.swing.JLabel();
    result = new javax.swing.JLabel();
    checkBtn = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setResizable(false);

    jLabel1.setFont(new java.awt.Font("Trebuchet MS", 0, 18)); // NOI18N
    jLabel1.setText("Original File:");

    file1path.setFont(new java.awt.Font("Trebuchet MS", 0, 14)); // NOI18N
    file1path.setAutoscrolls(false);

    select1.setText("Select");
    select1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            select1ActionPerformed(evt);
        }
    });

    jLabel2.setFont(new java.awt.Font("Trebuchet MS", 0, 18)); // NOI18N
    jLabel2.setText("Second File:");

    file2path.setFont(new java.awt.Font("Trebuchet MS", 0, 14)); // NOI18N
    file2path.setAutoscrolls(false);
    file2path.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));

    select2.setText("Select");
    select2.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            select2ActionPerformed(evt);
        }
    });

    hash1.setFont(new java.awt.Font("Trebuchet MS", 1, 20)); // NOI18N
    hash1.setForeground(java.awt.Color.blue);
    hash1.setText(" ");

    hash2.setFont(new java.awt.Font("Trebuchet MS", 1, 20)); // NOI18N
    hash2.setForeground(java.awt.Color.blue);
    hash2.setText(" ");

    result.setFont(new java.awt.Font("Trebuchet MS", 1, 20)); // NOI18N
    result.setText(" ");

    checkBtn.setFont(new java.awt.Font("Tahoma", 1, 13)); // NOI18N
    checkBtn.setText("Check");
    checkBtn.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            checkBtnActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jLabel1)
                .addComponent(jLabel2))
            .addGap(32, 32, 32)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(result, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(151, 151, 151)
                            .addComponent(hash2, javax.swing.GroupLayout.DEFAULT_SIZE, 340, Short.MAX_VALUE))
                        .addComponent(hash1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGap(30, 30, 30))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(file1path, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(file2path))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(select1, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(select2, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addContainerGap())
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(checkBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(331, 331, 331))
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addGap(59, 59, 59)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel1)
                .addComponent(file1path, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(select1))
            .addGap(27, 27, 27)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel2)
                .addComponent(file2path, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(select2))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 21, Short.MAX_VALUE)
            .addComponent(hash1)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(hash2)
            .addGap(13, 13, 13)
            .addComponent(result)
            .addGap(5, 5, 5)
            .addComponent(checkBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap())
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addGap(27, 27, 27)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(27, 27, 27))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addGap(34, 34, 34)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );

    pack();
    setLocationRelativeTo(null);
}// </editor-fold>                        

private void select1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
    JFileChooser chooser = new JFileChooser();
    chooser.setDialogTitle("Select file");

    if (chooser.showOpenDialog(select1) == JFileChooser.APPROVE_OPTION) {
        path1 = chooser.getSelectedFile().getAbsolutePath();
    }

    file1path.setText(path1);
    file1path.setBackground(Color.white);
}                                       

private void select2ActionPerformed(java.awt.event.ActionEvent evt) {                                        
    JFileChooser chooser = new JFileChooser();
    chooser.setDialogTitle("Select file");

    if (chooser.showOpenDialog(select2) == JFileChooser.APPROVE_OPTION) {
        path2 = chooser.getSelectedFile().getAbsolutePath();
    }

    file2path.setText(path2);
    file2path.setBackground(Color.white);
}                                       

private void checkBtnActionPerformed(java.awt.event.ActionEvent evt) {                                         
    try {
        HashMD5 hasher = new HashMD5();
        try {    
            hash1.setText(hasher.hash(path1));
            hash2.setText(hasher.hash(path2));
        } catch (NoSuchAlgorithmException | IOException ex) {
        }

        if ( hash1.getText().equals(hash2.getText())) {
            result.setForeground(Color.GREEN);
            result.setText("Hashes are the same. File has not been modified.");
        }
        else {
            result.setForeground(Color.RED);
            result.setText("Hashes are not the same. File has been modified.");
        }
    } catch (NullPointerException e) {
        if (file1path.getText() == null) {
            file1path.setBackground(Color.red);
        }
        else if (file2path.getText() == null) {
            file2path.setBackground(Color.red);
        }
        else {
            file1path.setBackground(Color.red);
            file2path.setBackground(Color.red);

        }
    }

}                                        

/**
 * @param args the command line arguments
 */

// Variables declaration - do not modify                     
private javax.swing.JButton checkBtn;
private javax.swing.JTextField file1path;
private javax.swing.JTextField file2path;
private javax.swing.JLabel hash1;
private javax.swing.JLabel hash2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel1;
private javax.swing.JLabel result;
private javax.swing.JButton select1;
private javax.swing.JButton select2;
// End of variables declaration                   
}

1 个答案:

答案 0 :(得分:3)

您可以使用TextField及其方法设置列来处理它,即“setColumns(int numOfCols)”

import java.awt.FlowLayout;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class TextFieldExample extends JFrame {
    JFrame frame = new JFrame();
    public TextFieldExample()
    {
        frame = new JFrame("Test");
        Container container = frame.getContentPane();
        JTextField fld1 = new JTextField("Java C++ C");
        JTextField fld2 = new JTextField("");
        JTextField fld3 = new JTextField("");

        fld1.setColumns(5); //Now, it will not be resized.
        fld2.setColumns(7); //Now, it will not be resized.
        fld3.setColumns(9); //Now, it will not be resized.
        container.setLayout(new FlowLayout());
        container.add(fld1);
        container.add(fld2);
        container.add(fld3);
        frame.setVisible(true);
        frame.setSize(150,150);
    }
    public static void main(String[] args) {
        TextFieldExample obj = new TextFieldExample();
    }
}