Anonymous user
Simple windowed application: Difference between revisions
m
→{{header|Java}}: Fixed some small issues with the code
m (→{{header|Java}}: Fixed some small issues with the code) |
|||
Line 1,169:
{{libheader|Swing}}
<lang java>import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
Line 1,174 ⟶ 1,175:
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public class Clicks extends JFrame
private long clicks = 0;
public Clicks(){
super("Clicks");//set window title
JLabel label = new JLabel(
JButton clicker = new JButton("click me");
clicker.addActionListener(
new ActionListener(){
@Override
}▼
}▼
);
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
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){
SwingUtilities.invokeLater( //Swing UI updates should not happen on the main thread
() -> new Clicks()
);
▲ public void actionPerformed(ActionEvent arg0) {
▲ text = "There have been " + (++clicks) + " clicks";
▲ }
}
}</lang>
|