2015年6月11日 星期四

作業4

實作
000  
程式碼:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class button
{

public static void main(String[] args)
{
JFrame jtfMainFrame = new JFrame("Which Button Demo");
jtfMainFrame.setSize(450, 150);
JButton jbnButton1 = new JButton("Button 1");
JButton jbnButton2 = new JButton("Button 2");
JTextField jtfInput = new JTextField(20);
JPanel jplPanel = new JPanel();

jbnButton1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jtfInput.setText("Button 1!");
}});
jbnButton2.addActionListener(new ActionListener() 
{
public void actionPerformed(ActionEvent e)
{
jtfInput.setText("Button 2!");
}});
jplPanel.setLayout(new FlowLayout());
jplPanel.add(jtfInput);
jplPanel.add(jbnButton1);
jplPanel.add(jbnButton2);
jtfMainFrame.getContentPane().add(jplPanel, BorderLayout.CENTER);
jtfMainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jtfMainFrame.pack();
jtfMainFrame.setVisible(true);
System.out.print("chih-yu hsu");
}
}

沒有留言:

張貼留言