📄 firstgui.java
字号:
import java.awt.*; // graphical classes
import java.awt.event.*; // event classes
/** A class that contains two buttons and a text field. When the buttons
on the screen are clicked, the value in the text field changes accordingly. */
public class FirstGui extends Frame implements ActionListener
{
Button countBtn, resetBtn;
int counter = 0;
TextField counterFld;
public FirstGui()
{
super("First GUI"); // add title to window
setLayout(new FlowLayout(FlowLayout.CENTER)); // how items will be laid out
countBtn = new Button("Click Me!");
resetBtn = new Button("Reset");
counterFld = new TextField("0", 4);
/* Indicate who will handle events. */
countBtn.addActionListener(this);
resetBtn.addActionListener(this);
addWindowListener(new WindowAdapter() // inner anonymous class
{
/* Called when the user closes the window. */
public void windowClosing(WindowEvent we)
{
setVisible(false); // hide the window
System.exit(0); // finish program
}
});
/* Add components to display. */
add(countBtn);
add(counterFld);
add(resetBtn);
setSize(300, 100); // set dimensions of the window
setVisible(true); // make window appear
}
/** This method is what is called when the buttons are clicked. */
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource() == countBtn) // count button clicked
counter++;
else
counter = 0;
counterFld.setText(counter + ""); // update text field
}
/** Starting point, just call the constructor. */
public static void main(String[] args)
{
new FirstGui();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -