关于多线程,急急急! synchronized同步关键字就是关键代码 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 看一直没人写,看来我真的要试试了。不过很不好意思,我现在不知道为什么要有Canvas这个类。我就用Component的子类对象写了,估计会用J类的。:) import javax.swing.*;import javax.swing.border.EtchedBorder;import java.awt.*;import java.awt.event.*;interface MyInitScheme { void initVarient(); void initFace();}class MyMenu extends JMenu implements MyInitScheme { public MyMenu(String text) { super(text); initVarient(); initFace(); } public void initFace() { setBackground(SystemColor.desktop); setFont(new Font("Serif", 0, 16)); setForeground(SystemColor.info); setBorder(BorderFactory.createEtchedBorder()); } public void initVarient(){};}class MyMenuItem extends JMenuItem implements MyInitScheme { public MyMenuItem(String text) { super(text); initVarient(); initFace(); } public void initFace() { setBackground(SystemColor.desktop); setFont(new Font("Serif", 0, 16)); setForeground(SystemColor.info); setBorder(BorderFactory.createEtchedBorder()); } public void initVarient(){};}class MyThread extends Thread { private Component p; private boolean stop=false; public void run() { while(!stop) { try { sleep(200); } catch (InterruptedException e) { System.out.println(e); } p.repaint(); } } public void stopThread() { stop=true; } public void start() { stop=false; super.start(); } public MyThread(Component cp) { p=cp; }}class MyFrame extends JFrame implements MyInitScheme { class MyContent extends JPanel { int x=10,y=10,r=30; public void paintComponent(Graphics g) { super.paintComponent(g); java.util.Random rand=new java.util.Random(); r=10+rand.nextInt(90); g.drawOval(x, y, r, r); } public MyContent() { setBackground(SystemColor.activeCaption); } } JPanel p1=new MyContent(); JPanel p2=new MyContent(); private JMenuBar mnuBar = new JMenuBar(); private JMenu mnuFile = new MyMenu("File"); private JMenuItem mnuFileExit = new MyMenuItem("Exit"); private JMenu mnuHelp = new MyMenu("Help"); private JMenuItem mnuHelpContact = new MyMenuItem("Contact Me"); public void initVarient() {}; public void initFace() { this.setSize(new Dimension(320, 240)); this.setResizable(false); getContentPane().setLayout(null); getContentPane().setBackground(SystemColor.activeCaption); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mnuBar.setBackground(SystemColor.desktop); mnuBar.setBorder(BorderFactory.createRaisedBevelBorder()); mnuFile.setMnemonic('F'); mnuFileExit.setMnemonic('x'); mnuFileExit.addActionListener(new ActionListener () { public void actionPerformed(ActionEvent e) { System.exit(0); } } ); mnuHelp.setMnemonic('H'); mnuHelpContact.setMnemonic('M'); mnuHelpContact.addActionListener(new ActionListener () { public void actionPerformed(ActionEvent e) { try { Process p=Runtime.getRuntime().exec("C:/Program Files/" +"Internet Explorer/IExplore.EXE" +" mailto:[email protected]"); } catch (Exception ex) { System.out.println(ex); } } } ); mnuFile.add(mnuFileExit); mnuHelp.add(mnuHelpContact); mnuBar.add(mnuFile); mnuBar.add(mnuHelp); this.setJMenuBar(mnuBar); p1.setBounds(new Rectangle(0,0,150,180)); p2.setBounds(new Rectangle(160,0,150,180)); getContentPane().add(p1); getContentPane().add(p2); } public MyFrame(String title) { super(title); initVarient(); initFace(); show(); }}public class ThreadApp { public static void main(String[] args) { MyFrame frame=new MyFrame("Test Threads"); final MyThread t1=new MyThread(frame.p1); final MyThread t2=new MyThread(frame.p2); frame.getContentPane().addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { t1.stopThread(); t2.stopThread(); } }); t1.start(); t2.start(); }} 如何在扩展的JButton里面,调用所在的Panel里的方法。 请教一个实现多个接口的问题? 3S——GIS、RS、GPS欢迎你 难题,将JAVA绘制的图形输出JPEG文件, 续:如何设置JTextPane里的文字为不可编辑的??? metome,谢谢你帮助 如何读取空字段不出错 这种问题实在不该问啊,可我得把java进行到底的呀! 如何在Bean里取得Session的值?多谢! 我是JAVA的初学者,有谁想和我一起学的,请加我的QQ,221017 极菜问题:在JButton上加Icon 好消息!本公司招骋JAVA开发工程师
import javax.swing.border.EtchedBorder;
import java.awt.*;
import java.awt.event.*;interface MyInitScheme {
void initVarient();
void initFace();
}
class MyMenu extends JMenu implements MyInitScheme {
public MyMenu(String text) {
super(text);
initVarient();
initFace();
}
public void initFace() {
setBackground(SystemColor.desktop);
setFont(new Font("Serif", 0, 16));
setForeground(SystemColor.info);
setBorder(BorderFactory.createEtchedBorder());
}
public void initVarient(){};
}
class MyMenuItem extends JMenuItem implements MyInitScheme {
public MyMenuItem(String text) {
super(text);
initVarient();
initFace();
}
public void initFace() {
setBackground(SystemColor.desktop);
setFont(new Font("Serif", 0, 16));
setForeground(SystemColor.info);
setBorder(BorderFactory.createEtchedBorder());
}
public void initVarient(){};
}class MyThread extends Thread {
private Component p;
private boolean stop=false; public void run() {
while(!stop) {
try {
sleep(200);
} catch (InterruptedException e) {
System.out.println(e);
}
p.repaint();
}
}
public void stopThread() {
stop=true;
}
public void start() {
stop=false;
super.start();
} public MyThread(Component cp) {
p=cp;
}
}
class MyFrame extends JFrame implements MyInitScheme {
class MyContent extends JPanel {
int x=10,y=10,r=30;
public void paintComponent(Graphics g) {
super.paintComponent(g);
java.util.Random rand=new java.util.Random();
r=10+rand.nextInt(90);
g.drawOval(x, y, r, r);
}
public MyContent() {
setBackground(SystemColor.activeCaption);
}
}
JPanel p1=new MyContent();
JPanel p2=new MyContent();
private JMenuBar mnuBar = new JMenuBar();
private JMenu mnuFile = new MyMenu("File");
private JMenuItem mnuFileExit = new MyMenuItem("Exit");
private JMenu mnuHelp = new MyMenu("Help");
private JMenuItem mnuHelpContact = new MyMenuItem("Contact Me");
public void initVarient() {};
public void initFace() {
this.setSize(new Dimension(320, 240));
this.setResizable(false);
getContentPane().setLayout(null);
getContentPane().setBackground(SystemColor.activeCaption);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mnuBar.setBackground(SystemColor.desktop);
mnuBar.setBorder(BorderFactory.createRaisedBevelBorder()); mnuFile.setMnemonic('F');
mnuFileExit.setMnemonic('x');
mnuFileExit.addActionListener(new ActionListener () {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
} ); mnuHelp.setMnemonic('H');
mnuHelpContact.setMnemonic('M');
mnuHelpContact.addActionListener(new ActionListener () {
public void actionPerformed(ActionEvent e) {
try {
Process p=Runtime.getRuntime().exec("C:/Program Files/"
+"Internet Explorer/IExplore.EXE"
+" mailto:[email protected]");
} catch (Exception ex) {
System.out.println(ex);
}
}
} );
mnuFile.add(mnuFileExit);
mnuHelp.add(mnuHelpContact);
mnuBar.add(mnuFile);
mnuBar.add(mnuHelp); this.setJMenuBar(mnuBar);
p1.setBounds(new Rectangle(0,0,150,180));
p2.setBounds(new Rectangle(160,0,150,180));
getContentPane().add(p1);
getContentPane().add(p2);
} public MyFrame(String title) {
super(title);
initVarient();
initFace();
show();
}
}
public class ThreadApp {
public static void main(String[] args) {
MyFrame frame=new MyFrame("Test Threads");
final MyThread t1=new MyThread(frame.p1);
final MyThread t2=new MyThread(frame.p2);
frame.getContentPane().addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
t1.stopThread();
t2.stopThread();
}
});
t1.start();
t2.start();
}
}