代码:
package myprojects.jmemorydemo;
import javax.swing.UIManager;
import java.awt.*;
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
public class JMemoryDemo
{
private boolean packFrame = false;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public JMemoryDemo()
{
MainFrame frame = new MainFrame();
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (packFrame)
{
frame.pack();
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else
{
frame.validate();
}
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (frameSize.height > screenSize.height)
{
frameSize.height = screenSize.height;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (frameSize.width > screenSize.width)
{
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height)
/ 2);
frame.setVisible(true);
}
public static void main(String[] args)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
new JMemoryDemo();
}
}
===================================================
package myprojects.jmemorydemo;
import javax.swing.*;
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
public class ProgressThread extends Thread
{
JProgressBar pbar;
JLabel label;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public ProgressThread(JProgressBar pbar,JLabel label)
{
this.pbar=pbar;
this.label=label;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void run()
{
int min=0;
int max=100;
int free=0;
int totle=0;
int status=0;
pbar.setMinimum(min);
pbar.setMaximum(max);
pbar.setValue(status);
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
while(true)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
totle=(int)(Runtime.getRuntime().totalMemory()/1024);
free=(int)(Runtime.getRuntime().freeMemory()/1024);
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
}catch(Exception e)
{
e.printStackTrace();
}
label.setText("Free Memory :"+(int)(Runtime.getRuntime().freeMemory()/1024)+"K"+" Totle Memory Allo
cated :"
+(int)(Runtime.getRuntime().totalMemory()/1024)+"K");
status=(int)(free*100/totle);
pbar.setValue(status);
pbar.setString("Free Momery "+ status+"%");
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
this.sleep(1000);
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
}catch(InterruptedException err)
{}
}
}
}
===============================================
package myprojects.jmemorydemo;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
public class MainFrame extends JFrame
{
private JPanel contentPane;
private BorderLayout borderLayout1 = new BorderLayout();
private JProgressBar jProgressBar1 = new JProgressBar();
private JLabel jLabel1 = new JLabel();
private JLabel jLabel2 = new JLabel();
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public MainFrame()
{
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
jbInit();
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
catch(Exception e)
{
e.printStackTrace();
}
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private void jbInit() throws Exception
{
(MainFrame.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(304, 215));
this.setTitle("JMemoryDemo");
jLabel1.setFont(new java.awt.Font("Dialog", 0, 14));
jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
jLabel1.setText("Memory Monitor");
jProgressBar1.setOrientation(JProgressBar.VERTICAL);
jProgressBar1.setFont(new java.awt.Font("Dialog", 0, 14));
jProgressBar1.setToolTipText("");
jProgressBar1.setStringPainted(true);
jLabel2.setFont(new java.awt.Font("Dialog", 0, 14));
jLabel2.setText("");
contentPane.add(jProgressBar1, BorderLayout.CENTER);
contentPane.add(jLabel1, BorderLayout.NORTH);
contentPane.add(jLabel2, BorderLayout.SOUTH);
ProgressThread pThread = new ProgressThread(this.jProgressBar1,this.jLabel2);
pThread.start();
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
protected void processWindowEvent(WindowEvent e)
{
super.processWindowEvent(e);
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (e.getID() == WindowEvent.WINDOW_CLOSING)
{
System.exit(0);
}
}
}
![](/Images/OutliningIndicators/None.gif)
问题的关键:
totle=(int)(Runtime.getRuntime().totalMemory()/1024);
free=(int)(Runtime.getRuntime().freeMemory()/1024);
posted on 2007-11-07 09:57
lk 阅读(358)
评论(0) 编辑 收藏 所属分类:
j2se