JPanel没有直接设置背景图片的方法。如果你想做成有背景的jpanel,需要自己继承JComponent,重载paintComponent,画出设置的壁纸。很简单代码:
很显然这白的颜色太难看了,要去掉就需要这样设置一下:
/setOpaque()/如果为 true,则该组件绘制其边界内的所有像素。
jslider.setOpaque(false);
package com.swing.panel; import java.awt.BorderLayout; import java.awt.Color; import java.awt.EventQueue; import java.awt.Font; import java.awt.Label; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BorderFactory; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JSlider; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; import javax.swing.border.Border; import javax.swing.border.TitledBorder; public class Demo extends JFrame { private boolean isShowing; private JLabel jlabel; private JPanelWithPhoto jpanel; private JButton jbutton1; private JButton jbutton2; private JSlider jslider; private JLabel jlabel2; private JLabel jlabel3; public Demo(){ init(); } public void init(){ isShowing = false ; jlabel = new JLabel(); jlabel.setForeground(Color.red); //jlabel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Tweaking Tooltips", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), java.awt.Color.red)); jlabel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "good", TitledBorder.DEFAULT_POSITION , TitledBorder.DEFAULT_POSITION , new Font("utg-8", 0, 23), Color.green )); jlabel.setText("<html>\n<body>\n<center>\n<h2>\nPACIFIC OCEAN\n</h2>\n<p>\n<strong>\n<em>\nIt has been snowing for thousands of years!\n</em>\n</strong>\n</p>\n</center>\n</body>\n</html>"); jbutton1 = new JButton(); jbutton1.setText("begin...."); jbutton1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(isShowing == true){ jbutton1.setText("end!!!"); }else{ jbutton1.setText("begin..."); } } }); jbutton2 = new JButton(); jbutton2.setText("stop"); jbutton2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub System.exit(0); } }); jslider = new JSlider(); jslider.setForeground(Color.white); jslider.setMaximum(10); jslider.setMinimum(1); jslider.setValue(5); //jslider.setVisible(true); // //如果为 true,则该组件绘制其边界内的所有像素。 jslider.setOpaque(false); jlabel2 = new JLabel("less"); jlabel3 = new JLabel("more"); jpanel = new JPanelWithPhoto(new ImageIcon(this.getClass().getResource("pacific.jpg"))); //jpanel.setIcon(new ImageIcon(this.getClass().getResource("pacific.jpg"))); javax.swing.GroupLayout wallpaperPanel1Layout = new javax.swing.GroupLayout(jpanel); jpanel.setLayout(wallpaperPanel1Layout); wallpaperPanel1Layout.setHorizontalGroup( wallpaperPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(wallpaperPanel1Layout.createSequentialGroup() .addContainerGap() .addGroup(wallpaperPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jlabel, javax.swing.GroupLayout.DEFAULT_SIZE, 437, Short.MAX_VALUE) .addGroup(wallpaperPanel1Layout.createSequentialGroup() .addComponent(jlabel2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jslider, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jlabel3) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 65, Short.MAX_VALUE) .addComponent(jbutton1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jbutton2))) .addContainerGap()) ); wallpaperPanel1Layout.setVerticalGroup( wallpaperPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, wallpaperPanel1Layout.createSequentialGroup() .addContainerGap() .addComponent(jlabel, javax.swing.GroupLayout.DEFAULT_SIZE, 349, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(wallpaperPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(wallpaperPanel1Layout.createSequentialGroup() .addGap(8, 8, 8) .addGroup(wallpaperPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jbutton2) .addComponent(jbutton1))) .addGroup(wallpaperPanel1Layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(wallpaperPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jslider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jlabel2) .addComponent(jlabel3)))) .addContainerGap()) ); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jpanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jpanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ); pack(); } public static void main(String args[]){ try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnsupportedLookAndFeelException e) { // TODO Auto-generated catch block e.printStackTrace(); } EventQueue.invokeLater(new Runnable() { @Override public void run() { // TODO Auto-generated method stub new Demo().setVisible(true); } }); } }
package com.swing.panel; import java.awt.Graphics; import javax.swing.Icon; import javax.swing.JPanel; public class JPanelWithPhoto extends JPanel { private Icon icon ; public Icon getIcon() { return icon; } public void setIcon(Icon icon) { this.icon = icon; } public JPanelWithPhoto(Icon icon){ this.icon = icon ; } @Override protected void paintComponent(Graphics g) { if(icon!=null) icon.paintIcon(this, g, 0, 0); }}
package com.swing.panel; import java.awt.BorderLayout; import java.awt.Color; import java.awt.EventQueue; import java.awt.Font; import java.awt.Label; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BorderFactory; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JSlider; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; import javax.swing.border.Border; import javax.swing.border.TitledBorder; public class Demo extends JFrame { private boolean isShowing; private JLabel jlabel; private JPanelWithPhoto jpanel; private JButton jbutton1; private JButton jbutton2; private JSlider jslider; private JLabel jlabel2; private JLabel jlabel3; public Demo(){ init(); } public void init(){ isShowing = false ; jlabel = new JLabel(); jlabel.setForeground(Color.red); //jlabel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Tweaking Tooltips", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), java.awt.Color.red)); jlabel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "good", TitledBorder.DEFAULT_POSITION , TitledBorder.DEFAULT_POSITION , new Font("utg-8", 0, 23), Color.green )); jlabel.setText("<html>\n<body>\n<center>\n<h2>\nPACIFIC OCEAN\n</h2>\n<p>\n<strong>\n<em>\nIt has been snowing for thousands of years!\n</em>\n</strong>\n</p>\n</center>\n</body>\n</html>"); jbutton1 = new JButton(); jbutton1.setText("begin...."); jbutton1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(isShowing == true){ jbutton1.setText("end!!!"); }else{ jbutton1.setText("begin..."); } } }); jbutton2 = new JButton(); jbutton2.setText("stop"); jbutton2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub System.exit(0); } }); jslider = new JSlider(); jslider.setForeground(Color.white); jslider.setMaximum(10); jslider.setMinimum(1); jslider.setValue(5); //jslider.setVisible(true); // //如果为 true,则该组件绘制其边界内的所有像素。 jslider.setOpaque(false); jlabel2 = new JLabel("less"); jlabel3 = new JLabel("more"); jpanel = new JPanelWithPhoto(new ImageIcon(this.getClass().getResource("pacific.jpg"))); //jpanel.setIcon(new ImageIcon(this.getClass().getResource("pacific.jpg"))); javax.swing.GroupLayout wallpaperPanel1Layout = new javax.swing.GroupLayout(jpanel); jpanel.setLayout(wallpaperPanel1Layout); wallpaperPanel1Layout.setHorizontalGroup( wallpaperPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(wallpaperPanel1Layout.createSequentialGroup() .addContainerGap() .addGroup(wallpaperPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jlabel, javax.swing.GroupLayout.DEFAULT_SIZE, 437, Short.MAX_VALUE) .addGroup(wallpaperPanel1Layout.createSequentialGroup() .addComponent(jlabel2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jslider, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jlabel3) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 65, Short.MAX_VALUE) .addComponent(jbutton1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jbutton2))) .addContainerGap()) ); wallpaperPanel1Layout.setVerticalGroup( wallpaperPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, wallpaperPanel1Layout.createSequentialGroup() .addContainerGap() .addComponent(jlabel, javax.swing.GroupLayout.DEFAULT_SIZE, 349, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(wallpaperPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(wallpaperPanel1Layout.createSequentialGroup() .addGap(8, 8, 8) .addGroup(wallpaperPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jbutton2) .addComponent(jbutton1))) .addGroup(wallpaperPanel1Layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(wallpaperPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jslider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jlabel2) .addComponent(jlabel3)))) .addContainerGap()) ); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jpanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jpanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ); pack(); } public static void main(String args[]){ try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnsupportedLookAndFeelException e) { // TODO Auto-generated catch block e.printStackTrace(); } EventQueue.invokeLater(new Runnable() { @Override public void run() { // TODO Auto-generated method stub new Demo().setVisible(true); } }); } }
package com.swing.panel; import java.awt.Graphics; import javax.swing.Icon; import javax.swing.JPanel; public class JPanelWithPhoto extends JPanel { private Icon icon ; public Icon getIcon() { return icon; } public void setIcon(Icon icon) { this.icon = icon; } public JPanelWithPhoto(Icon icon){ this.icon = icon ; } @Override protected void paintComponent(Graphics g) { if(icon!=null) icon.paintIcon(this, g, 0, 0); }}