import java.awt.*;
import javax.swing.*;
import javax.swing.*;
import javax.swing .table.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.event.*;
public class TestProgressbar {
public static void main(String[] args) {
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch (Exception e)
{
e.printStackTrace();
}
MyFrame f=new MyFrame();
f.setVisible(true);
}
}
class MyFrame extends JFrame implements ActionListener,ChangeListener
{
private JProgressBar jpb;
private JButton b1,b2,b3,b4,b5,b6,b7,b8;
String[] headers = { "表头一", "表头二", "表头三" };
Object[][] cellData = {{"row1-col1", "row1-col2", "row1-col3"},{"row2-col1", "row2-col2", "row2-col3"}};;
JTable table;
DefaultTableModel model = new DefaultTableModel(cellData, headers) {
public boolean isCellEditable(int row, int column) {
return false;
}
};
//在构造方法内实现窗体的建立
public MyFrame()
{
super("进度条演示");
model.setColumnCount(10);
model.setRowCount(10);
table = new JTable(model);
this.setBounds(60,80, 600, 400);
this.getContentPane().setLayout(new FlowLayout());
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
jpb=new JProgressBar();
jpb.addChangeListener(this);
b1=new JButton("设置进度条的当前值为50,最小值为0,最大值为100");
b1.addActionListener(this);
b2=new JButton("得到进度条的当前值");
b2.addActionListener(this);
b3=new JButton("增加");
b3.addActionListener(this);
b4=new JButton("减少");
b4.addActionListener(this);
b5=new JButton("新增列");
b5.addActionListener(this);
b6=new JButton("删除列");
b6.addActionListener(this);
b7=new JButton("新增行");
b7.addActionListener(this);
b8=new JButton("删除行");
b8.addActionListener(this);
Container c=this.getContentPane();
c.add(jpb);
c.add(b1);
c.add(b2);
c.add(b3);
c.add(b4);
c.add(table);
c.add(b5);
c.add(b6);
c.add(b7);
c.add(b8);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{
jpb.setMinimum(0);
jpb.setMaximum(100);
}
if(e.getSource()==b2)
{
JOptionPane.showMessageDialog(this, jpb.getValue()+"", "提示",JOptionPane.INFORMATION_MESSAGE);
}
if(e.getSource()==b3)
{
if(jpb.getValue()<jpb.getMaximum())
{jpb.setValue(jpb.getValue()+1);}
}
if(e.getSource()==b4)
{
if(jpb.getValue()>jpb.getMinimum())
{jpb.setValue(jpb.getValue()-1);}
}
if(e.getSource()==b5)
{model.addColumn("新列名");
}
if(e.getSource()==b6)
{table.removeColumn(table.getColumnModel().getColumn(table.getColumnCount()-1));
}
if(e.getSource()==b7)
{model.addRow(new Object[]{"sitinspring", (table.getRowCount()+1)+"", "Boss"});
}
if(e.getSource()==b8)
{int[] selRowIndexs=table.getSelectedRows();// 用户所选行的序列
for(int i=0;i<selRowIndexs.length;i++){
model.removeRow(table.getSelectedRow());
}
}
}
public void stateChanged(ChangeEvent e) {
//JOptionPane.showMessageDialog(this, jpb.getValue()+"", "提示",JOptionPane.INFORMATION_MESSAGE);
}
}