1.创建界面窗口:
frame = new JFrame("开卡控制面板"); frame.getContentPane().setForeground(Color.WHITE); frame.setBounds(100, 100, 450, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(null);
2.界面添加操作项:
JLabel lblNewLabel = new JLabel("开卡业务操作控制面板"); lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 18)); lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER); lblNewLabel.setBounds(10, 22, 414, 35); frame.getContentPane().add(lblNewLabel); JLabel lblNewLabel_1 = new JLabel("单卡操作:"); lblNewLabel_1.setFont(new Font("微软雅黑", Font.PLAIN, 14)); lblNewLabel_1.setBounds(46, 67, 74, 15); frame.getContentPane().add(lblNewLabel_1); JButton btnNewButton = new JButton("进入单卡操作界面"); btnNewButton.setBackground(Color.CYAN); btnNewButton.setBounds(116, 64, 155, 23); frame.getContentPane().add(btnNewButton); addActionListenerButton1(btnNewButton, frame); JLabel lblNewLabel_2 = new JLabel("多卡操作:"); lblNewLabel_2.setFont(new Font("微软雅黑", Font.PLAIN, 14)); lblNewLabel_2.setBounds(46, 116, 74, 15); frame.getContentPane().add(lblNewLabel_2); JButton btnNewButton_1 = new JButton("进入多卡操作界面"); btnNewButton_1.setBackground(Color.CYAN); btnNewButton_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { } }); btnNewButton_1.setBounds(116, 113, 155, 23); frame.getContentPane().add(btnNewButton_1); addActionListenerButton2(btnNewButton_1);
3.按钮绑定事件
/** * 进入单卡操作界面 * @param saveButton */ private static void addActionListenerButton1(JButton saveButton, JFrame frame) { // 为按钮绑定监听器 saveButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { frame.setVisible(false);//关闭当前窗口,打开新的窗口 // 对话框 new SingleCardOperationClass(); //JOptionPane.showMessageDialog(null, "进入单卡开卡操作页面成功!"); } }); } /** * 进入多卡操作界面 * @param saveButton */ private static void addActionListenerButton2(JButton saveButton) { // 为按钮绑定监听器 saveButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // 对话框 JOptionPane.showMessageDialog(null, "进入多卡开卡操作页面成功!"); } }); }
4.实现效果图
5.全部代码
import java.awt.Color; import java.awt.EventQueue; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.SwingConstants; public class ControlPanelSwing { public JFrame frame; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { ControlPanelSwing window = new ControlPanelSwing(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the application. */ public ControlPanelSwing() { initialize(); } /** * Initialize the contents of the frame. */ private void initialize() { frame = new JFrame("开卡控制面板"); frame.getContentPane().setForeground(Color.WHITE); frame.setBounds(100, 100, 450, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(null); JLabel lblNewLabel = new JLabel("开卡业务操作控制面板"); lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 18)); lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER); lblNewLabel.setBounds(10, 22, 414, 35); frame.getContentPane().add(lblNewLabel); JLabel lblNewLabel_1 = new JLabel("单卡操作:"); lblNewLabel_1.setFont(new Font("微软雅黑", Font.PLAIN, 14)); lblNewLabel_1.setBounds(46, 67, 74, 15); frame.getContentPane().add(lblNewLabel_1); JButton btnNewButton = new JButton("进入单卡操作界面"); btnNewButton.setBackground(Color.CYAN); btnNewButton.setBounds(116, 64, 155, 23); frame.getContentPane().add(btnNewButton); addActionListenerButton1(btnNewButton, frame); JLabel lblNewLabel_2 = new JLabel("多卡操作:"); lblNewLabel_2.setFont(new Font("微软雅黑", Font.PLAIN, 14)); lblNewLabel_2.setBounds(46, 116, 74, 15); frame.getContentPane().add(lblNewLabel_2); JButton btnNewButton_1 = new JButton("进入多卡操作界面"); btnNewButton_1.setBackground(Color.CYAN); btnNewButton_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { } }); btnNewButton_1.setBounds(116, 113, 155, 23); frame.getContentPane().add(btnNewButton_1); addActionListenerButton2(btnNewButton_1); } /** * 进入单卡操作界面 * @param saveButton */ private static void addActionListenerButton1(JButton saveButton, JFrame frame) { // 为按钮绑定监听器 saveButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { frame.setVisible(false); // 对话框 new SingleCardOperationClass(); //JOptionPane.showMessageDialog(null, "进入单卡开卡操作页面成功!"); } }); } /** * 进入多卡操作界面 * @param saveButton */ private static void addActionListenerButton2(JButton saveButton) { // 为按钮绑定监听器 saveButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // 对话框 JOptionPane.showMessageDialog(null, "进入多卡开卡操作页面成功!"); } }); } }