JAVA setlocation()方法并不能定义组件的位置,setBounds()方法无法改变组件的大小。小白一枚
小白一枚,请问各位大神这是为啥。。。下面是代码importjava.awt.Window;importjavax.swing.ImageIcon;importjavax....
小白一枚,请问各位大神这是为啥。。。下面是代码
import java.awt.Window;
import javax.swing.ImageIcon;
import javax.swing.*;
public class Frame extends JFrame {
public static final int WIDTH=500;
public static final int HEIGHT=600;
public Frame(){
super("QQ对话框");
setSize(WIDTH,HEIGHT);
setLocation(600, 300);
this.setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String path = new String("C:\\Users\\Administrator\\Desktop\\DF11B95E663DFFC98D5DA4DE3EAE57E8.jpg");
ImageIcon background = new ImageIcon(path);
JLabel myLabelmag=new JLabel(background);
myLabelmag.setLayout(null);
myLabelmag.setBounds(0, 0,this.getWidth() ,this.getHeight() );
JPanel imagePanel = (JPanel)this.getContentPane();
imagePanel.setOpaque(false);
this.add(myLabelmag,new Integer(Integer.MIN_VALUE));
JLabel myLabel= new JLabel("This is a window that ");
this.add(myLabel);
myLabel.setLocation(230,200);
this.setVisible(true);
}
public static void main(String args[]){
new Frame();
}
} 展开
import java.awt.Window;
import javax.swing.ImageIcon;
import javax.swing.*;
public class Frame extends JFrame {
public static final int WIDTH=500;
public static final int HEIGHT=600;
public Frame(){
super("QQ对话框");
setSize(WIDTH,HEIGHT);
setLocation(600, 300);
this.setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String path = new String("C:\\Users\\Administrator\\Desktop\\DF11B95E663DFFC98D5DA4DE3EAE57E8.jpg");
ImageIcon background = new ImageIcon(path);
JLabel myLabelmag=new JLabel(background);
myLabelmag.setLayout(null);
myLabelmag.setBounds(0, 0,this.getWidth() ,this.getHeight() );
JPanel imagePanel = (JPanel)this.getContentPane();
imagePanel.setOpaque(false);
this.add(myLabelmag,new Integer(Integer.MIN_VALUE));
JLabel myLabel= new JLabel("This is a window that ");
this.add(myLabel);
myLabel.setLocation(230,200);
this.setVisible(true);
}
public static void main(String args[]){
new Frame();
}
} 展开
展开全部
把java控件所在的容器(比如:JFrame,JPanel)的布局设为空布局。(比如:jp.setLayout(null);
然后用java控件的setBounds函数设置java控件的位置和大小。(比如:JTextField jtf=new JTextField(5); jtf.setBounds(100,100,30,20);//100,100是在容器中的相对位置坐标x和坐标y
30,20是Java控件的大小,30是长,20是高。)
我给你个Java语言的例子,你看看吧。
import javax.swing.JButton;
import javax.swing.JFrame;
public class B {
B(){
JFrame jf=new JFrame();
jf.setLayout(null);
JButton jb=new JButton("确定");
jb.setBounds(25, 20, 60, 20);
jf.add(jb);
jf.setSize(200,100);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
public static void main(String[] args) {
new B();
}
}
运行结果:
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询