我是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
}
答案 0 :(得分:1)
JPanel p=new JPanel(null);
除非必须,否则请勿使用null布局。阅读this article了解如何使用GridBagLayout。它易于使用且非常灵活。
关于您的重绘问题:您确定要在Event Dispatch Thread上执行该代码吗?