当我尝试在我的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);
}
}
}
}
答案 0 :(得分:4)
public class JPanel4 extends JPanel{
JPanel2 paneel = new JPanel2();
在这里,您将创建JPanel2的新实例。您应该通过构造函数或settor方法将在main方法中创建的JPanel2实例传递给JPanel4。