[JAVA-SWING] Ubianie panelu/ladowanie nowego

0

Witam,
tak jak w temacie potrzebuje ubić panel obecnie istniejący i załadować nowy jak to zrobić?
Projekt tworzony w NeatBeansie..

pozdrawiam.

0

JPanel dziedziczy po klasie Container - looknij w dokumentacje.

public void remove(Component comp)
Removes the specified component from this container.

lub jeśli nie masz tego obiektu w zmiennej

public void remove(int index)
Removes the component, specified by index, from this container.

dodawanie wykonujsze funkcja add.

0

Mógłbyś mi jeszcze coś wyjaśnić bo się pogubiłem?, zobacz:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewJFrame.java
 *
 * Created on 2010-05-25, 21:59:50
 */

package javaapplication1;

import com.mysql.jdbc.PreparedStatement;
import javax.swing.JOptionPane;
import java.sql.*;
import javax.swing.JFrame;
import javax.swing.JPanel;




/**
 *
 * @author m.
 */
public class Main extends javax.swing.JFrame {

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

        textUsername = new javax.swing.JTextField();
        textPassword = new javax.swing.JTextField();
        logonButton = new javax.swing.JToggleButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        logonButton.setText("Logon");
        logonButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                logonButtonActionPerformed(evt);
            }
        });

        jLabel1.setText("Username:");

        jLabel2.setText("Password:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1))
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(textUsername)
                    .addComponent(textPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(logonButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(textUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(textPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(logonButton))
                .addContainerGap(58, Short.MAX_VALUE))
        );

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

    private void logonButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
        String username, password;
        username = textUsername.getText();
        password = textPassword.getText();

        if(username.length() <= 0)
            JOptionPane.showMessageDialog(null, "Please enter a username");
        else if (password.length() <= 0)
            JOptionPane.showMessageDialog(null, "Please enter a password");
        else
        logon(username, password);
    }                                           
    private int logon(String username, String password) {
                Connection  conn = null;
                PreparedStatement stmt = null;
		ResultSet rslt = null;

		try
		{
			conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1/test", "root", "test");
			//stmt = conn.createStatement();
                        stmt = (PreparedStatement) conn.prepareStatement("SELECT * FROM users WHERE username = ? AND password = ?");
                        stmt.setString(1, username);
                        stmt.setString(2, password);
                        rslt = stmt.executeQuery();
                        if(rslt.next()) {
                            String resultUsername = rslt.getString("username");
                            String resultPassword = rslt.getString("password");
                            if(resultUsername.equals(username) && resultPassword.equals(password)) {
/*Niszczenie starej formy ładowanie nowej z pliku mess.java*/
                            }

                        }
		}
		catch (SQLException sqlEx)
		{
			System.out.println(sqlEx.getMessage());
			//return;
		}
		finally
		{
			try
			{
				if (rslt != null)
				{
					rslt.close();
				}
			}
			catch (Exception ex)
			{
				System.out.println("Error closing result set");
			}
			try
			{
				if (stmt != null)
				{
					stmt.close();
				}
			}
			catch (Exception ex)
			{
				System.out.println("Error closing statement");
			}
			try
			{
				if (conn != null)
				{
					conn.close();
				}
			}
			catch (Exception ex)
			{
				System.out.println("Error closing connection");
			}
		}
        JOptionPane.showMessageDialog(null, username+" "+password);
        return 1;
    }
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Main().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JToggleButton logonButton;
    private javax.swing.JTextField textPassword;
    private javax.swing.JTextField textUsername;
    // End of variables declaration                   

}

Layout owej Formy jest wykreowany przez Edytor Wizualny zawarty w NetBeansie. I jak to jest teraz mogę tego używać czy owy wizerunek powinienem rzeźbić ręcznie (JFrame frame = new JFrame("xyz");)? aby cokolwiek zrobić ?

0

jeżeli chcesz używać wielu paneli dość przydatny jest manager java.awt.CardLayout;

1 użytkowników online, w tym zalogowanych: 0, gości: 1