自己做一个小的程序来熟悉代码,可是运行的时候表格两边的线很长是怎么回事啊?应该怎样解决? 注意看两边,表格已经没了,但是线还是延长了下来。import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;public class AddInterface extends JPanel{
private JTextField yearjtf=new JTextField(4);
private JTextField monthjtf=new JTextField(2);
private JTextField datejtf=new JTextField(2);
private JTextPane yearjtp=new JTextPane();
private JTextPane monthjtp=new JTextPane();
private JTextPane datejtp=new JTextPane();
private JButton add=new JButton("Add");
private JLabel datejlb=new JLabel("日期:");
private JPanel jp=new JPanel();
private JPanel jp1=new JPanel();
private Object[][] numberData=new Object[5][3];
private String[] columnNames={"消费项目","金额","备注"};
private DefaultTableModel tableModel=new DefaultTableModel(numberData,columnNames);
private JTable addTable=new JTable(tableModel);
public AddInterface(){
yearjtp.setText("年");
yearjtp.setEditable(false);
yearjtp.setBackground(this.getBackground());
monthjtp.setText("月");
monthjtp.setEditable(false);
monthjtp.setBackground(this.getBackground());
datejtp.setText("日");
datejtp.setEditable(false);
datejtp.setBackground(this.getBackground());
addTable.setRowHeight(25);
jp.setLayout(new GridLayout(1,6));
jp.add(datejlb);
jp.add(yearjtf);
jp.add(yearjtp);
jp.add(monthjtf);
jp.add(monthjtp);
jp.add(datejtf);
jp.add(datejtp);
jp.add(add);
jp1.add(new JScrollPane(addTable)); add(jp);
add(jp1);
}
/*public static void main(String[] args){
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception e){
throw new RuntimeException(e);
}
SwingConsole.run(new AddInterface(),500,500);
}*/}
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;public class AddInterface extends JPanel{
private JTextField yearjtf=new JTextField(4);
private JTextField monthjtf=new JTextField(2);
private JTextField datejtf=new JTextField(2);
private JTextPane yearjtp=new JTextPane();
private JTextPane monthjtp=new JTextPane();
private JTextPane datejtp=new JTextPane();
private JButton add=new JButton("Add");
private JLabel datejlb=new JLabel("日期:");
private JPanel jp=new JPanel();
private JPanel jp1=new JPanel();
private Object[][] numberData=new Object[5][3];
private String[] columnNames={"消费项目","金额","备注"};
private DefaultTableModel tableModel=new DefaultTableModel(numberData,columnNames);
private JTable addTable=new JTable(tableModel);
public AddInterface(){
yearjtp.setText("年");
yearjtp.setEditable(false);
yearjtp.setBackground(this.getBackground());
monthjtp.setText("月");
monthjtp.setEditable(false);
monthjtp.setBackground(this.getBackground());
datejtp.setText("日");
datejtp.setEditable(false);
datejtp.setBackground(this.getBackground());
addTable.setRowHeight(25);
jp.setLayout(new GridLayout(1,6));
jp.add(datejlb);
jp.add(yearjtf);
jp.add(yearjtp);
jp.add(monthjtf);
jp.add(monthjtp);
jp.add(datejtf);
jp.add(datejtp);
jp.add(add);
jp1.add(new JScrollPane(addTable)); add(jp);
add(jp1);
}
/*public static void main(String[] args){
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception e){
throw new RuntimeException(e);
}
SwingConsole.run(new AddInterface(),500,500);
}*/}
private JTextField yearjtf=new JTextField(4);
private JTextField monthjtf=new JTextField(2);
private JTextField datejtf=new JTextField(2);
private JTextPane yearjtp=new JTextPane();
private JTextPane monthjtp=new JTextPane();
private JTextPane datejtp=new JTextPane();
private JButton add=new JButton("Add");
private JLabel datejlb=new JLabel("日期:");
private JPanel jp=new JPanel();
private JPanel jp1=new JPanel();
private JScrollPane jScrollPane = new JScrollPane();
private Object[][] numberData=new Object[5][3];
private String[] columnNames={"消费项目","金额","备注"};
private DefaultTableModel tableModel=new DefaultTableModel(numberData,columnNames);
private JTable addTable=new JTable(tableModel);
public Test(){
yearjtp.setText("年");
yearjtp.setEditable(false);
yearjtp.setBackground(this.getBackground());
monthjtp.setText("月");
monthjtp.setEditable(false);
monthjtp.setBackground(this.getBackground());
datejtp.setText("日");
datejtp.setEditable(false);
datejtp.setBackground(this.getBackground());
addTable.setRowHeight(25);
addTable.setSize(100, 100);
addTable.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
jScrollPane.getViewport().setBackground(Color.white);
//主要是给JscrollPane设置大小。它会有个默认的大小
jScrollPane.setPreferredSize(new Dimension(300, 150));
jScrollPane.getViewport().add(addTable);
jp.setLayout(new GridLayout(1,6));
jp.add(datejlb);
jp.add(yearjtf);
jp.add(yearjtp);
jp.add(monthjtf);
jp.add(monthjtp);
jp.add(datejtf);
jp.add(datejtp);
jp.add(add);
jp1.add(jScrollPane); add(jp);
add(jp1);
}
public static void main(String[] args){
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception e){
throw new RuntimeException(e);
}
Test t=new Test();
JFrame f=new JFrame();
f.add(t);
f.setVisible(true);
f.setSize(500,300);
}}
不过多问一下,怎样才能让表格自动调整到JScrollPane的大小呢?或者说让JScrollPane自动调整到表格的大小。像上面调整的话是我们设置的高度150,要是我每一行高度都不一样就很麻烦。
JScrollPane.setPreferredSize(new Dimension(addTable.getWidth(),addTable.getHeight())检查发现addTable.getWidth()和addTable.getHeight()的值都是0,为什么啊?
用JTable的方法 setPreferredScrollableViewportSize(Dimension size)设置表格本身的首选大小。
但是用此方法设置表格首选大小后,getWidth()和getHeight()的值仍然为0,不知道为什么。
addTable.setSize(300, 150);
jScrollPane.getViewport().add(addTable);
jScrollPane.setPreferredSize(new Dimension(addTable.getWidth(), addTable.getHeight()));
这样就行了,这样addTable.getWidth()和addTable.getHeight()的值就为300,150了,不知道是不是你想要的结果。