gpt4 book ai didi

java - 创建表时 JFrame 中的组件不出现

转载 作者:行者123 更新时间:2023-12-01 12:30:29 25 4
gpt4 key购买 nike

我是java编程新手。一切进行得都很顺利。但每当创建在 JScrollPane 中调用的 Jtable 时,我的 jframe 似乎都会遇到问题。有时所有组件(例如按钮、标签等)都不会出现。但是当我评论表及其属性时,组件就会出现。我的问题可能是什么?非常感谢您的帮助。多谢!这是我创建表的方法。再次感谢。

package myphonebookbeta;

import java.awt.event.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

public class MainForm extends JFrame implements ActionListener{

//<editor-fold defaultstate="collpased" desc="my components">

JFrame f=new JFrame();
JPanel p=new JPanel(null);
JLabel fnameLbl,lnameLbl,addressLbl,cpLbl;
JTextField fname,lname,address,cp;
JButton save,delete,cancel,update;
JTable table=null;
JScrollPane scrollPane=null;
JDesktopPane dp=null;
//</editor-fold>

//<editor-fold defaultstate="collapsed" desc="sql variabled">

private final String url="jdbc:mysql://localhost:3306/";
private final String db="dbnetbeans";
private final String uid="root";
private final String pwd="";
private final String driver="com.mysql.jdbc.Driver";
private Connection con=null;
private Statement st=null;
private ResultSet rs;
private ResultSetMetaData md;
private String sql;

//</editor-fold>

public MainForm(){

mainFormGui();

}//end MainForm

private void mainFormGui(){

f.setTitle("My Phonebook Beta");
f.setSize(600,400);
f.setLocationRelativeTo(f);
f.setVisible(true);
f.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
f.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e){
int cls=JOptionPane.showOptionDialog(null,"You are about to exit the program! Do you wish to proceed?","WARNING",
JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE,null,null,null);
if (cls==0){
System.exit(0);
}
}
});

initComponents();

}//end of mainFormGui

private void initComponents(){

//<editor-fold defaultstate="collapsed" desc="components declaration">

fnameLbl=new JLabel("First Name : "); fnameLbl.setBounds(10,18,90,24);
lnameLbl=new JLabel("Last Name : "); lnameLbl.setBounds(10,52,90,24);
addressLbl=new JLabel("Address : "); addressLbl.setBounds(10,86,90,24);
cpLbl=new JLabel("Contact No. :"); cpLbl.setBounds(10,120,90,24);

fname=new JTextField(""); fname.setBounds(100,14,140,24);
lname=new JTextField(""); lname.setBounds(100,48,140,24);
address=new JTextField(""); address.setBounds(100,82,140,24);
cp=new JTextField(""); cp.setBounds(100,116,140,24);

save=new JButton("SAVE"); save.setBounds(10,150,110,24);
cancel=new JButton("Cancel"); cancel.setBounds(130,150,110,24);
update=new JButton("Update"); update.setBounds(10,180,110,24);
delete=new JButton("Delete"); delete.setBounds(130,180,110,24);


//</editor-fold>

//<editor-fold defaultstate="collapsed" desc="adding to panel">
p.setVisible(true);

p.add(fnameLbl); p.add(fname);
p.add(lnameLbl); p.add(lname);
p.add(addressLbl); p.add(address);
p.add(cpLbl); p.add(cp);

p.add(save);p.add(delete); p.add(update); p.add(cancel);


//</editor-fold>

btnAction();

//createTable();

    f.add(p); 


}//end of initComponents

private void btnAction(){
cancel.addActionListener(this);
save.addActionListener(this);
delete.addActionListener(this);

}//end void btnAction

@Override
public void actionPerformed(ActionEvent ae){

String fn=fname.getText();
String ln=lname.getText();
String ad=address.getText();
String cpn=cp.getText();

try{

//<editor-fold defaultstate="collapsed" desc="cancel_click">

if(ae.getSource()==cancel){

clearFields();

}//end cancel

//</editor-fold>

//<editor-fold defaultstate="collapsed" desc="save_click">

if(ae.getSource()==save){

sql="Insert into tbphonebook (firstname,lastname,address,cp)"
+ " value('"+fn+"','"+ln+"','"+ad+"','"+cpn+"')";
connect();
st=con.createStatement();
st.executeUpdate(sql);
disconnect();
JOptionPane.showMessageDialog(null,"Info successfully SAVED!","confirmation - SAVE ", JOptionPane.INFORMATION_MESSAGE);
clearFields();

}//end save

//</editor-fold>

//<editor-fold defaultstate="collapsed" desc="delete_click">

if(ae.getSource()==delete){

int ch=JOptionPane.showOptionDialog(null,"This action is irrevocable. Do you wish tocontinue?",
"Delete this contact?",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE,null,null,null);

if (ch==0){

sql="delete from tbphonebook where firstname like '"+fn+"' and lastname like '"+ln+"'";
connect();
st=con.createStatement();
st.executeUpdate(sql);
disconnect();
JOptionPane.showMessageDialog(null,"Contact successfully DELETED!","confirmation - DELETE ", JOptionPane.INFORMATION_MESSAGE);
clearFields();

}//end if(ch==0)

}//end delete

//</editor-fold>


}//try

catch(Exception e){
e.printStackTrace();
}//catch
}//end actionPerformed

private void connect(){

try{
Class.forName(driver).newInstance();
con=DriverManager.getConnection(url+db,uid,pwd);
}
catch(Exception ex){
ex.printStackTrace();
}


}//end connect

private void disconnect(){

try{
con.close();
}
catch(Exception ex){
ex.printStackTrace();
}


}//end disconnect

private void clearFields(){

fname.setText("");
lname.setText("");
address.setText("");
cp.setText("");
fname.requestFocus();

}//end clearField

private void createTable(){

scrollPane=new JScrollPane(this.table);
scrollPane.setBounds(250,14,320,160);
scrollPane.setVisible(true);
scrollPane.setViewportView(table);
p.add(scrollPane);

}//end createTable

private void fillTable() throws SQLException{

ArrayList colNames=new ArrayList();
ArrayList data=new ArrayList();
ArrayList row=new ArrayList();

sql="select * from tbphonebook";
connect();
st=con.createStatement();
rs = st.executeQuery(sql);

try{

md =rs.getMetaData();
int col=md.getColumnCount();

//to getcolumn names
for (int i=1;1<=col;i++){
colNames.add(md.getColumnName(i));
}

//to get row data
while(rs.next()){

for(int i=1;1<=col;i++){

row.add(rs.getObject(i));

}

data.add(row);
}

}
catch(Exception ex){

ex.printStackTrace();

}


}//end fillTable
}

最佳答案

JPanel p=new JPanel(null); 

除非必要,否则不要使用空布局。阅读 this article关于如何使用 GridBagLayout。它易于使用且非常灵活。

关于您的重绘问题:您确定要在 Event Dispatch Thread 上执行该代码吗? ?

关于java - 创建表时 JFrame 中的组件不出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25955443/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com