关于多线程,急急急! 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(); }} 排序速度问题 在JDialog里边设置JTextField焦点问题,大家帮忙想想,问题解决立刻结帖 求教!!关于java全局变量的问题 救命!一个NB中GUI设计问题 不规则的字符串 不规则的空格该怎么处理 各位大虾 怎么用java从ftp上下东西啊 帮忙看段代码~~~ 为什么老是java.lang.NullPointerException Netty作为server端的问题 java反射如何获取并修改list子对象 极菜问题:在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();
}
}