import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class From extends JFrame { private JPanel jPanel; private JButton button1; From() { init(); } public static void main(String[] args) { JFrame frame = new JFrame("From"); frame.setContentPane(new From().jPanel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setSize(600, 500); frame.setLocationRelativeTo(null); frame.setVisible(true); } private void init() { ButtonClickListener buttonClickListener =new ButtonClickListener(); button1 = new JButton("连接"); jPanel.add(button1); button1.addActionListener(buttonClickListener); } } class ButtonClickListener implements ActionListener { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, "连接成功", "提示", JOptionPane.INFORMATION_MESSAGE); } }