javaswing 组件中线程与流程问题

神奇大自然 2017-08-22 04:08:34
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.awt.*;
class A
{
public static void main(String [] args)
{
Ck ck=new Ck();
ck.scck();


}



}

class Ck//窗口
{
JFrame jf= new JFrame("测试窗口");


JButton button=new JButton("按钮");//生成按钮1
JButton button2=new JButton("按钮1");//生成按钮2
JTextField text=new JTextField();//生成文本框1
JTextField text2=new JTextField();//生成文本框2
JButton button3=new JButton("上一页");//上一页按钮
JButton button4=new JButton("下一页");//下一页按钮
JPanel jp=new JPanel(){

public void paintComponent(Graphics s)
{
System.out.println("被重写111");
ImageIcon icon=new ImageIcon("c:\\123.jpg");
s.drawImage(icon.getImage(),0,0,400,200,jf);

}


};//生成自定义容器




public void scck()
{


scJpanel(jp);
text.setVisible(true);//文本框1
text.setBackground(Color.green);
text.setBounds(170,200,200,50);

text2.setVisible(true);//文本框2
text2.setBackground(Color.red);
text2.setBounds(370,200,200,50);


button.setVisible(true);//按钮1
button.setBackground(Color.lightGray);
button.setBounds(170,420,200,50);
button.addActionListener(djbutton());//按钮1监听

button2.setVisible(true);//按钮2
button2.setBackground(Color.cyan);
button2.setBounds(370,420,200,50);
button2.addActionListener(djbutton2());//按钮2监听

button3.setVisible(true);
button3.setBackground(Color.red);
button3.setBounds(120,110,30,30);
button3.addActionListener(syybutton());//上一页按钮监听


button4.setVisible(true);
button4.setBackground(Color.red);
button4.setBounds(590,110,30,30);
button4.addActionListener(xyybutton());//下一页按钮监听

jf.setVisible(true);
jf.setSize(800,600);
jf.setLayout(null);
jf.add(button);
jf.add(button2);
jf.add(text2);
jf.add(text);
jf.add(jp);
jf.add(button3);
jf.add(button4);

jf.addWindowListener(new WindowAdapter() //为了关闭窗口
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
} );






}

public ActionListener djbutton2()//监听按钮2
{
ActionListener al=new ActionListener()
{

public void actionPerformed(ActionEvent ws)
{
String nr=text2.getText();//获取内容
System.out.println("按钮2被单机");
System.out.println("文本框2"+nr);
}

};
return al;
}
public ActionListener djbutton()//监听按钮1
{
ActionListener al=new ActionListener()
{

public void actionPerformed(ActionEvent ws)
{
String nr=text.getText();
System.out.println("按钮1被单机");
System.out.println("文本框"+nr);
}

};
return al;
}

public ActionListener syybutton()
{
ActionListener ls=new ActionListener()
{
public void actionPerformed(ActionEvent a)
{
System.out.println("上一页被单机");
jp=null;
jp=new JPanel(){

public void paintComponent(Graphics s)
{
System.out.println("被重写");
ImageIcon icon=new ImageIcon("C:\\124.jpg");
s.drawImage(icon.getImage(),0,0,400,200,jf);

}

};

if(jp==null)
{

System.out.println("kong");
}

scJpanel(jp,jf);

}


};

return ls;

}
public ActionListener xyybutton()
{
ActionListener al=new ActionListener()
{
public void actionPerformed(ActionEvent s)
{
System.out.println("下一页被单机");


}
};
return al;

}

public void scJpanel(JPanel jp)
{
jp.setVisible(true);//自定义容器
jp.setBounds(170,20,400,200);
jp.setBackground(Color.orange);

}
public void scJpanel(JPanel jp,JFrame jf)
{
System.out.println("中转被调用");
jp.setVisible(true);//自定义容器
jp.setBounds(170,20,410,160);
jp.setBackground(Color.orange);
jf.add(jp);
}
}
...全文
250 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
神奇大自然 2017-08-25
  • 打赏
  • 举报
回复
好的 非常感谢 我试下
神奇大自然 2017-08-24
  • 打赏
  • 举报
回复
楼上大神 可以把代码复制下来试一下 编译运行没问题 就是点击按钮的时候 哪个方法没有执行
nj_dobetter 2017-08-24
  • 打赏
  • 举报
回复
在paintComponent方法前面加上注釋@Override,看看是否能編譯通過呢? 如果不能编译通過,就説明沒有覆盖成功。
110成成 2017-08-24
  • 打赏
  • 举报
回复
将图片URL 做成动态设置,设置完之后,调用repaint方法重新绘制就行了。
神奇大自然 2017-08-24
  • 打赏
  • 举报
回复
大神果然够6 再请教一下 我的思路是 每点击一次按钮 jpanel的图片就换一张 这个大体的思路是什么啊
110成成 2017-08-24
  • 打赏
  • 举报
回复
我觉得你首先要去看下paintComponent 是什么时候调用 paintComponent 一般是在重新绘制组件的时候才调用。
JPanel jp=new JPanel(){

public void paintComponent(Graphics s)
{
System.out.println("被重写111");
ImageIcon icon=new ImageIcon("c:\\123.jpg");
s.drawImage(icon.getImage(),0,0,400,200,jf);

}


};//生成自定义容器
为什么会被执行,因为会存在绘制窗体的这个过程,所以会执行。
jp=null;
jp=new JPanel(){

public void paintComponent(Graphics s)
{
System.out.println("被重写");
ImageIcon icon=new ImageIcon("C:\\124.jpg");
s.drawImage(icon.getImage(),0,0,400,200,jf);

}

};
而这部分绘制是在夹在按钮事件中,而按钮事件中并不存在重新绘制的处理 所以是不会执行的。 你在点击按钮之后,你放大或者缩小窗体,就会执行 因为这时就牵扯到重新绘制了。
神奇大自然 2017-08-22
  • 打赏
  • 举报
回复
其中 这里面的方法没有调用

62,625

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧