代码如下:
import java.awt.*import java.awt.event.*
import javax.swing.*
public class GraExp extends JFrame
{
CInstead c1=new CInstead()
Container c
JLabel lbl1=new JLabel("姓名:" )
JLabel lbl2=new JLabel("密码:" )
JTextField tf1=new JTextField(10),
tf2=new JTextField(10)
public GraExp()
{
setContentPane(c1)
c=getContentPane()
c.setLayout(new FlowLayout(FlowLayout.LEFT))
c.add(lbl1)
c.add(tf1)
c.add(lbl2)
c.add(tf2)
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE)
setSize(new Dimension(400,300))
show()
}
public static void main(String[] args)
{
GraExp ge=new GraExp()
}
class CInstead extends JPanel
{
ImageIcon icon
Image img
public CInstead()
{
icon=new ImageIcon(GraExp.class.getResource("333.png" ))
img=icon.getImage()
}
public void paintComponent(Graphics g)
{
super.paintComponent(g)
g.drawImage(img,0,0,null )
}
}
}
程序分析:
JComponent.paint先绘制组件,然后绘制组件的边框,再绘制组件的子组件。调用的顺序确保组件、边框和子组件都是可视的。如果组件有一个 UI代表,则JComponent.paintComponent调用该代表的Update方法,该方法为不透明组件擦除背景,然后绘制组件。
CInstead是一个不透明的组件,如果重载paint方法,其背景图是无法被擦除的,因此,即使更新了组件的所有包含组件,在界面上是看不到的。所以必须重载paintComponent方法,在绘制子组件前先擦除背景。
对双缓存组件,paint方法负责把组件绘制到屏外缓存中,然后把屏外缓存拷贝到组件的屏上代表中,正因为如此,不建议为Swing组件重载paint,如果需要重新定义如何绘制组件,那么就重载paintComponent()。
package test.arrayimport java.awt.Graphics
import java.awt.GridLayout
import java.awt.event.ActionEvent
import java.awt.event.ActionListener
import java.awt.image.BufferedImage
import java.io.File
import java.io.IOException
import javax.imageio.ImageIO
import javax.swing.JButton
import javax.swing.JFrame
import javax.swing.JLabel
import javax.swing.JPanel
import javax.swing.JPasswordField
import javax.swing.JTextField
public class Login implements ActionListener {
public static void main(String args[]) {
new Login().face()
}
public void face() {
JFrame jf
JLabel j1, j2
JTextField jtf
JPasswordField jd
JButton jb1, jb2
//JPanel p1, p2, p3
jf = new JFrame("登陆窗口")
j1 = new JLabel("用户名")
j2 = new JLabel("密 码")
jtf = new JTextField(15)
jd = new JPasswordField(15)
jb1 = new JButton("确定")
jb2 = new JButton("取消")
// p1 = new JPanel()
// p2 = new JPanel()
// p3 = new JPanel()
JPanel p = new JPanel() {
private static final long serialVersionUID = 1L
@Override
protected void paintComponent(Graphics g) {
try {
BufferedImage img = ImageIO.read(new File(this.getClass().getResource("1.jpg").getPath()))
g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), null)
} catch (IOException e) {
e.printStackTrace()
}
}
}
p.setLayout(null)
//p.add(p1)
//p.add(p2)
//p.add(p3)
j1.setBounds(50, 10, 60, 20)
p.add(j1)
jtf.setBounds(120, 10, 120, 20)
p.add(jtf)
j2.setBounds(50, 40, 60, 20)
p.add(j2)
jd.setBounds(120, 40, 120, 20)
p.add(jd)
jb1.setBounds(80, 70, 60, 20)
p.add(jb1)
jb2.setBounds(160, 70, 60, 20)
p.add(jb2)
jf.add(p)
jb1.addActionListener(this)
jb2.addActionListener(this)
jf.setBounds(300, 200, 280, 160)
jf.setResizable(false)
jf.setVisible(true)
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
}
public void actionPerformed(ActionEvent e) {
String s = e.getActionCommand()
if (s.equals("确定")) {
} else {
System.exit(0)
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)