package test;
import javaawtBorderLayout;
import javaawtContainer;
import javaawteventActionEvent;
import javaawteventActionListener;
import javaawteventMouseEvent;
import javaawteventMouseListener;
import javaxswingJButton;
import javaxswingJFrame;
import javaxswingJLabel;
import javaxswingJMenu;
import javaxswingJMenuBar;
import javaxswingJMenuItem;
import javaxswingJPopupMenu;
import javaxswingJScrollPane;
import javaxswingJTextArea;
import javaxswingJToolBar;
@SuppressWarnings("serial")
public class MyEditor extends JFrame implements ActionListener,MouseListener {
private Container contentPane;
private JMenuBar menuBar=new JMenuBar();
private JMenu fileMenu=new JMenu("File");
private JMenu editMenu=new JMenu("Edit");
private JMenu helpMenu=new JMenu("Help");
private JMenuItem newMI=new JMenuItem("New");
private JMenuItem openMI=new JMenuItem("Open");
private JMenuItem saveMI=new JMenuItem("Save");
private JMenuItem exitMI=new JMenuItem("Exit");
private JMenuItem copyMI=new JMenuItem("Copy");
private JMenuItem cutMI=new JMenuItem("Cut");
private JMenuItem pasteMI=new JMenuItem("Paste");
private JMenuItem aboutMI=new JMenuItem("About");
private JToolBar toolBar=new JToolBar();
private JButton copyBtn=new JButton("Copy");
private JButton cutBtn=new JButton("Cut");
private JButton pasteBtn=new JButton("Paste");
private JScrollPane scorllPane=new JScrollPane();
private JTextArea editArea=new JTextArea();
private JLabel statusBar=new JLabel("status:");
private JPopupMenu popupMenu=new JPopupMenu();
private JMenuItem copyPMI=new JMenuItem("Copy");
private JMenuItem cutPMI=new JMenuItem("Cut");
private JMenuItem pastePMI=new JMenuItem("Paste");
public MyEditor(String title){
super(title);
setDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
contentPane=getContentPane();
setSize(800,600);
initGUI();
}
private void initGUI(){
fileMenuadd(newMI);
fileMenuadd(openMI);
fileMenuadd(saveMI);
fileMenuaddSeparator(); //分割符
fileMenuadd(exitMI);
editMenuadd(copyMI);
editMenuadd(cutMI);
editMenuadd(pasteMI);
helpMenuadd(aboutMI);
menuBaradd(fileMenu);
menuBaradd(editMenu);
menuBaradd(helpMenu);
popupMenuadd(copyPMI);
popupMenuadd(cutPMI);
popupMenuadd(pastePMI);
setJMenuBar(menuBar);
contentPanesetLayout(new BorderLayout());
toolBaradd(copyBtn);
toolBaradd(cutBtn);
toolBaradd(pasteBtn);
contentPaneadd(toolBar,BorderLayoutNORTH);
thisscorllPanegetViewport()add(thiseditArea);
contentPaneadd(scorllPane,BorderLayoutCENTER);
contentPaneadd(thisstatusBar,BorderLayoutSOUTH);
exitMIaddActionListener(this);
copyMIaddActionListener(this);
cutMIaddActionListener(this);
pasteMIaddActionListener(this);
saveMIaddActionListener(this);
copyBtnaddActionListener(this);
cutBtnaddActionListener(this);
pasteBtnaddActionListener(this);
editAreaaddMouseListener(this);
}
public void actionPerformed(ActionEvent e){
Object source=egetSource();
if(source==exitMI){
Systemexit(0);
}
if(source==copyMI || source==copyBtn){
editAreacopy();
}
if(source == cutMI || source == cutBtn){
editAreacut();
}
if(source==pasteMI || source==pasteBtn){
editAreapaste();
}
}
public void go(){
setVisible(true);
}
public static void main(String[] args) {
(new MyEditor("my editor"))go();
}
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
int x=egetX();int y=egetY();
if(popupMenuisPopupTrigger(e)){
popupMenushow(editArea, x, y);
}
}
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
mousePressed(e);
}
}
这可是我的原创啊,你要是能在网上搜到,我就佩服你!
另外,你是学java的吗?你要是会java就不可能运行不了的!!!
楼上的,这是用JavaScript来做的,而不是用java!!
建一个文件名叫:clockhtml,里面代码如下:
<html>
<head>
<title>js做的时钟</title>
<SCRIPT type="text/javascript" >
<!--
Dateprototypeformat = function(mask) {
var d = this;
var zeroize = function (value, length) {
if (!length) length = 2;
value = String(value);
for (var i = 0, zeros = ''; i < (length - valuelength); i++) {
zeros += '0';
}
return zeros + value;
};
return maskreplace(/"[^"]"|'[^']'|\b(:d{1,4}|m{1,4}|yy(:yy)|([hHMstT])\1|[lLZ])\b/g, function($0) {
switch($0) {
case 'd': return dgetDate();
case 'dd': return zeroize(dgetDate());
case 'ddd': return ['Sun','Mon','Tue','Wed','Thr','Fri','Sat'][dgetDay()];
case 'dddd': return ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'][dgetDay()];
case 'M': return dgetMonth() + 1;
case 'MM': return zeroize(dgetMonth() + 1);
case 'MMM': return ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'][dgetMonth()];
case 'MMMM': return ['January','February','March','April','May','June','July','August','September','October','November','December'][dgetMonth()];
case 'yy': return String(dgetFullYear())substr(2);
case 'yyyy': return dgetFullYear();
case 'h': return dgetHours() % 12 || 12;
case 'hh': return zeroize(dgetHours() % 12 || 12);
case 'H': return dgetHours();
case 'HH': return zeroize(dgetHours());
case 'm': return dgetMinutes();
case 'mm': return zeroize(dgetMinutes());
case 's': return dgetSeconds();
case 'ss': return zeroize(dgetSeconds());
case 'l': return zeroize(dgetMilliseconds(), 3);
case 'L': var m = dgetMilliseconds();
if (m > 99) m = Mathround(m / 10);
return zeroize(m);
case 'tt': return dgetHours() < 12 'am' : 'pm';
case 'TT': return dgetHours() < 12 'AM' : 'PM';
case 'Z': return dtoUTCString()match(/[A-Z]+$/);
// Return quoted strings with the surrounding quotes removed
default: return $0substr(1, $0length - 2);
}
});
};
function refreshCalendarClock(){
documentallcalendarClock4innerHTML = new Date()format("yyyy年MM月dd日 HH:mm:ss");
}
var going = true;
function oprationClock(obj) {
if(obj) {
refreshCalendarClock();
setTimeout("oprationClock(going)",1000)
if(!going) going=true;
return;
}
going = false;
}
// -->
</SCRIPT>
</head>
<body onLoad="oprationClock(true)">
<h1>用js做的时钟:</h1>
<hr />
<font color="green"><h2 id="calendarClock4"> </h2></font>
时钟开关:<input type="radio" name="opration" value="1" onClick="oprationClock(this)" checked/>开
<input type="radio" name="opration" onClick="oprationClock(false)" />关
</body>
</html>
效果图,另外有个更有趣的时钟,像闹钟一样的,见参考资料
应该可以了,是你定义了MouseHandler定义了一个MouseAdapter的方法而没有用到,把mouserHandler改成mousePressed或者是mouseReleased就可以了下面是代码:
package comboby;
import javaawt;
import javaawtevent;
import javautil;
import javaawtgeom;
import javaxswing;
public class MouseTest {
public static void main(String[] args) {
MouseFrame frame = new MouseFrame();
framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
framesetVisible(true);
}
}
class MouseFrame extends JFrame {
public MouseFrame() {
setTitle("MouseTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
MousePanel panel = new MousePanel();
Container contentPane = getContentPane();
contentPaneadd(panel);
}
public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 200;
}
class MousePanel extends JPanel {
public MousePanel() {
squares = new ArrayList();
current = null;
addMouseListener(new MouseHandler());
addMouseMotionListener(new MouseMotionHandler());
}
public void paintComponent(Graphics g) {
superpaintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2setPaint(ColorRED);
for (int i = 0; i < squaressize(); i++)
g2draw((Rectangle2D) squaresget(i));
}
public Rectangle2D find(Point2D p) {
for (int i = 0; i < squaressize(); i++) {
Rectangle2D r = (Rectangle2D) squaresget(i);
if (rcontains(p))
return r;
}
return null;
}
public void add(Point2D p) {
double x = pgetX();
double y = pgetY();
current = new Rectangle2DDouble(x - SIDELENGTH / 2,
y - SIDELENGTH / 2, SIDELENGTH, SIDELENGTH);
squaresadd(current);
repaint();
}
public void remove(Rectangle2D s) {
if (s == null)
return;
if (s == current)
current = null;
squaresremove(s);
repaint();
}
private static final int SIDELENGTH = 10;
private ArrayList squares;
private Rectangle2D current;
private class MouseHandler extends MouseAdapter {
public void mouseReleased(MouseEvent event) {
current = find(eventgetPoint());
if (current == null)
add(eventgetPoint());
}
public void mouseClicked(MouseEvent event) {
current = find(eventgetPoint());
if (current == null && eventgetClickCount() >= 2)
remove(current);
}
}
private class MouseMotionHandler implements MouseMotionListener
{
public void mouseMoved(MouseEvent event) {
if (find(eventgetPoint()) == null)
setCursor(CursorgetDefaultCursor());
else
setCursor(CursorgetPredefinedCursor(CursorCROSSHAIR_CURSOR));
}
public void mouseDragged(MouseEvent event) {
if (current != null) {
int x = eventgetX();
int y = eventgetY();
currentsetFrame(x - SIDELENGTH / 2, y - SIDELENGTH / 2,
SIDELENGTH, SIDELENGTH);
repaint();
}
}
}
}
以上就是关于求一个简单的java程序全部的内容,包括:求一个简单的java程序、很简单的java程序制作、java简单程序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)