该范例主要是JFrame(框架)和Jpanel(画板),在Jpanel容器上添加控件,然后再把Jpanel放进JFrame的容器里面。
FrameDemo.java
import java.awt.Dimension; import java.awt.Toolkit; import javax.swing.JFrame; public class FrameDemo extends JFrame{ public static void main(String[] args) throws Exception { //获取主显示器屏幕大小 Toolkit kit = Toolkit.getDefaultToolkit(); Dimension screenSize = kit.getScreenSize(); int screenWidth = screenSize.width; int screenHeight = screenSize.height; //设置界面组件 ComponentDemo component = new ComponentDemo(); FrameDemo frameDemo = new FrameDemo(); frameDemo.setContentPane(component); //将Panel放入到Frame中 frameDemo.setSize(400, 500); frameDemo.setLocation(screenWidth / 2 - 390 / 2, screenHeight / 2 - 580 / 2); frameDemo.setVisible(true); } }
ComponentDemo.java
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.JTextPane; public class ComponentDemo extends JPanel implements ActionListener { //JLabel是标签,JTextField是文本框 private JLabel userLabel = null; private JTextField userText = null; private JLabel passwordLabel = null; private JTextField passwordText = null; private JButton btnLogin = null; private JLabel contentLabel = null; private JTextPane contentPane = null; //组件初始化 public ComponentDemo() { super(); initialize(); } private void initialize() { userLabel = new JLabel(); /*用setBounds(Rectangle r)画矩形,Rectangle(x,y,weight,height) x,y表示矩形的左上角坐标,weight表示矩形长度,height表示高度 */ userLabel.setBounds(new java.awt.Rectangle(10, 10, 70, 22)); userLabel.setText("账号:"); this.setLayout(null); //添加Label(标签)和TextField(文本框) this.add(userLabel, null); this.add(getUserText(), null); this.add(getPasswordLabel(), null); this.add(getPasswordText(), null); this.add(getBtnLogin(), null); this.add(getContentLabel(), null); this.add(getContentPaneText(), null); } private JTextField getUserText() { if (userText == null) { userText = new JTextField(); userText.setBounds(new java.awt.Rectangle(80, 10, 100, 22)); } return userText; } private JLabel getPasswordLabel() { if (passwordLabel == null) { passwordLabel = new JLabel(); passwordLabel.setText("密码:"); passwordLabel.setBounds(new java.awt.Rectangle(210, 10, 70, 22)); } return passwordLabel; } private JTextField getPasswordText() { if (passwordText == null) { passwordText = new JTextField(); passwordText.setBounds(new java.awt.Rectangle(280, 10, 100, 22)); } return passwordText; } private JLabel getContentLabel() { if (contentLabel == null) { contentLabel = new JLabel(); contentLabel.setText("内容如下:"); contentLabel.setBounds(new java.awt.Rectangle(10, 70, 70, 22)); } return contentLabel; } private JTextPane getContentPaneText() { if (contentPane == null) { contentPane = new JTextPane(); contentPane.setEditable(false); contentPane.setBounds(new java.awt.Rectangle(10, 90, 370, 150)); } return contentPane; } private JButton getBtnLogin() { if (btnLogin == null) { btnLogin = new JButton("登录"); btnLogin.setBounds(new java.awt.Rectangle(150, 45, 100, 22)); btnLogin.addActionListener(this); //注册监听事件 } return btnLogin; } public void actionPerformed(ActionEvent e) { //在这里添加按下按钮后的触发事件 } }
代码运行结果如下:
时间: 2024-11-19 02:31:36