June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -1,35 +1,38 @@
|
|||
import java.awt.BorderLayout;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
public class Clicks extends JFrame implements ActionListener{
|
||||
import javax.swing.SwingUtilities;
|
||||
public class Clicks extends JFrame{
|
||||
private long clicks = 0;
|
||||
private JLabel label;
|
||||
private JButton clicker;
|
||||
private String text;
|
||||
|
||||
public Clicks(){
|
||||
text = "There have been no clicks yet";
|
||||
label = new JLabel(text);
|
||||
clicker = new JButton("click me");
|
||||
clicker.addActionListener(this);//listen to the button
|
||||
super("Clicks");//set window title
|
||||
JLabel label = new JLabel("There have been no clicks yet");
|
||||
JButton clicker = new JButton("click me");
|
||||
clicker.addActionListener(//listen to the button
|
||||
new ActionListener(){
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
label.setText("There have been " + (++clicks) + " clicks");//change the text
|
||||
}
|
||||
}
|
||||
);
|
||||
setLayout(new BorderLayout());//handles placement of components
|
||||
add(label,BorderLayout.CENTER);//add the label to the biggest section
|
||||
add(clicker,BorderLayout.SOUTH);//put the button underneath it
|
||||
setSize(300,200);//stretch out the window
|
||||
label.setPreferredSize(new Dimension(300,100));//nice big label
|
||||
label.setHorizontalAlignment(JLabel.CENTER);//text not up against the side
|
||||
pack();//fix layout
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//stop the program on "X"
|
||||
setVisible(true);//show it
|
||||
}
|
||||
public static void main(String[] args){
|
||||
new Clicks();//call the constructor where all the magic happens
|
||||
}
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
if(arg0.getSource() == clicker){//if they clicked the button
|
||||
text = "There have been " + (++clicks) + " clicks";
|
||||
label.setText(text);//change the text
|
||||
}
|
||||
|
||||
SwingUtilities.invokeLater( //Swing UI updates should not happen on the main thread
|
||||
() -> new Clicks() //call the constructor where all the magic happens
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue