我需要一个JFrame,里面有一个JPanel和一个JTable。我想在北方有jtable,在南方有JPanel。
当我在JFrame的南边添加它时,JPanel有两行,第二行不可见!如此令人困惑......请帮忙
package javaapplication18;
import javaapplication23.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import javax.swing.JTable.*;
import javax.swing.table.*;
import javax.swing.*;
import javax.swing.JFrame;
/**
*
* @author danial
*/
public class JavaApplication18 extends JFrame {
static JLabel jFN,jLN,jP,jR,jB,jIranianDate;
static JTextField tFN,tLN,tP,tR,tfBirthDate,tIranianDate;
static java.util.Date dateBirthDate;
static java.sql.Date sqlBirthDate;
static ResultSet rs;
static Object[][] info;
static Object[] columns={"ID","FirstName","LastName","P","R","BD"};
static DefaultTableModel dTable=new DefaultTableModel(info,columns);
public static void main(String[] args) {
JavaApplication23 iranianDateInterface=new JavaApplication23() ;
JavaApplication23 iranianDateInterface1=new JavaApplication23() ;
iranianDateInterface1.setIranianDate(1364, 3, 14);
String bg=iranianDateInterface1.getIranianDate();
String iranianDate=iranianDateInterface.getIranianDate();
System.out.println(bg);
JFrame frame=new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Connection con;
try {
Class.forName("com.mysql.jdbc.Driver");
con= DriverManager.getConnection("jdbc:mysql://localhost/kn","root","rit3000");
Statement st=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String query = "Select * From tabmain";
rs=st.executeQuery(query);
while(rs.next()) {
Object[] object=new Object[]{rs.getInt(1),rs.getString(2),rs.getString(3),rs.getInt(4),rs.getInt(5),rs.getDate(6)};
dTable.addRow(object);
}
} catch (ClassNotFoundException ex) {
}catch (SQLException e){
System.out.println(e.getMessage());
}
final JTable jTable=new JTable(dTable);
jTable.setRowHeight(jTable.getRowHeight()+10);
jTable.setFont( new Font("Serif",Font.PLAIN,20));
jTable.setAutoCreateRowSorter(true);
jTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
JScrollPane scrollPane= new JScrollPane(jTable);
frame.add(scrollPane,BorderLayout.CENTER);
JButton add= new JButton("add");
add.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
}
});
JButton remove=new JButton("remove");
remove.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
}
});
jFN=new JLabel("First Name");
jLN=new JLabel ("Last Name");
jP=new JLabel("Payment");
jR=new JLabel("Rest");
jB=new JLabel("Birthday");
jIranianDate=new JLabel("Cal");
tFN=new JTextField(15);
tLN=new JTextField (15);
tP=new JTextField(7);
tR=new JTextField(7);
tIranianDate=new JTextField(iranianDate,8);
tfBirthDate=new JTextField("0000-00-00",7);
JPanel inputPanel =new JPanel();
inputPanel.add(jFN);
inputPanel.add(tFN);
inputPanel.add(jLN);
inputPanel.add(tLN);
inputPanel.add(jP);
inputPanel.add(tP);
inputPanel.add(jR);
inputPanel.add(tR);
inputPanel.add(jB);
inputPanel.add(tfBirthDate);
inputPanel.add(add);
inputPanel.add(remove);
inputPanel.add(jIranianDate);
inputPanel.add(tIranianDate);
frame.add(inputPanel,BorderLayout.SOUTH);
frame.setSize(900,600);
frame.setVisible(true);
}
}
答案 0 :(得分:0)
当您使用BorderLayout时,您的目标是让几个控件具有一定的宽度(EAST / WEST)或高度(NORTH,SOUTH),并使其余的自动调整大小(CENTER)。要为边上的控件设置特定宽度或高度,请使用java.awt.Component类的首选size属性。