首页 > 学院 > 开发设计 > 正文

java 窗体程序swing

2019-11-14 10:38:45
字体:
来源:转载
供稿:网友

以下代码是修改他人的源码,增加一些注释。

利用以下的功能基本可以写一个小的窗体程序了。

package test2;import java.awt.BorderLayout;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.GridLayout;import java.awt.Insets;import java.awt.event.ActionEvent;import java.util.Arrays;import java.util.List;import javax.swing.AbstractAction;import javax.swing.Action;import javax.swing.BorderFactory;import javax.swing.JButton;import javax.swing.JComponent;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JPassWordField;import javax.swing.JTextField;import javax.swing.SwingUtilities;import javax.swing.UIManager; /** * @author Administrator * */public class MyFrameTow extends JPanel {	PRivate static final String LABEL_USERNAME = "用户账号:";	private static final String LABEL_PASSWORD = "用户密码:";	private static final String LABEL_OK = "确定";	private static final String LABEL_Cancel = "取消";	private static final int FIELD_COLS = 20;	private JTextField fieldUsername;	private JPasswordField fieldPassword;	private Action actionOK;	private Action actionCancel;			public static void main(String[] args) {		SwingUtilities.invokeLater(new Runnable() {			@Override			public void run() {				try {					UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());				} catch (Exception e) {					e.printStackTrace();				}				JFrame test = new JFrame("欢迎进入本系统");				test.setContentPane(new MyFrameTow());				test.pack();				test.setResizable(false);				test.setLocationRelativeTo(null);				test.setDefaultCloSEOperation(JFrame.DISPOSE_ON_CLOSE);				test.setVisible(true);			}		});	}	MyFrameTow() {		super(new BorderLayout(5, 5));		assert SwingUtilities.isEventDispatchThread();		// 文本框		fieldUsername = new JTextField(FIELD_COLS);		// 密码框		fieldPassword = new JPasswordField(FIELD_COLS);		actionOK = new AbstractAction(LABEL_OK) {			@Override			public void actionPerformed(ActionEvent e) {				// @TODO to be implemented. 实现确认按钮的操作				//TODO				String text = fieldUsername.getText();				if (text.isEmpty()) {					JOptionPane.showMessageDialog(null, "用户名不能为空!", "提示", JOptionPane.ERROR_MESSAGE);					return;				}								JOptionPane.showMessageDialog(null, "这是一个假登录"+text, "提示", JOptionPane.ERROR_MESSAGE); 			}		};		actionCancel = new AbstractAction(LABEL_Cancel) {			@Override			public void actionPerformed(ActionEvent e) {				// @TODO to be implemented. 实现取消按钮的操作				//退出程序				System.exit(0);			}		};		//  BorderLayout.NORTH 这是指定向北浮动		add(layoutText(Arrays.asList( "第一行文字", "第二行文字", "第三行文字")), BorderLayout.NORTH);		add(layoutFields(), BorderLayout.CENTER);		add(layoutControl(), BorderLayout.SOUTH);		setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));	}	private JComponent layoutControl() {		// 控件布局  		JComponent result = new JPanel();		// 这里是确认 和 取消的按钮		result.add(new JButton(actionOK));		result.add(new JButton(actionCancel));		return result;	}	private JComponent layoutFields() {		// 布局位置		JComponent result = new JPanel(new GridBagLayout());		GridBagConstraints gbc = new GridBagConstraints();		gbc.gridx = 0;		gbc.gridy = 0;		//固定 向西		gbc.anchor = GridBagConstraints.WEST;		//是否填充 		gbc.fill = GridBagConstraints.NONE;		gbc.insets = new Insets(5, 5, 5, 5);		//加入文字描述		result.add(new JLabel(getBolDHTML(LABEL_USERNAME)), gbc);		gbc.gridy++;		result.add(new JLabel(getBoldHTML(LABEL_PASSWORD)), gbc);		gbc.gridx++;		gbc.gridy = 0;		gbc.fill = GridBagConstraints.HORIZONTAL;		result.add(fieldUsername, gbc);		gbc.gridy++;		result.add(fieldPassword, gbc);		return result;	}	private JComponent layoutText(List<String> lines) {		assert lines != null;		JComponent result = new JPanel(new GridLayout(lines.size(), 1));		for (String line : lines)			result.add(new JLabel(line));		result.setBorder(BorderFactory.createEmptyBorder(5, 10, 0, 10));		return result;	}	private String getBoldHTML(String s) {		// 设置字体的大小粗细		return "<html><b>" + s + "</b></html>";	}}

这里是打包jar 的教程:http://blog.csdn.net/QQ_27292113/article/details/54632963
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表