1.放一個button在frame上
import javax. swing.*;
public static void main (String[ ] args) {
JFrame frame = new JFrame( );
JButton button = new JButton("clicked me");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Window關閉時程式結束
frame.getContentPane( ).add(button);
frame.setSize(300,300);
frame.setVisible(true); //顯示frame
}
2.按下按鈕後觸發事件
import javax. swing.*;
import java.awt.event.*;
public class GuiTest implements ActionListener {
JButton button;
public static void main (String[ ] args) {
GuiTest mygui = new GuiTest( );
mygui.go( );
}
public void go( ) {
JFrame frame = new JFrame( );
JButton button = new JButton("clicked me");
button.addActionListener(this); //向按鈕登記
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Window關閉時程式結束
frame.getContentPane( ).add(button);
frame.setSize(300,300);
frame.setVisible(true); //顯示frame
}
public void actionPerformed(ActionEvent event) {
button.setText("I have been clicked");
}
}
3.自創繪圖組件
import java.awt.*;
import javax.swing.*;
class MyPanel extends JPanel {
public void paintComponent(Graphics g) {
g.setColor(Color.orange); //Color(red,green,blue)
g.fillRect(20,50,100,100);
}
- Jan 24 Fri 2014 10:18
gui初步使用
close
全站熱搜
留言列表