JFrame中怎么添加背景图片,输入框的大小怎么修改 15
我用eclipse编写的如图,1、怎么添加背景图片2、输入框这么大不太好看,怎么调整大小啊,还有两个输入框之间的间距能调吗3、按钮之间的间距能修改吗...
我用eclipse编写的
如图,1、怎么添加背景图片
2、输入框这么大不太好看,怎么调整大小啊,还有两个输入框之间的间距能调吗
3、按钮之间的间距能修改吗 展开
如图,1、怎么添加背景图片
2、输入框这么大不太好看,怎么调整大小啊,还有两个输入框之间的间距能调吗
3、按钮之间的间距能修改吗 展开
展开全部
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import javax.imageio.ImageIO;
public class LoginExample {
public static void main(final String... args) {
EventQueue.invokeLater(new Runnable(){
@Override public void run(){
try {
final XPanel container = new XPanel(ImageIO.read(LoginExample.class.getResource("/images/background.jpg")));
final JPanel panel = new JPanel();
final GroupLayout layout = new GroupLayout(panel);
container.setLayout(new BorderLayout());
panel.setLayout(layout);
panel.setOpaque(false);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
final JLabel name = new JLabel("用户名");
final JLabel pass = new JLabel("密码");
final JTextField name_field = new JTextField(12);
final JPasswordField pass_field = new JPasswordField(12);
name_field.setOpaque(false);
pass_field.setOpaque(false);
GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
hGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING).
addComponent(name).addComponent(pass));
hGroup.addGroup(layout.createParallelGroup().
addComponent(name_field).addComponent(pass_field));
layout.setHorizontalGroup(hGroup);
GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
vGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE).
addComponent(name).addComponent(name_field));
vGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE).
addComponent(pass).addComponent(pass_field));
layout.setVerticalGroup(vGroup);
final JFrame frame = new JFrame("Login");
container.add(new JLabel("**欢迎您登录**", JLabel.CENTER), BorderLayout.PAGE_START);
container.add(panel);
frame.setContentPane(container);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
} catch (Exception e) {
}
}
});
}
public static class XPanel extends JPanel {
private BufferedImage background;
public XPanel(BufferedImage background) {
this.background = background;
}
@Override public void paintComponent(Graphics g) {
BufferedImage bufimg = new BufferedImage(getWidth(),getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = bufimg.createGraphics();
// restore the foreground color in case the superclass needs it
g2.setColor(g.getColor());
super.paintComponent(g2);
// do an alpha composite
Graphics2D gx = (Graphics2D) g;
gx.drawImage(background,0,0,null);
gx.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,0.8f));
gx.drawImage(bufimg,0,0,null);
}
}
}
2013-08-11
展开全部
如果你对布局十分在行的话,可以用绝对布局,就是用setLayout(null)设置空布局,然后用setLocation(int x, int y)和setSize(int width, int height)或setBounds(int x, int y, int width, int height)设置位置大小。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-08-11
展开全部
看看用的是哪个布局。界面不是太复杂,不使用布局,绝对bounds定位,还方便
背景在paintComponent事件,绘一张图片
背景在paintComponent事件,绘一张图片
追问
能不能说的具体点,才接触java,不是太懂呢
能不能说的具体点,才接触java,不是太懂呢
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询