swing更新当前窗体

所用方法:

JPanel.updateUI();    更新当前窗体

示例:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Demos extends JFrame{
	JPanel jPanel;
	public Demos() {
	setTitle("更新窗体");
	setBounds(100, 100, 600, 450);
	setDefaultCloseOperation(EXIT_ON_CLOSE);
	Container c =getContentPane();
	
	 jPanel = new JPanel();
	jPanel.setLayout(null);
	JButton JB = new JButton("点击我你会看到惊喜的");
	JB.setFont(new Font("华文行楷", 1, 22));
	JB.setForeground(Color.BLUE);
	JB.setBorder(BorderFactory.createLineBorder(Color.BLUE));
	JB.setBounds(0, 0, 600, 80);
	c.add(jPanel);
	jPanel.add(JB);
	JB.addActionListener(new ActionListener() {
		
		public void actionPerformed(ActionEvent arg0) {
			//JOptionPane.showMessageDialog(Demos.this, "点击按钮");
			//jPanel.removeAll();
			JLabel jLabel = new JLabel("你的大名在此:");
			jLabel.setFont(new Font("华文行楷", 1, 22));
			jLabel.setForeground(Color.RED);
			jLabel.setBounds(150,150,180,40);
			jPanel.add(jLabel);
			JLabel jLabel1 = new JLabel("你的密码在此:");
			jLabel1.setFont(new Font("华文行楷", 1, 22));
			jLabel1.setForeground(Color.RED);
			jLabel1.setBounds(150,250,180,40);
			jPanel.add(jLabel1);
			
			JTextField jTextField = new JTextField(20);
			
			jTextField.setBounds(300,150,200,30);
			jPanel.add(jTextField);
			
			JTextField jTextField1 = new JTextField(20);
			jTextField1.setBounds(300,250,200,30);
			jPanel.add(jTextField1);
			
			JButton JB1 = new JButton("确认");
			JB1.setFont(new Font("华文行楷", 1, 22));
			JB1.setForeground(Color.BLUE);
			JB1.setBorder(BorderFactory.createLineBorder(Color.BLUE));
			JB1.setBounds(180, 350, 100, 30);
			
			JButton JB2 = new JButton("取消");
			JB2.setFont(new Font("华文行楷", 1, 22));
			JB2.setForeground(Color.BLUE);
			JB2.setBorder(BorderFactory.createLineBorder(Color.BLUE));
			JB2.setBounds(320, 350, 100, 30);
			
			jPanel.add(JB1);
			jPanel.add(JB2);
			
			jPanel.updateUI();         //更新当前窗体方法
		}
	});
	
	setVisible(true);
	}
	public static void main(String[] args) {
		new Demos();
	}

}

结果:

点击按钮后在当前窗体出现以下内容:

猜你喜欢

转载自blog.csdn.net/zxdspaopao/article/details/85245003