各位大哥:
有个事请教一下各位,是这样的,我用JPanel想画一个东东,现在因为内容多,我想添加一个滚动条,
JScrollPane pane = new JScrollPane(monitorJPanel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
可以显示,但滚动条却没有作用,不清楚是哪写得有问题,请各位指出我的错误.谢谢,
附两个类的源码
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.table.*;
import java.util.*;
import java.io.*;
import java.util.Properties;
import javax.swing.UIManager;
public class DBMonitor extends JFrame {
String fileTableConfig = "dbtableconfig.properties";
String fileDBConfig = "dbconfig.properties";
MultiDataSource multiDataSource = null;
// Set setAllDB = null;
boolean DEBUG = true;
DBMonitor() {
init();
}
void init() {
HashMap map ;
JTabbedPane jtpMonitor = new JTabbedPane(SwingConstants.TOP);
/********************/
TableDataBase tdb = new TableDataBase(fileDBConfig);
HashMap tabMap = tdb.getDataDistribution(fileTableConfig);
/*******************/
String[] dbName = BufferTableCfg.getDBList(tabMap); // 取监控数据库名
for (int i = 0; i < dbName.length; i++) {
map = (HashMap) tabMap.get(dbName[i]);
DBMonitorJPanel monitorJPanel = new DBMonitorJPanel(map);
/**添加滚动条*/
JScrollPane pane = new JScrollPane(monitorJPanel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jtpMonitor.addTab("DataBase-" + dbName[i], pane);
}
getContentPane().add(jtpMonitor, BorderLayout.CENTER);
} public String readFile(String filename) {
String buffer = null;
byte[] bread = new byte[1024 * 1024]; try {
FileInputStream fis = new FileInputStream(filename);
int i = 0;
int nread = 0; while ((nread = fis.read()) != -1) {
bread[i++] = (byte) nread;
// buffer.append((char) nread);
}
buffer = new String(bread, 0, i); } catch (Exception e) {
e.printStackTrace();
}
return buffer;
}
public static void main(String[] args) {
final DBMonitor monitor = new DBMonitor(); monitor.setTitle("数据库监控");
monitor.setSize(800, 600);
monitor.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); monitor.setLocation((d.width - monitor.getSize().width) / 2, (d.height - monitor.getSize().height) / 2);
// frame.pack();
monitor.setVisible(true); monitor.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
// System.exit();
monitor.dispose();
} public void windowDeiconified(WindowEvent e) {// monitor.start();
} public void windowIconified(WindowEvent e) {// monitor.stop();
}
});
}
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.HashMap;
import java.util.*;
public class DBMonitorJPanel extends JPanel { HashMap tabMap ;
DBMonitorJPanel(HashMap tabMap) {
//setSize(800, 600);// 数据库名称 根据数据库名称显示相应数据库的值
this.tabMap = tabMap;
}
public void paint(Graphics g) {
super.paint(g);
Dimension dim = getSize(); //this.setPreferredSize(dim);
//this.revalidate(); g.setColor(Color.white);
g.fillRect(0, 0, (int) dim.getWidth(), (int) dim.getHeight());
int wid = (int) dim.getWidth() / 5; // 将屏幕分成5等份
g.setColor(Color.black);
g.drawString("表名", 20, 30);
g.drawString("1天", 20 + wid *1, 30);
g.drawString("2天", 20 + wid *2, 30);
g.drawString("1周", 20 + wid *3, 30);
g.drawString("1月", 20 + wid *4, 30);
String[] tabName = BufferTableCfg.getDBList(tabMap); // 取监控数据表名称
int hight = 0;
int width = 0;
Double D1;
for(int i = 0;i<tabName.length;i++){
//tabMap = (HashMap) tabMap.get(tabName[i]);
Vector vRet = (Vector) tabMap.get(tabName[i]);
//System.out.println("111111111="+tabName[i]);
hight = i;
g.setColor(Color.black);
g.drawString(tabName[i], 20, 40 * (i + 1) + 30);
for (int j = 0; j < vRet.size(); j++) {
StaticsInfo si = (StaticsInfo) vRet.get(j);
g.setColor(Color.black);
g.drawRect(wid * (j + 1)-20, 40 * (hight + 1)+10, 150, 30);
//在图上标识最大值最小值与当前值
g.drawLine(wid * (j + 1)-20, 40 * (hight + 1) + 25, wid * (j + 1)-20+150, 40 * (hight + 1) + 25);
g.drawString(""+si.min, wid * (j + 1)+20, 40 * (hight + 1) + 25);
g.drawString(""+si.max, wid * (j + 1)+70, 40 * (hight + 1) + 25);
if (si.cur<si.min) {
g.setColor(Color.red);
D1=new Double(si.cur/si.min*50);
width=D1.intValue();
g.fillRect(wid * (j + 1)-20+width, 40 * (hight + 1)+25, 150-width, 15);
}else if(si.cur>si.max){
g.setColor(Color.blue);
if(si.cur>=si.max*2){
width = 0;
}else{
D1=new Double((si.cur-si.max)/si.max*50);
width=D1.intValue();
}
//System.out.println("width="+width);
//g.fillRect(wid * (j + 1)-20, 50 * (hight + 1)+25, 150-width, 15);
g.fillRect(wid * (j + 1)-20, 40 * (hight + 1)+25, 150-width, 15);
}else if(si.min==si.max)
{
//System.out.println("zero,zero");
}
else {
g.setColor(Color.green);
D1=new Double((si.cur-si.min)/(si.max-si.min)*50);
width=D1.intValue();
//g.fillRect(wid * (j + 1)-20, 50 * (hight + 1) + 40, 150+width-100, 20);
g.fillRect(wid * (j + 1)-20, 40 * (hight + 1)+25, 50+width, 15);
}
g.setColor(Color.black);
g.drawString("cur="+si.cur, wid * (j + 1)+40, 40 * (hight + 1) + 38);
}
}
}
/**
* @return 返回最佳desktop尺寸..
*/
/*public Dimension preferredSizeOfAllFrames()
{
JInternalFrame[] array = getAllFrames();
int maxX = 0;
int maxY = 0;
for (int i = 0; i < array.length; i++)
{
if ( array[ i ].isVisible() )
{
int cx;
cx = array[i].getX();
int x = cx + array[i].getWidth();
if (x > maxX) maxX = x;
int cy;
cy = array[i].getY();
int y = cy + array[i].getHeight();
if (y > maxY) maxY = y;
}
}
return new Dimension(maxX, maxY);
}*/
}
有个事请教一下各位,是这样的,我用JPanel想画一个东东,现在因为内容多,我想添加一个滚动条,
JScrollPane pane = new JScrollPane(monitorJPanel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
可以显示,但滚动条却没有作用,不清楚是哪写得有问题,请各位指出我的错误.谢谢,
附两个类的源码
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.table.*;
import java.util.*;
import java.io.*;
import java.util.Properties;
import javax.swing.UIManager;
public class DBMonitor extends JFrame {
String fileTableConfig = "dbtableconfig.properties";
String fileDBConfig = "dbconfig.properties";
MultiDataSource multiDataSource = null;
// Set setAllDB = null;
boolean DEBUG = true;
DBMonitor() {
init();
}
void init() {
HashMap map ;
JTabbedPane jtpMonitor = new JTabbedPane(SwingConstants.TOP);
/********************/
TableDataBase tdb = new TableDataBase(fileDBConfig);
HashMap tabMap = tdb.getDataDistribution(fileTableConfig);
/*******************/
String[] dbName = BufferTableCfg.getDBList(tabMap); // 取监控数据库名
for (int i = 0; i < dbName.length; i++) {
map = (HashMap) tabMap.get(dbName[i]);
DBMonitorJPanel monitorJPanel = new DBMonitorJPanel(map);
/**添加滚动条*/
JScrollPane pane = new JScrollPane(monitorJPanel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jtpMonitor.addTab("DataBase-" + dbName[i], pane);
}
getContentPane().add(jtpMonitor, BorderLayout.CENTER);
} public String readFile(String filename) {
String buffer = null;
byte[] bread = new byte[1024 * 1024]; try {
FileInputStream fis = new FileInputStream(filename);
int i = 0;
int nread = 0; while ((nread = fis.read()) != -1) {
bread[i++] = (byte) nread;
// buffer.append((char) nread);
}
buffer = new String(bread, 0, i); } catch (Exception e) {
e.printStackTrace();
}
return buffer;
}
public static void main(String[] args) {
final DBMonitor monitor = new DBMonitor(); monitor.setTitle("数据库监控");
monitor.setSize(800, 600);
monitor.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); monitor.setLocation((d.width - monitor.getSize().width) / 2, (d.height - monitor.getSize().height) / 2);
// frame.pack();
monitor.setVisible(true); monitor.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
// System.exit();
monitor.dispose();
} public void windowDeiconified(WindowEvent e) {// monitor.start();
} public void windowIconified(WindowEvent e) {// monitor.stop();
}
});
}
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.HashMap;
import java.util.*;
public class DBMonitorJPanel extends JPanel { HashMap tabMap ;
DBMonitorJPanel(HashMap tabMap) {
//setSize(800, 600);// 数据库名称 根据数据库名称显示相应数据库的值
this.tabMap = tabMap;
}
public void paint(Graphics g) {
super.paint(g);
Dimension dim = getSize(); //this.setPreferredSize(dim);
//this.revalidate(); g.setColor(Color.white);
g.fillRect(0, 0, (int) dim.getWidth(), (int) dim.getHeight());
int wid = (int) dim.getWidth() / 5; // 将屏幕分成5等份
g.setColor(Color.black);
g.drawString("表名", 20, 30);
g.drawString("1天", 20 + wid *1, 30);
g.drawString("2天", 20 + wid *2, 30);
g.drawString("1周", 20 + wid *3, 30);
g.drawString("1月", 20 + wid *4, 30);
String[] tabName = BufferTableCfg.getDBList(tabMap); // 取监控数据表名称
int hight = 0;
int width = 0;
Double D1;
for(int i = 0;i<tabName.length;i++){
//tabMap = (HashMap) tabMap.get(tabName[i]);
Vector vRet = (Vector) tabMap.get(tabName[i]);
//System.out.println("111111111="+tabName[i]);
hight = i;
g.setColor(Color.black);
g.drawString(tabName[i], 20, 40 * (i + 1) + 30);
for (int j = 0; j < vRet.size(); j++) {
StaticsInfo si = (StaticsInfo) vRet.get(j);
g.setColor(Color.black);
g.drawRect(wid * (j + 1)-20, 40 * (hight + 1)+10, 150, 30);
//在图上标识最大值最小值与当前值
g.drawLine(wid * (j + 1)-20, 40 * (hight + 1) + 25, wid * (j + 1)-20+150, 40 * (hight + 1) + 25);
g.drawString(""+si.min, wid * (j + 1)+20, 40 * (hight + 1) + 25);
g.drawString(""+si.max, wid * (j + 1)+70, 40 * (hight + 1) + 25);
if (si.cur<si.min) {
g.setColor(Color.red);
D1=new Double(si.cur/si.min*50);
width=D1.intValue();
g.fillRect(wid * (j + 1)-20+width, 40 * (hight + 1)+25, 150-width, 15);
}else if(si.cur>si.max){
g.setColor(Color.blue);
if(si.cur>=si.max*2){
width = 0;
}else{
D1=new Double((si.cur-si.max)/si.max*50);
width=D1.intValue();
}
//System.out.println("width="+width);
//g.fillRect(wid * (j + 1)-20, 50 * (hight + 1)+25, 150-width, 15);
g.fillRect(wid * (j + 1)-20, 40 * (hight + 1)+25, 150-width, 15);
}else if(si.min==si.max)
{
//System.out.println("zero,zero");
}
else {
g.setColor(Color.green);
D1=new Double((si.cur-si.min)/(si.max-si.min)*50);
width=D1.intValue();
//g.fillRect(wid * (j + 1)-20, 50 * (hight + 1) + 40, 150+width-100, 20);
g.fillRect(wid * (j + 1)-20, 40 * (hight + 1)+25, 50+width, 15);
}
g.setColor(Color.black);
g.drawString("cur="+si.cur, wid * (j + 1)+40, 40 * (hight + 1) + 38);
}
}
}
/**
* @return 返回最佳desktop尺寸..
*/
/*public Dimension preferredSizeOfAllFrames()
{
JInternalFrame[] array = getAllFrames();
int maxX = 0;
int maxY = 0;
for (int i = 0; i < array.length; i++)
{
if ( array[ i ].isVisible() )
{
int cx;
cx = array[i].getX();
int x = cx + array[i].getWidth();
if (x > maxX) maxX = x;
int cy;
cy = array[i].getY();
int y = cy + array[i].getHeight();
if (y > maxY) maxY = y;
}
}
return new Dimension(maxX, maxY);
}*/
}
我不太清楚JScrollPanel这东东到底要怎么样设置,我是做数据库的,对JAVA不是很熟,对JSP倒是熟点!
public void paint(Graphics g) { super.paint(g);
Dimension dim = getSize();
//****************/我加上这两句话,最大化一下,再最小化,那滚动条有用,但是最大化后将失效,不能再往下滚,
this.setPreferredSize(dim);
this.revalidate();
我改成this.setPreferredSize(new Dimension(800,2000));
就显示成高2000,滚动条可以到那又没有啦,请问一下怎么样设成自动根着屏幕往下走的?
有谁可以教我一下,做成自动的啊,像IE的下拉滚动条一样,有就显示,长度不够就不需要显示
我用JScrollPane+JTable是没有问题的。
JScrollPane.setPreferredSize(new Dimension(width, height));
记得JScrollPane初始化的时候要设置一下大小,即不需滚动条时JScrollPane的大小
JScrollPane.setSize(350, 350);
public Dimension getPreferredSize() {
// 这里返回DBMonitorJPanel的实际大小,可以用Dimension dim = getSize()这个作为大小吧
}