如何在swt desiger中实现button的清空与登录功能即如何修改下列代码package com.accp.zxt;import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;public class Mine extends JFrame {
private JPasswordField passwordField;
private JTextField textField;
/**
 * Launch the application
 * @param args
 */
public static void main(String args[]) {
try {
Mine frame = new Mine();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
} /**
 * Create the frame
 */
public Mine() {
super();
getContentPane().setLayout(null);
setBounds(100, 100, 257, 313);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JButton button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});


button.setText("登录");
button.setBounds(56, 197, 57, 23);
getContentPane().add(button); final JButton button_1 = new JButton();
button_1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {



}
});
button_1.setText("清空");
button_1.setBounds(135, 197, 57, 23);
getContentPane().add(button_1); textField = new JTextField();
textField.setBounds(107, 60, 85, 23);
getContentPane().add(textField); passwordField = new JPasswordField();
passwordField.setBounds(107, 112, 85, 23);
getContentPane().add(passwordField); final JLabel label = new JLabel();
label.setText("用户名");
label.setBounds(56, 60, 45, 23);
getContentPane().add(label); final JLabel label_1 = new JLabel();
label_1.setText("密  码");
label_1.setBounds(56, 116, 45, 15);
getContentPane().add(label_1);
//
}}

解决方案 »

  1.   


    import org.eclipse.swt.SWT;
    import org.eclipse.swt.events.SelectionAdapter;
    import org.eclipse.swt.events.SelectionEvent;
    import org.eclipse.swt.widgets.Button;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Label;
    import org.eclipse.swt.widgets.Shell;
    import org.eclipse.swt.widgets.Text;public class LoginDemo extends Shell
    {    private Text txtPsd;    private Text txtUser;    /**
         * Launch the application
         * @param args
         */
        public static void main(String args[])
        {
            try
            {
                Display display = Display.getDefault();
                LoginDemo shell = new LoginDemo(display, SWT.SHELL_TRIM);
                shell.open();
                shell.layout();
                
                while(!shell.isDisposed())
                {
                    if(!display.readAndDispatch())
                    {
                        display.sleep();
                    }
                }
                
                display.dispose();
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }    /**
         * Create the shell
         * @param display
         * @param style
         */
        public LoginDemo(Display display, int style)
        {
            super(display, style);
            createContents();
        }    /**
         * Create contents of the window
         */
        protected void createContents()
        {
            setText("SWT Application");
            setSize(416, 279);        final Label lbUser = new Label(this, SWT.NONE);
            lbUser.setText("用户名");
            lbUser.setBounds(45, 80, 52, 21);        final Label lbPsd = new Label(this, SWT.NONE);
            lbPsd.setText("密  码");
            lbPsd.setBounds(45, 120, 52, 21);        txtUser = new Text(this, SWT.BORDER);
            txtUser.setBounds(103, 75, 260, 25);        txtPsd = new Text(this, SWT.BORDER);
            txtPsd.setEchoChar('*');
            txtPsd.setBounds(103, 113, 260, 25);        final Button btnLogin = new Button(this, SWT.NONE);
            btnLogin.addSelectionListener(new SelectionAdapter()
            {
                public void widgetSelected(final SelectionEvent e)
                {
                    
                }
            });
            btnLogin.setText("登陆");
            btnLogin.setBounds(80, 191, 84, 25);        final Button btnClear = new Button(this, SWT.NONE);
            btnClear.addSelectionListener(new SelectionAdapter()
            {
                public void widgetSelected(final SelectionEvent e)
                {
                    
                }
            });
            btnClear.setText("清空");
            btnClear.setBounds(244, 191, 84, 25);
            //
        }    @Override
        protected void checkSubclass()
        {
        // Disable the check that prevents subclassing of SWT components
        }
    }