程式碼如下:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main; | |
import java.lang.Math; | |
import java.awt.EventQueue; | |
import javax.swing.JFrame; | |
import javax.swing.JButton; | |
import java.awt.event.ActionListener; | |
import java.awt.event.ActionEvent; | |
import java.awt.Panel; | |
import java.awt.event.ContainerAdapter; | |
import java.awt.event.ContainerEvent; | |
import javax.swing.JLabel; | |
import javax.swing.JList; | |
import javax.swing.JTextArea; | |
import javax.swing.JPanel; | |
import javax.swing.JToolBar; | |
import java.awt.TextArea; | |
public class windowdemo { | |
private JFrame frame; | |
private int sum = 0; | |
/** | |
* Launch the application. 運行應用程式 | |
*/ | |
public static void main(String[] args) { | |
EventQueue.invokeLater(new Runnable() { | |
public void run() { | |
try { | |
windowdemo window = new windowdemo(); | |
window.frame.setVisible(true); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
}); | |
} | |
/** | |
* Create the application. 創造框架 | |
*/ | |
public windowdemo() { | |
initialize(); | |
} | |
/** | |
* Initialize the contents of the frame. 在框架中安裝元件 | |
*/ | |
private void initialize() { | |
frame = new JFrame("Title"); // 建立一個標題為 Title 的視窗 | |
frame.setBounds(100, 100, 450, 300); // 在螢幕上(100,100)的位置產生(寬450,長300)的視窗 | |
// setBounds(x, y, width, height) | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 設定 | |
frame.getContentPane().setLayout(null); // 設定視窗排版規則 | |
// exit 按鈕 | |
JButton btnNewButton_9 = new JButton("Exit"); | |
btnNewButton_9.addActionListener(new ActionListener() { | |
public void actionPerformed(ActionEvent arg0) { | |
System.exit(0); | |
} | |
}); | |
btnNewButton_9.setBounds(330, 195, 87, 23); | |
frame.getContentPane().add(btnNewButton_9); | |
// dispose 按鈕 | |
JButton btnNewButton_10 = new JButton("Dispose"); | |
btnNewButton_10.addActionListener(new ActionListener() { | |
public void actionPerformed(ActionEvent arg0) { | |
frame.dispose(); | |
} | |
}); | |
btnNewButton_10.setBounds(330, 228, 87, 23); | |
frame.getContentPane().add(btnNewButton_10); | |
//區塊1 | |
JPanel panel_1 = new JPanel(); | |
panel_1.setBounds(0, 0, 180, 33); | |
frame.getContentPane().add(panel_1); | |
// sum 按鈕 | |
JButton btnNewButton_1 = new JButton("Sum"); | |
panel_1.add(btnNewButton_1); | |
JPanel panel = new JPanel(); | |
panel_1.add(panel); | |
JLabel label = new JLabel("點擊次數:0"); | |
panel_1.add(label); | |
JPanel panel_2 = new JPanel(); | |
panel_2.setBounds(0, 34, 149, 33); | |
frame.getContentPane().add(panel_2); | |
// Print 按鈕 | |
JButton btnNewButton = new JButton("Print"); | |
panel_2.add(btnNewButton); | |
JPanel panel_3 = new JPanel(); | |
panel_2.add(panel_3); | |
JLabel lblNewLabel = new JLabel("hahaha"); | |
panel_2.add(lblNewLabel); | |
btnNewButton.addActionListener(new ActionListener() { | |
public void actionPerformed(ActionEvent arg0) { | |
String num = Integer.toString((int)(Math.random()*99+0)); | |
lblNewLabel.setText(num); | |
} | |
}); | |
btnNewButton_1.addActionListener(new ActionListener() { | |
public void actionPerformed(ActionEvent arg0) { | |
sum++; | |
label.setText("點擊次數:"+sum); | |
} | |
}); | |
} | |
} |
沒有留言:
張貼留言