Topic: 版主:我列宽的问题解决了,可是新的问题来了,能帮我看一下吗?

  Print this page

1.版主:我列宽的问题解决了,可是新的问题来了,能帮我看一下吗? Copy to clipboard
Posted by: imain
Posted on: 2005-05-25 14:20

JTable表格在窗体内为什么不居中呢?每次都是左对起.

2.Re:版主:我列宽的问题解决了,可是新的问题来了,能帮我看一下吗? [Re: imain] Copy to clipboard
Posted by: imain
Posted on: 2005-05-25 14:21

这是原代码:

import javax.swing.*;
import java.awt.*;
import javax.swing.table.*;
import java.sql.*;
import com.borland.dbswing.*;
import java.util.*;
import java.awt.event.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright Coffee 2005</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/

public class ItemPrice extends JFrame implements ActionListener {
JPanel mainP = new JPanel();
JButton okBtn=new JButton("添加");
JButton cancelBtn=new JButton("取消");
JPanel p2=new JPanel();
JLabel cate=new JLabel("用水类别");
JTextField tfCate=new JTextField(10);
JLabel price=new JLabel("单 价");
JTextField tfPrice=new JTextField(10);
public ItemPrice()
{

Container cont=getContentPane();
cont.setLayout(new FlowLayout(FlowLayout.CENTER));
cont.add(new p1());
cont.add(mainP);
p2.setLayout(new GridLayout(3,2));
p2.add(cate);
p2.add(price);
p2.add(tfCate);
p2.add(tfPrice);
p2.add(okBtn);
p2.add(cancelBtn);
mainP.add(p2);
okBtn.addActionListener(this);
cancelBtn.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==okBtn)
{
WaterDB db=new WaterDB();
String sql="insert into category(name,price) values(\'"+tfCate.getText()+"\',\'"+tfPrice.getText()+"\')";
db.executeUpdate(sql);
tfCate.setText("");
tfPrice.setText("");
}
else if(e.getSource()==cancelBtn)
{
tfCate.setText("");
tfPrice.setText("");
}
}

class p1 extends JPanel {
public p1() {
Vector cell;
Vector row = new Vector();
DefaultTableModel tableModel = new DefaultTableModel();
String[] tableHeads = {"用水类别", "单价"};
Vector tableHeadName = new Vector();
for (int i = 0; i <tableHeads.length; i++) {
tableHeadName.add(tableHeads[i]);
}
try {
WaterDB wdb = new WaterDB();
String sql = "select * from category";
ResultSet rs = wdb.executeQuery(sql);
while (rs.next()) {
cell = new Vector();
for (int i = 1; i <= tableHeads.length; i++) {
cell.add(rs.getStringLight Bulb);
}
row.add(cell);
}
tableModel.setDataVector(row, tableHeadName);
JTable table = new JTable(tableModel);
table.setFont(new Font("Dialog", 1, 13));
table.setRowHeight(20);
table.getColumn( "用水类别" ).setMaxWidth(120 ) ;
table.getColumn( "用水类别" ).setResizable( false ) ;
table.getColumn( "单价" ).setMaxWidth(80 ) ;
table.getColumn( "单价" ).setResizable( false ) ;
table.setCursor(new Cursor(12));
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setCursor(new Cursor(12));
this.add(scrollPane);
this.setVisible(true);
}
catch (SQLException f) {}
}
}
public static void main(String[] args) {
ItemPrice itemPrice = new ItemPrice();
itemPrice.setSize(280, 300);
itemPrice.show();
}
}

3.Re:版主:我列宽的问题解决了,可是新的问题来了,能帮我看一下吗? [Re: imain] Copy to clipboard
Posted by: kavinwang
Posted on: 2005-05-25 19:18

居中的话,左右两边都有一块白的,岂不是更难看,嘿嘿!
你可以调整你的frame的大小,或者重新布局到一个比较好看的界面。
另外你可以调整head的宽度,填充满整个panel或者scrollpane,可能还要好看一点

4.Re:版主:我列宽的问题解决了,可是新的问题来了,能帮我看一下吗? [Re: imain] Copy to clipboard
Posted by: imain
Posted on: 2005-05-26 18:45

怎么布置呀,我就知道往panel上放置jtable.麻烦版主能帮我改一下吗?先谢了

5.Re:版主:我列宽的问题解决了,可是新的问题来了,能帮我看一下吗? [Re: imain] Copy to clipboard
Posted by: kavinwang
Posted on: 2005-05-27 08:52

你可以把下面的东西放在jscrollpane的右边,然后调整tableheader的宽度,保持能把整个pane填充满

6.Re:版主:我列宽的问题解决了,可是新的问题来了,能帮我看一下吗? [Re: imain] Copy to clipboard
Posted by: imain
Posted on: 2005-05-27 20:13

好,我试一下。

7.Re:版主:我列宽的问题解决了,可是新的问题来了,能帮我看一下吗? [Re: imain] Copy to clipboard
Posted by: imain
Posted on: 2005-05-29 18:56

还是没弄明白

8.Re:版主:我列宽的问题解决了,可是新的问题来了,能帮我看一下吗? [Re: imain] Copy to clipboard
Posted by: kavinwang
Posted on: 2005-05-30 08:31

看看关于布局方面的资料吧!
http://java.sun.com/docs/books/tutorial/uiswing/TOC.html


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923