登录框注册按钮点击改变窗口大小——出现注册面板
首先用swt可视化设计登录窗口如下图:
此时窗口高度为578
没点击注册时高度为301(可自己定)
注意:注册用户的Jpanel 的border选择Title Border,title属性是“注册用户”
布局Layout选择Absolute Layout
接着,对话框窗口设计好后,双击注册按钮,进行代码编辑,在注册按钮的监听代码中增加一个if判断,当等于301,就给改为窗口高度578,否则改为301
因为使用的是匿名内部类,不能直接使用this.关键字调用getHeight()方法和setHeight()方法,所以用LoginDialog.this.的方法进行调用
完整代码如下:
package com.swift; import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextField; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; import javax.swing.border.TitledBorder; public class LoginDialog extends JDialog { private JPasswordField passwordField_2; private JPasswordField passwordField_1; private JTextField textField_2; private JTextField textField; public static void main(String args[]) { JFrame.setDefaultLookAndFeelDecorated(true); JDialog.setDefaultLookAndFeelDecorated(true); try { UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel"); } catch (ClassNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (InstantiationException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IllegalAccessException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (UnsupportedLookAndFeelException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } EventQueue.invokeLater(new Runnable() { public void run() { try { LoginDialog dialog = new LoginDialog(); dialog.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); dialog.setVisible(true); } catch