JFrame的setSize

时间:2013-07-07 15:31:07

标签: java swing size jframe preferredsize

我正在使用我的新项目。现在我有这么多Jframe页面。但是我无法通过setSize方法调整框架大小。请任何人都可以帮我编码

以下代码集包含登录屏幕

/*
 * LoginScreen.java
 *
* Created on July 6, 2013, 11:52 AM
*/

package com.macfast.mbaplus.admin;

import com.macfast.mbaplus.domain.Users;
import com.macfast.mbaplus.support.Util;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 *
 * @author  My
 */
public class LoginScreen extends javax.swing.JFrame {

/** Creates new form LoginScreen */

@Override
public void setSize(int width, int height) {
    super.setSize(1000, 300);
}
public LoginScreen() {
    initComponents();

    signInBtn.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            String uname = usrNameTxt.getText();
            char[] pword = pwordTxt.getPassword();
            String password = new String(pword);
            if (uname == null || uname.equals("") || uname.equals(" ") || password == null || password.equals("") || password.equals(" ")) {

                Util.showErrorMessageDialog("Please fill all the fields");

            }else {
                authenticateLogin();
            }

        }
    });
}
public Boolean authenticateLogin() {

    Boolean authenticationSuccess = false;
 Users user = null;
            String uname = usrNameTxt.getText();
            char[] pword = pwordTxt.getPassword();
            String password = new String(pword);
            Util util = new Util();
            user = util.authenticate(uname, password);
            if (null != user) {
                authenticationSuccess = true;
                Util.showSuccessMessageDialog("Welcome "+uname);
                this.dispose();
                if (user.getIsAdmin()) {
                    new AdminHome().setVisible(true);
                } else {
                    new Sections().setVisible(true);
                }

            } else {
                 Util.showErrorMessageDialog("Invalid Login Credentials");
            }
           return authenticationSuccess; 
}
/** 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.
 */
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jLabel3 = new javax.swing.JLabel();
    jLabel4 = new javax.swing.JLabel();
    jLabel5 = new javax.swing.JLabel();
    jLabel6 = new javax.swing.JLabel();
    jLabel7 = new javax.swing.JLabel();
    jPanel2 = new javax.swing.JPanel();
    jLabel1 = new javax.swing.JLabel();
    usrNameTxt = new javax.swing.JTextField();
    jLabel2 = new javax.swing.JLabel();
    pwordTxt = new javax.swing.JPasswordField();
    signInBtn = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(macfast.MacfastApp.class).getContext().getResourceMap(LoginScreen.class);
    setBackground(resourceMap.getColor("Form.background")); // NOI18N
    setForeground(resourceMap.getColor("Form.foreground")); // NOI18N
    setName("Form"); // NOI18N

    jLabel3.setIcon(resourceMap.getIcon("jLabel3.icon")); // NOI18N
    jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
    jLabel3.setName("jLabel3"); // NOI18N

    jLabel4.setFont(resourceMap.getFont("jLabel4.font")); // NOI18N
    jLabel4.setIcon(resourceMap.getIcon("jLabel4.icon")); // NOI18N
    jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
    jLabel4.setName("jLabel4"); // NOI18N

    jLabel5.setIcon(resourceMap.getIcon("jLabel5.icon")); // NOI18N
    jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
    jLabel5.setName("jLabel5"); // NOI18N

    jLabel6.setFont(resourceMap.getFont("jLabel6.font")); // NOI18N
    jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
    jLabel6.setName("jLabel6"); // NOI18N

    jLabel7.setIcon(resourceMap.getIcon("jLabel7.icon")); // NOI18N
    jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
    jLabel7.setName("jLabel7"); // NOI18N

    jPanel2.setName("jPanel2"); // NOI18N

    jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
    jLabel1.setName("jLabel1"); // NOI18N

    usrNameTxt.setText(resourceMap.getString("usrNameTxt.text")); // NOI18N
    usrNameTxt.setName("usrNameTxt"); // NOI18N

    jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
    jLabel2.setName("jLabel2"); // NOI18N

    pwordTxt.setText(resourceMap.getString("pwordTxt.text")); // NOI18N
    pwordTxt.setName("pwordTxt"); // NOI18N

    signInBtn.setText(resourceMap.getString("signInBtn.text")); // NOI18N
    signInBtn.setName("signInBtn"); // NOI18N

    javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
    jPanel2.setLayout(jPanel2Layout);
    jPanel2Layout.setHorizontalGroup(
        jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel2Layout.createSequentialGroup()
            .addGap(36, 36, 36)
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addComponent(signInBtn)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel1)
                        .addComponent(jLabel2))
                    .addGap(71, 71, 71)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(pwordTxt)
                        .addComponent(usrNameTxt, javax.swing.GroupLayout.DEFAULT_SIZE, 145, Short.MAX_VALUE))))
            .addContainerGap(45, Short.MAX_VALUE))
    );
    jPanel2Layout.setVerticalGroup(
        jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel2Layout.createSequentialGroup()
            .addGap(44, 44, 44)
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel1)
                .addComponent(usrNameTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(36, 36, 36)
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addComponent(jLabel2)
                .addComponent(pwordTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(30, 30, 30)
            .addComponent(signInBtn)
            .addContainerGap(56, Short.MAX_VALUE))
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(28, 28, 28)
                    .addComponent(jLabel5))
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jLabel6)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jLabel7))
                .addGroup(layout.createSequentialGroup()
                    .addGap(102, 102, 102)
                    .addComponent(jLabel4)))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(111, 111, 111)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 228, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createSequentialGroup()
                    .addGap(68, 68, 68)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
            .addContainerGap(46, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(jLabel7)
                            .addGap(92, 92, 92)
                            .addComponent(jLabel4))
                        .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.LEADING))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 58, Short.MAX_VALUE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap())
                .addGroup(layout.createSequentialGroup()
                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 258, Short.MAX_VALUE)
                    .addGap(381, 381, 381))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(39, 39, 39))))
    );

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

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new LoginScreen().setVisible(true);

        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JPanel jPanel2;
private javax.swing.JPasswordField pwordTxt;
private javax.swing.JButton signInBtn;
private javax.swing.JTextField usrNameTxt;
// End of variables declaration                   

}

我使用以下代码来设置我的jframe(检查我更新的代码开始部分)&gt;但它不起作用。

@Override
public void setSize(int width, int height) {
    super.setSize(1000, 300);
}

2 个答案:

答案 0 :(得分:4)

这里最好的解决方案可能是随时拨打setSize(..),而是拨打pack()。这将确保GUI是显示其中组件所需的最小尺寸。

答案 1 :(得分:2)

你不是在任何地方打电话给setSize()。你要覆盖它(你不应该这样做),但你不是在呼唤它。