java图形界面图片,覆盖文字怎么办
2个回答
展开全部
重画面板,将图片当成背景,在图片之上画控件
参考下我的这串代码
public void serverframe() {
int x, y;
x = 600;
y = 480;
Toolkit theKit = getToolkit();
Dimension winSize = theKit.getScreenSize(); //获取当前显示器分辨率
setBounds((winSize.width - x) / 2, (winSize.height - y) / 2, x, y); //主窗体在屏幕居
this.setTitle("简易局域网聊天工具");
Toolkit kit =Toolkit.getDefaultToolkit();//更改左上角小图标
Image image = kit.createImage(getClass().getResource("images/10.png"));
this.setIconImage(image);
setBak(); //调用背景方法
Container c = getContentPane(); //获取JFrame面板
JPanel jp = new JPanel(); //创建个JPanel
jp.setOpaque(false); //把JPanel设置为透明 这样就不会遮住后面的背景 这样就能在JPanel随意加组件
c.add(jp);
setLayout(null);
label.setBounds(new Rectangle(20, 0, 460, 20));
txarea.setBounds(new Rectangle(20, 20, 460, 280));
label1.setBounds(new Rectangle(20, 320, 460, 20));
txfield.setBounds(new Rectangle(20, 340, 460, 90));
btnend.setBounds(new Rectangle(490, 20, 100, 30));
btnend1.setBounds(new Rectangle(490, 55, 100, 30));
btnend2.setBounds(new Rectangle(490, 90, 100, 30));
btnend3.setBounds(new Rectangle(490, 125, 100, 30));
btnend4.setBounds(new Rectangle(490, 160, 100, 30));
btnend5.setBounds(new Rectangle(490, 195, 100, 30));
btnend6.setBounds(new Rectangle(490, 230, 100, 30));
btnend7.setBounds(new Rectangle(490, 265, 100, 30));
btnend8.setBounds(new Rectangle(490, 380, 100, 50));
add(label);
add(txarea);
add(label1);
add(txfield);
add(btnend);
add(btnend1);
add(btnend2);
add(btnend3);
add(btnend4);
add(btnend5);
add(btnend6);
add(btnend7);
add(btnend8);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
txfield.addActionListener(new textlistener());
this.setResizable(false);
setVisible(true);
connect();
}
public void setBak() { //设置窗口背景
((JPanel) this.getContentPane()).setOpaque(false);
ImageIcon img = new ImageIcon(getClass().getResource("images/1.jpg"));
JLabel background = new JLabel(img);
this.getLayeredPane().add(background, new Integer(Integer.MIN_VALUE));
background.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
}
参考下我的这串代码
public void serverframe() {
int x, y;
x = 600;
y = 480;
Toolkit theKit = getToolkit();
Dimension winSize = theKit.getScreenSize(); //获取当前显示器分辨率
setBounds((winSize.width - x) / 2, (winSize.height - y) / 2, x, y); //主窗体在屏幕居
this.setTitle("简易局域网聊天工具");
Toolkit kit =Toolkit.getDefaultToolkit();//更改左上角小图标
Image image = kit.createImage(getClass().getResource("images/10.png"));
this.setIconImage(image);
setBak(); //调用背景方法
Container c = getContentPane(); //获取JFrame面板
JPanel jp = new JPanel(); //创建个JPanel
jp.setOpaque(false); //把JPanel设置为透明 这样就不会遮住后面的背景 这样就能在JPanel随意加组件
c.add(jp);
setLayout(null);
label.setBounds(new Rectangle(20, 0, 460, 20));
txarea.setBounds(new Rectangle(20, 20, 460, 280));
label1.setBounds(new Rectangle(20, 320, 460, 20));
txfield.setBounds(new Rectangle(20, 340, 460, 90));
btnend.setBounds(new Rectangle(490, 20, 100, 30));
btnend1.setBounds(new Rectangle(490, 55, 100, 30));
btnend2.setBounds(new Rectangle(490, 90, 100, 30));
btnend3.setBounds(new Rectangle(490, 125, 100, 30));
btnend4.setBounds(new Rectangle(490, 160, 100, 30));
btnend5.setBounds(new Rectangle(490, 195, 100, 30));
btnend6.setBounds(new Rectangle(490, 230, 100, 30));
btnend7.setBounds(new Rectangle(490, 265, 100, 30));
btnend8.setBounds(new Rectangle(490, 380, 100, 50));
add(label);
add(txarea);
add(label1);
add(txfield);
add(btnend);
add(btnend1);
add(btnend2);
add(btnend3);
add(btnend4);
add(btnend5);
add(btnend6);
add(btnend7);
add(btnend8);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
txfield.addActionListener(new textlistener());
this.setResizable(false);
setVisible(true);
connect();
}
public void setBak() { //设置窗口背景
((JPanel) this.getContentPane()).setOpaque(false);
ImageIcon img = new ImageIcon(getClass().getResource("images/1.jpg"));
JLabel background = new JLabel(img);
this.getLayeredPane().add(background, new Integer(Integer.MIN_VALUE));
background.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
}
追问
我是后加的图片先前的字已经打好了。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询