首页上一页 1 下一页尾页 1 条记录 1/1页
监听出错
发表在Java图书答疑
2016-09-06
是否精华
是
否
版块置顶:
是
否
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
class MyDialog extends JDialog{
public MyDialog(SwingAndThread frame){
super(frame,true);
Container CD=getContentPane();
CD.add(new JLabel("乌龟在奔跑!"));
setBounds(120,120,100,100);
}
}
public class SwingAndThread extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
private JLabel jl=new JLabel();
private static Thread t;
private int count=0;
private Container C=getContentPane();
public SwingAndThread(){
setBounds(0,200,1366,280);
C.setLayout(new FlowLayout());
ImageIcon url = new ImageIcon(SwingAndThread.class.getResource("Tortoise.gif"));
jl.setIcon(url);
jl.setHorizontalAlignment(SwingConstants.LEFT);
jl.setOpaque(true);
t=new Thread(new Runnable(){
public void run(){
while(count<=1050){
jl.setBounds(count,0,250,240);
try{
Thread.sleep(300);
}catch(Exception e){
e.printStackTrace();
}
count+=10;
if(count==1050){
count=10;
}
}
}
});
jl.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
new MyDialog(SwingAndThread.this).setVisible(true);
}
});
t.start();
C.add(jl);
C.setBackground(Color.white);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new SwingAndThread();
}
}
(给我的提示是把代码进行更改,我全部都试过没有用)
(为什么那些标签,容器,线需要“Private”?是不是我进行监听的“MyDialog”也需要“Private”?)
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
class MyDialog extends JDialog{
public MyDialog(SwingAndThread frame){
super(frame,true);
Container CD=getContentPane();
CD.add(new JLabel("乌龟在奔跑!"));
setBounds(120,120,100,100);
}
}
public class SwingAndThread extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
private JLabel jl=new JLabel();
private static Thread t;
private int count=0;
private Container C=getContentPane();
public SwingAndThread(){
setBounds(0,200,1366,280);
C.setLayout(new FlowLayout());
ImageIcon url = new ImageIcon(SwingAndThread.class.getResource("Tortoise.gif"));
jl.setIcon(url);
jl.setHorizontalAlignment(SwingConstants.LEFT);
jl.setOpaque(true);
t=new Thread(new Runnable(){
public void run(){
while(count<=1050){
jl.setBounds(count,0,250,240);
try{
Thread.sleep(300);
}catch(Exception e){
e.printStackTrace();
}
count+=10;
if(count==1050){
count=10;
}
}
}
});
jl.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
new MyDialog(SwingAndThread.this).setVisible(true);
}
});
t.start();
C.add(jl);
C.setBackground(Color.white);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new SwingAndThread();
}
}
(给我的提示是把代码进行更改,我全部都试过没有用)
(为什么那些标签,容器,线需要“Private”?是不是我进行监听的“MyDialog”也需要“Private”?)