3

当我尝试从中调用方法时JPanel2,按下按钮后JPanel4它不会显示任何更改JFrame,但是当我尝试将按钮放入时JPanel2,一切正常。所以我的问题是,正如我在标题中所说 - 我创建了一个副本JPanel吗?我对java相当陌生,所以任何帮助表示赞赏。

主要的

package tellimine;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;


public class Tellimine extends JFrame{

public Tellimine(){
      super("Tellimine");
}

public void looFrame() throws Exception{

   this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   this.setPreferredSize(new Dimension(800, 310));
   this.setLayout(new GridBagLayout());
   GridBagConstraints c = new GridBagConstraints();


    JPanel2 panel2 = new JPanel2();
    c.fill = GridBagConstraints.HORIZONTAL;
    c.insets = new Insets(0,0,0,0);
    c.weightx = 1.0;
    c.gridx = 0;
    c.gridy = 1;
    this.add(panel2, c);


    JPanel4 panel4 = new JPanel4();
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipady = 0;
    c.weighty = 1.0;
    c.anchor = GridBagConstraints.PAGE_END;
    c.gridx = 0;
    c.gridwidth = 2;
    c.gridy = 3;
    this.add(panel4, c);

    this.setVisible(true);
    this.pack();

}

public static void main(String[] args) throws Exception{

    Class.forName("cubrid.jdbc.driver.CUBRIDDriver");

   final Tellimine raam = new Tellimine();
    EventQueue.invokeLater(new Runnable() {
        @Override
    public void run() {
            try {
                raam.looFrame();
            } catch (Exception ex) {
                Logger.getLogger(Tellimine.class.getName()).log(Level.SEVERE, null, ex);
            }
            }
         });
   }
}

JPanel2

package tellimine;

import java.awt.GridLayout;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class JPanel2 extends JPanel{

private Connection conn;
Integer suurus;
JLabel tellimus, tk_nr;
JFormattedTextField tellija, kuupaev;
Statement stmt = null;
ResultSet rs = null;

public JPanel2() throws SQLException{
    super();
 panel2();
}


    public static Connection connect() {

    Connection conn = null;

    try{    
        conn = DriverManager.getConnection("jdbc:cubrid:localhost:33000:Access::","dba","qwerty");
    }
    catch ( Exception e ) {

       System.err.println("SQLException : " + e.getMessage());
    }
    return conn;
}

public void panel2() throws SQLException{

    conn=connect();
    stmt = conn.createStatement();
    rs = stmt.executeQuery("SELECT * FROM yldandmed");
    rs.next();
    tellimus = new JLabel();
    tk_nr = new JLabel();
    tellija = new JFormattedTextField();
    kuupaev = new JFormattedTextField();

    yldandmed();

    this.setLayout(new GridLayout(3,5));
    this.add(new JLabel("Tellimuse ID"));
    this.add(tellimus);
    this.add(new JLabel("Tellija:"));
    this.add(tellija);
    this.add(new JLabel(""));
    this.add(new JLabel("Sissekandekuupäev"));
    this.add(kuupaev);
    this.add(new JLabel("Töökäsu nr:"));
    this.add(tk_nr);
    this.add(new JLabel(""));
    this.add(new JLabel("Tellimise Detailid"));
    this.add(new JLabel(""));
    this.add(new JLabel(""));
    this.add(new JLabel(""));

}

public void yldandmed() throws SQLException{

    tellimus.setText(rs.getString("tel_id"));
    System.out.println(rs.getString("tellija"));
    tellija.setText(rs.getString("tellija"));
    kuupaev.setText(rs.getString("kuupaev"));
    tk_nr.setText(rs.getString("tk_nr"));
  }
}

JPanel4

package tellimine;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JPanel;


public class JPanel4 extends JPanel{

JPanel2 paneel = new JPanel2();

    public JPanel4() throws Exception{
    super();
 panel4();
}

    private void panel4() throws SQLException{

        JButton uuenda = new JButton("<");
        this.add(uuenda);
        uuendanupp uuendus = new uuendanupp();
        uuenda.addActionListener(uuendus);

        JButton lisa = new JButton(">");
        this.add(lisa);
        lisanupp lisamine = new lisanupp();
        lisa.addActionListener(lisamine);       
    }



    private class uuendanupp implements ActionListener{

        public void actionPerformed(ActionEvent e){

            System.out.println("<");

        }

}
    private class lisanupp implements ActionListener{

        public void actionPerformed(ActionEvent e){
            try {   
                paneel.rs.next();
                paneel.yldandmed();
                System.out.println(">");    
            } catch (SQLException ex) {
                Logger.getLogger(JPanel4.class.getName()).log(Level.SEVERE, null, ex);
            }

        }        
   }
}
4

1 回答 1

4
public class JPanel4 extends JPanel{

JPanel2 paneel = new JPanel2();

在这里,您正在创建 JPanel2 的新实例。您应该通过构造函数或通过 settor 方法将 main 方法中创建的 JPanel2 实例传递给 JPanel4。

于 2012-10-25T11:34:57.527 回答