下面这段java代码执行后结果为什么只显示背景图片而不显示控件?
importjavax.swing.*;importjava.awt.*;importjava.util.*;publicclassTest2{JFrameframe;J...
import javax.swing.*;
import java.awt.*;
import java.util.*;
public class Test2 {
JFrame frame;
JTextField field;
JPanel panel;
JButton button;
JLabel label;
public void myframe(){
frame=new JFrame("Test");
frame.setSize(300,300);
frame.setVisible(true);
Container c2=frame.getLayeredPane();
c2.setLayout(new BorderLayout());
c2.add(new MyComponent());
JPanel c=(JPanel)frame.getContentPane();
c.setOpaque(false);
c.setLayout(new FlowLayout());
c.add(field=new JTextField(10));
c.add(button=new JButton("ok"));
}
public static void main(String[] agrs){
new Test2().myframe();
}
}
class MyComponent extends JComponent{
public void paintComponent(Graphics g){
Image image=new ImageIcon("c://first.jpg").getImage();
g.drawImage(image,0,0,300,300,null);
}
} 展开
import java.awt.*;
import java.util.*;
public class Test2 {
JFrame frame;
JTextField field;
JPanel panel;
JButton button;
JLabel label;
public void myframe(){
frame=new JFrame("Test");
frame.setSize(300,300);
frame.setVisible(true);
Container c2=frame.getLayeredPane();
c2.setLayout(new BorderLayout());
c2.add(new MyComponent());
JPanel c=(JPanel)frame.getContentPane();
c.setOpaque(false);
c.setLayout(new FlowLayout());
c.add(field=new JTextField(10));
c.add(button=new JButton("ok"));
}
public static void main(String[] agrs){
new Test2().myframe();
}
}
class MyComponent extends JComponent{
public void paintComponent(Graphics g){
Image image=new ImageIcon("c://first.jpg").getImage();
g.drawImage(image,0,0,300,300,null);
}
} 展开
1个回答
2016-10-27
展开全部
Image image=new ImageIcon("c://first.jpg").getImage();
最好 图片,先加载好
最好 图片,先加载好
追问
为什么呢?
是不是加载图片会卡,用新线程还会这样吗
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询