0

我制作了一个JFrame并使用JLabel. 但是,一旦我放大,JFrame一切都会错位。我怎样才能建立我的JFrame,所以它可以调整大小?

public class First extends javax.swing.JFrame {

    /**
     * Creates new form First
     */
    public First() {
        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() {

        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        t1 = new javax.swing.JTextField();
        t2 = new javax.swing.JPasswordField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("WELCOME");
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jButton1.setFont(new java.awt.Font("Segoe Script", 3, 24)); // NOI18N
        jButton1.setText("LOGIN");
        jButton1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jButton1.setContentAreaFilled(false);
        jButton1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 310, 140, 30));

        jLabel1.setFont(new java.awt.Font("Viner Hand ITC", 3, 36)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Administrator of Library System");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 30, 614, -1));

        t1.setFont(new java.awt.Font("Segoe UI", 3, 20)); // NOI18N
        t1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        t1.setOpaque(false);
        t1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                t1ActionPerformed(evt);
            }
        });
        getContentPane().add(t1, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 200, 180, 30));

        t2.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        t2.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        t2.setOpaque(false);
        getContentPane().add(t2, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 250, 180, 30));

        jLabel2.setFont(new java.awt.Font("Segoe Script", 3, 24)); // NOI18N
        jLabel2.setText("Username     -");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 200, 200, 30));

        jLabel3.setFont(new java.awt.Font("Segoe Script", 3, 24)); // NOI18N
        jLabel3.setText("Password     -");
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 250, 230, 30));

        jButton2.setFont(new java.awt.Font("Segoe Script", 3, 24)); // NOI18N
        jButton2.setText("Forgot Password ??");
        jButton2.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jButton2.setContentAreaFilled(false);
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 410, 250, 40));

        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/librarymanagementsystem/library_book.jpg"))); // NOI18N
        jLabel4.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(-10, -10, 880, 500));

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

    private void t1ActionPerformed(java.awt.event.ActionEvent evt) {                                   
        // TODO add your handling code here:
    }                                  

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        try
        {
            Class.forName("java.sql.Driver");  
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql", "root", "parin");
            Statement stmt=con.createStatement();
            String username="";
            String password="";

            String query="Select * from Liblogin;";
            ResultSet rs=stmt.executeQuery(query);



            while(rs.next())
            {
                username=rs.getString("username");
                password=rs.getString("password");
            }


            rs.close();
            stmt.close();
            con.close();


            String enteredUsername=t1.getText().toString();
            String enteredPassword = new String(t2.getText());
            if(enteredUsername.contentEquals(username)&&enteredPassword.contentEquals(password))
            {
                Homepage a=new Homepage();
                a.setVisible(true); 
                this.dispose();
            }
            else
            {
                JOptionPane.showMessageDialog(this,"INVALID USER");
            }
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(this, "ERROR IN CORRECTION");
        }
    }                                        

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        this.dispose();
        Security a=new Security();
        a.setVisible(true);

    }                                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(First.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(First.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(First.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(First.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new First().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField t1;
    private javax.swing.JPasswordField t2;
    // End of variables declaration                   
}
4

1 回答 1

0

GridBagLayout用作您的布局管理器并花时间查看组件的preferredSizeminimumSize属性maximumSize。您还可以使用Box.Filler创建不可见组件,并在JPanel调整窗口大小时将它们添加到您的组件之间。我帮不了你再进一步,因为我不知道您到底希望组件的行为如何。我建议您阅读这本书Swing,第 4 章适合您

于 2013-09-25T13:20:58.543 回答