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 の新しいインスタンスを作成しています。代わりに、main メソッドで作成された JPanel2 インスタンスを、コンストラクターまたは settor メソッドを介して JPanel4 に渡す必要があります。

于 2012-10-25T11:34:57.527 に答える