日期:2014-05-20 浏览次数:20607 次
import java.awt.BorderLayout; import java.awt.Container; import java.awt.FlowLayout; 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.JPanel; public class Test extends JFrame{ JPanel panel_1, panel_2; JButton button1, button2; JLabel label; String a = "in button_1_1_1"; String b = "in button_2_2_2"; public Test(){ setTitle("test"); setBounds(300, 100, 500, 500); Container c = getContentPane(); c.setLayout(new BorderLayout()); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); button1 = new JButton("button1"); button2 = new JButton("button2"); label = new JLabel(); panel_1 = new JPanel(); panel_1.setLayout(new FlowLayout(FlowLayout.CENTER, 100, 10)); //设置panel_1的网格 panel_2 = new JPanel(); panel_1.add(button1); panel_1.add(button2); panel_2.add(label); c.add(panel_1, BorderLayout.NORTH); c.add(panel_2, BorderLayout.CENTER); //------------------------------------- button1.addActionListener(new ActionListener(){ //加载button1按钮的事件 public void actionPerformed(ActionEvent e){ label.setText(a); } }); button2.addActionListener(new ActionListener(){ //加载button2按钮的事件 public void actionPerformed(ActionEvent e){ label.setText(b); } }); } public static void main(String []args){ Test frame = new Test(); frame.setVisible(true); } }