//获取窗体根容器
Container ct = thisgetContentPane();
//创建文本域对象
JTextArea showInfo = new JTextArea();
//创建滚动板对象,并把文本域添加到滚动面板中
JScrollPane scroll = new JScrollPane(showInfo);
//添加组件到容器
ctadd(scroll);
//设置窗体尺寸和显示位置
thissetBounds(180, 100, 600, 500);
//设置窗体可见
thissetVisible(true);import javaio;
import javautilHashtable;
import javautilVector;
import javaxswing;
import javaxswingtableAbstractTableModel;
import javaawt;
import javaawteventActionEvent;
import javaawteventActionListener;
import javaawteventAdjustmentEvent;
import javaawteventAdjustmentListener;
public class T extends JFrame {
/
@param args
@throws Exception
/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
new T();
}
JButton btnA = new JButton("add");
JTable tab = new JTable();
JScrollBar bar = new JScrollBar();
JScrollPane sp = new JScrollPane(JScrollPaneVERTICAL_SCROLLBAR_NEVER,JScrollPaneHORIZONTAL_SCROLLBAR_NEVER);
int row = 0;
Vector<Hashtable<String,String>> data = new Vector<Hashtable<String,String>>();
int page;
int totalPage;
int pageSize = 20;
int displayCount;
public T() {
super("table");
thissetSize(400,400);
thissetLayout(new BorderLayout());
thisadd(btnA,BorderLayoutSOUTH);
JPanel p = new JPanel(new BorderLayout());
//padd(sp,BorderLayoutCENTER);
//padd(bar,BorderLayoutEAST);
baraddAdjustmentListener(new AdjustmentListener() {
public void adjustmentValueChanged(AdjustmentEvent e) {
page = bargetValue();
Systemoutprintln(egetAdjustmentType());
refresh(false);
}
});
barsetUnitIncrement(1);
barsetMinimum(1);
barsetMaximum(1);
barsetValue(1);
spsetViewportView(tab);
//spsetVerticalScrollBar(bar);
//sprevalidate();
//barrevalidate();
padd(sp,BorderLayoutCENTER);
padd(bar,BorderLayoutEAST);
thisadd(p,BorderLayoutCENTER);
tabsetModel(new TModel());
btnAaddActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Hashtable<String,String> h = new Hashtable<String,String>();
hput("a", "aa");
hput("b", "bb");
dataadd(h);
refresh(true);
barsetValue(page);
barsetMaximum(totalPage);
barrevalidate();
barrepaint();
}
});
thissetDefaultCloseOperation(DISPOSE_ON_CLOSE);
thissetVisible(true);
}
void refresh(boolean end) {
totalPage = datasize()/pageSize;
if(datasize()%pageSize!=0)totalPage++;
if(end)page = totalPage;
displayCount = (datasize()-(page-1)pageSize)>pageSizepageSize:datasize()-(page-1)pageSize;
if(page==0)displayCount = 0;
Systemoutprintln(displayCount);
//Systemoutprintln(datasize()+","+totalPage+","+page+","+displayCount);
tabrevalidate();
thisrepaint();
}
class TModel extends AbstractTableModel {
public int getColumnCount() {
return 2;
}
public int getRowCount() {
return displayCount;
}
public Object getValueAt(int rowIndex, int columnIndex) {
int dataIndex = (page-1)pageSize+rowIndex;
if(dataIndex>=datasize()||dataIndex<0)return null;
else {
Hashtable<String,String> h = dataget(dataIndex);
switch(columnIndex) {
case 0:return hget("a");
case 1:return hget("b");
default:return null;
}
}
}
}
}
不想搞了,就这样了,因为对scrollbar的事件监听不太懂,添加数据超过一页时要再添加第2个才换页,你看大致效果是不是你想要的了。import javaxswing;
import javaawt;
import javaawtevent;
public class ScrollIt extends JFrame
{
JScrollPane spContainer;
JTextArea taedit;
JButton bScroll;
public ScrollIt()
{
spContainer=new JScrollPane();
taedit=new JTextArea();
spContainergetViewport()add(taedit);
bScroll=new JButton("Scroll to last line");
bScrolladdActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JScrollBar sbar=spContainergetVerticalScrollBar();
sbarsetValue(sbargetMaximum());
}
});
for(int i=0;i<40;i++)
taeditappend("line"+i+"\n");
thisgetContentPane()setLayout(new BorderLayout());
thisgetContentPane()add(spContainer,BorderLayoutCENTER);
thisgetContentPane()add(bScroll,BorderLayoutNORTH);
thissetSize(500,500);
thissetVisible(true);
}
public static void main(String args[])
{
new ScrollIt();
}
}如果是swing做的,在子窗口的构造函数中写上这一句thissetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);就可以了,不用写什么监听器,以上是按我记忆写出来的,拼写可能有点错误。监听文本字段的Java程序如下:
import javaawtevent;
import javaxswing;
import javaawt;
public class dz extends JFrame implements ActionListener {
JTextField value = new JTextField("50", 30);//文本字段
JScrollBar bar = new JScrollBar(SwingConstantsHORIZONTAL,
50, 10, 0, 100);//滚动窗格
public dz() {
super("Well Adjusted");
setSize(350, 100);
valueaddActionListener(this);
valuesetHorizontalAlignment(SwingConstantsCENTER);//文本字段显示的字符串在中间位置
valuesetEditable(true);//文本字段可以编辑
JPanel pane = new JPanel();
panesetLayout(new BorderLayout());//网格布局
paneadd(value, "Center");
paneadd(bar, "South");
setDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
setContentPane(pane);
}
public static void main(String[] arguments) {
dz frame = new dz();
frameshow();
}
public void actionPerformed(ActionEvent ae) {
if(aegetSource()==value){
barsetValue(IntegervalueOf(valuegetText()));
}
}
}这个是JTextArea,将光标放到最前的行,就可以 了
~~~~~~~~~~~
~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~滚动条属性scrollbar滚动条顺序用以设置滚动条的显示外观。1)scrollbar-3d-light-color该属性用于设置或检索滚动条亮边框颜色。例如:div {scrollbar-3d-light-color :threedhighlight; }2)scrollbar-highlight-color该属性用于设置或检索滚动条3D界面的亮边颜色。例如:div {scrollbar-highlight-color :threedhighlight; }3)scrollbar-face-color该属性用于设置或检索滚动条3D表面的颜色。例如:div {scrollbar-face-color : threedface; }4)scrollbar-arrow-color该属性用于设置或检索滚动条方向箭头的颜色。当滚动条出现但不可用时,此属性失效。例如:div {scrollbar-arrow-color : buttontext; }5)scrollbar-shadow-color该属性用于设置或检索滚动条3D界面的暗边颜色。例如:div {scrollbar-shadow-color :ThreedDarkShadow; }6)scrollbar-dark-shadow-color该属性用于设置或检索滚动条暗边框颜色。例如:div {scrollbar-dark-shadow-color :threeddarkshadow; }7)scrollbar-base-color该属性用于设置或检索滚动条基准颜色。其他界面颜色将据此自动调整。例如:div {scrollbar-base-color : buttonface; }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)