Javaswing 怎样给界面添加背景图片呢?
现代码:packagemypackage;importjavax.swing.*;importjava.awt.*;publicclassHelloWorldextend...
现代码:
package mypackage;
import javax.swing.*;
import java.awt.*;
public class HelloWorld extends JPanel
{
//构造框架
static final int WIDTH=500;
static final int HEIGHT=300;
static JFrame loginframe;
public void add(Component c,GridBagConstraints constraints,int x,int y,int w,int h)
{
constraints.gridx=x;
constraints.gridy=y;
constraints.gridwidth=w;
constraints.gridheight=h;
add(c,constraints);
}
{
loginframe=new JFrame("青春同学录");
loginframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagLayout lay=new GridBagLayout();
setLayout(lay);
loginframe.add(this,BorderLayout.WEST);
loginframe.setSize(WIDTH,HEIGHT);
Toolkit kit=Toolkit.getDefaultToolkit();
Dimension screenSize=kit.getScreenSize();
int width=screenSize.width;
int height=screenSize.height;
int x=(width-WIDTH)/2;
int y=(height-HEIGHT)/2;
loginframe.setLocation(x, y);
JButton ok=new JButton("登录");
JButton cancel=new JButton("放弃");
JLabel title=new JLabel("青春,朋友一直在这里");
JLabel name=new JLabel("用户名");
JLabel password=new JLabel("密码");
final JTextField nameinput=new JTextField(15);
final JTextField passwordinput= new JTextField(15);
GridBagConstraints constraints=new GridBagConstraints();
constraints.fill=GridBagConstraints.NONE;
constraints.anchor= GridBagConstraints.EAST;
constraints.weightx=3;
constraints.weighty=4;
add(title,constraints,0,0,4,1);
add(name,constraints,0,1,1,1);
add(password,constraints,0,2,1,1);
add(nameinput,constraints,2,1,1,1);
add(passwordinput,constraints,2,2,1,1);
add(ok,constraints,0,3,1,1);
add(cancel,constraints,2,3,1,1);
loginframe.setResizable(false);
loginframe.setVisible(true);
}
public static void main(String[] args)
{
HelloWorld hello=new HelloWorld();
}
} 展开
package mypackage;
import javax.swing.*;
import java.awt.*;
public class HelloWorld extends JPanel
{
//构造框架
static final int WIDTH=500;
static final int HEIGHT=300;
static JFrame loginframe;
public void add(Component c,GridBagConstraints constraints,int x,int y,int w,int h)
{
constraints.gridx=x;
constraints.gridy=y;
constraints.gridwidth=w;
constraints.gridheight=h;
add(c,constraints);
}
{
loginframe=new JFrame("青春同学录");
loginframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagLayout lay=new GridBagLayout();
setLayout(lay);
loginframe.add(this,BorderLayout.WEST);
loginframe.setSize(WIDTH,HEIGHT);
Toolkit kit=Toolkit.getDefaultToolkit();
Dimension screenSize=kit.getScreenSize();
int width=screenSize.width;
int height=screenSize.height;
int x=(width-WIDTH)/2;
int y=(height-HEIGHT)/2;
loginframe.setLocation(x, y);
JButton ok=new JButton("登录");
JButton cancel=new JButton("放弃");
JLabel title=new JLabel("青春,朋友一直在这里");
JLabel name=new JLabel("用户名");
JLabel password=new JLabel("密码");
final JTextField nameinput=new JTextField(15);
final JTextField passwordinput= new JTextField(15);
GridBagConstraints constraints=new GridBagConstraints();
constraints.fill=GridBagConstraints.NONE;
constraints.anchor= GridBagConstraints.EAST;
constraints.weightx=3;
constraints.weighty=4;
add(title,constraints,0,0,4,1);
add(name,constraints,0,1,1,1);
add(password,constraints,0,2,1,1);
add(nameinput,constraints,2,1,1,1);
add(passwordinput,constraints,2,2,1,1);
add(ok,constraints,0,3,1,1);
add(cancel,constraints,2,3,1,1);
loginframe.setResizable(false);
loginframe.setVisible(true);
}
public static void main(String[] args)
{
HelloWorld hello=new HelloWorld();
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询