Java swing 加完图片按钮以后覆盖了所有。代码如下
packagedemo;publicclassdemo_01{privateJDesktopPanedesktopPane;privateJFrameframe;priv...
package demo;
public class demo_01 {
private JDesktopPane desktopPane;
private JFrame frame;
private JLabel backLabel;
private JPanel myPanel;
public Toolkit toolkit = Toolkit.getDefaultToolkit() ;
public Dimension sc = toolkit.getScreenSize() ;
public JLabel name1 = new JLabel("姓 名") ;
public JTextField textName1 = new JTextField() ;
public Font font1 = new Font("宋体",4,25) ;
public demo_01(){
frame = new JFrame("测试");
frame.addComponentListener(new ComponentAdapter(){
public void componentResized(ComponentEvent e){
backLabel.setSize(frame.getWidth(),frame.getHeight());
backLabel.setText("<html><body><image width='"+frame.getWidth()+"'height='"+(frame.getHeight()-110)+"'src="+demo_01.this.getClass().getResource("tianxiexinxi.jpg")+"'></img></body></html>");
}
});
frame.getContentPane().setLayout(new BorderLayout(5,5));
frame.setBounds(0,0,2550,1440);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
backLabel=new JLabel();
backLabel.setVerticalAlignment(SwingConstants.TOP);
backLabel.setHorizontalAlignment(SwingConstants.CENTER);
frame.setLayout(new BorderLayout(5,5));
name1.setForeground(Color.blue);
name1.setSize(200,70);
name1.setFont(font1) ;
name1.setLocation(420, 210);
frame.add(name1,BorderLayout.CENTER);
name4.setForeground(Color.blue);
textName1.setBounds(540, 227, 400, 33);
frame.add(textName1);
desktopPane=new JDesktopPane();
desktopPane.add(backLabel,new Integer(Integer.MIN_VALUE));
frame.getContentPane().add(desktopPane);
//完成
JButton icon1=new JButton(new ImageIcon("res/wancheng.png"));
myPanel = new JPanel();
myPanel.add(icon1);
myPanel.setSize(414, 129);
frame.add(myPanel);
icon1.setSize(414,129);
icon1.setLocation(0, 0);
frame.setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
new demo_01();
}
});
}
}
加完图片按钮之后,之前的背景就不见了。肿么破啊? 展开
public class demo_01 {
private JDesktopPane desktopPane;
private JFrame frame;
private JLabel backLabel;
private JPanel myPanel;
public Toolkit toolkit = Toolkit.getDefaultToolkit() ;
public Dimension sc = toolkit.getScreenSize() ;
public JLabel name1 = new JLabel("姓 名") ;
public JTextField textName1 = new JTextField() ;
public Font font1 = new Font("宋体",4,25) ;
public demo_01(){
frame = new JFrame("测试");
frame.addComponentListener(new ComponentAdapter(){
public void componentResized(ComponentEvent e){
backLabel.setSize(frame.getWidth(),frame.getHeight());
backLabel.setText("<html><body><image width='"+frame.getWidth()+"'height='"+(frame.getHeight()-110)+"'src="+demo_01.this.getClass().getResource("tianxiexinxi.jpg")+"'></img></body></html>");
}
});
frame.getContentPane().setLayout(new BorderLayout(5,5));
frame.setBounds(0,0,2550,1440);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
backLabel=new JLabel();
backLabel.setVerticalAlignment(SwingConstants.TOP);
backLabel.setHorizontalAlignment(SwingConstants.CENTER);
frame.setLayout(new BorderLayout(5,5));
name1.setForeground(Color.blue);
name1.setSize(200,70);
name1.setFont(font1) ;
name1.setLocation(420, 210);
frame.add(name1,BorderLayout.CENTER);
name4.setForeground(Color.blue);
textName1.setBounds(540, 227, 400, 33);
frame.add(textName1);
desktopPane=new JDesktopPane();
desktopPane.add(backLabel,new Integer(Integer.MIN_VALUE));
frame.getContentPane().add(desktopPane);
//完成
JButton icon1=new JButton(new ImageIcon("res/wancheng.png"));
myPanel = new JPanel();
myPanel.add(icon1);
myPanel.setSize(414, 129);
frame.add(myPanel);
icon1.setSize(414,129);
icon1.setLocation(0, 0);
frame.setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
new demo_01();
}
});
}
}
加完图片按钮之后,之前的背景就不见了。肿么破啊? 展开
3个回答
展开全部
frame布局的问题,border布局要设定子view的方位
追问
您好。我最后把布局删掉了。使用绝对布局。其他组件的显示都正常。可是就是一添加按钮就会遮挡住背景。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
设置按钮的透明度。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
设置JButton透明啊
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询