Sync
This commit is contained in:
parent
6f050a029e
commit
776bba907c
3887 changed files with 59894 additions and 7280 deletions
|
|
@ -0,0 +1,93 @@
|
|||
import java.awt.GridLayout;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JOptionPane;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JTextField;
|
||||
|
||||
public class Interact extends JFrame{
|
||||
final JTextField numberField;
|
||||
final JButton incButton, randButton;
|
||||
|
||||
public Interact(){
|
||||
//stop the GUI threads when the user hits the X button
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
|
||||
numberField = new JTextField();
|
||||
incButton = new JButton("Increment");
|
||||
randButton = new JButton("Random");
|
||||
|
||||
numberField.setText("0");//start at 0
|
||||
|
||||
//listen for button presses in the text field
|
||||
numberField.addKeyListener(new KeyListener(){
|
||||
@Override
|
||||
public void keyTyped(KeyEvent e) {
|
||||
//if the entered character is not a digit
|
||||
if(!Character.isDigit(e.getKeyChar())){
|
||||
//eat the event (i.e. stop it from being processed)
|
||||
e.consume();
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void keyReleased(KeyEvent e){}
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e){}
|
||||
});
|
||||
|
||||
//listen for button clicks on the increment button
|
||||
incButton.addActionListener(new ActionListener(){
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
String text = numberField.getText();
|
||||
if(text.isEmpty()){
|
||||
numberField.setText("1");
|
||||
}else{
|
||||
numberField.setText((Long.valueOf(text) + 1) + "");
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
//listen for button clicks on the random button
|
||||
randButton.addActionListener(new ActionListener(){
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
//show a dialog and if they answer "Yes"
|
||||
if(JOptionPane.showConfirmDialog(null, "Are you sure?") ==
|
||||
JOptionPane.YES_OPTION){
|
||||
//set the text field text to a random positive long
|
||||
numberField.setText(Long.toString((long)(Math.random()
|
||||
* Long.MAX_VALUE)));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
//arrange the components in a grid with 2 rows and 1 column
|
||||
setLayout(new GridLayout(2, 1));
|
||||
|
||||
//a secondary panel for arranging both buttons in one grid space in the window
|
||||
JPanel buttonPanel = new JPanel();
|
||||
|
||||
//the buttons are in a grid with 1 row and 2 columns
|
||||
buttonPanel.setLayout(new GridLayout(1, 2));
|
||||
//add the buttons
|
||||
buttonPanel.add(incButton);
|
||||
buttonPanel.add(randButton);
|
||||
|
||||
//put the number field on top of the buttons
|
||||
add(numberField);
|
||||
add(buttonPanel);
|
||||
//size the window appropriately
|
||||
pack();
|
||||
|
||||
}
|
||||
|
||||
public static void main(String[] args){
|
||||
new Interact().setVisible(true);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
|
||||
public interface FunctionalKeyListener extends KeyListener {
|
||||
@Override
|
||||
public default void keyPressed(KeyEvent event) {}
|
||||
@Override
|
||||
public default void keyTyped(KeyEvent event) {}
|
||||
@Override
|
||||
public default void keyReleased(KeyEvent event) {}
|
||||
|
||||
@FunctionalInterface
|
||||
public static interface Pressed extends FunctionalKeyListener {
|
||||
@Override
|
||||
public void keyPressed(KeyEvent event);
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
public static interface Typed extends FunctionalKeyListener {
|
||||
@Override
|
||||
public void keyTyped(KeyEvent event);
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
public static interface Released extends FunctionalKeyListener {
|
||||
@Override
|
||||
public void keyReleased(KeyEvent event);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,191 @@
|
|||
import java.awt.GridLayout;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Stream;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JOptionPane;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JTextField;
|
||||
|
||||
public interface Interact {
|
||||
public static final JFrame FRAME = new JFrame();
|
||||
public static final JTextField FIELD = new JTextField();
|
||||
public static final JPanel PANEL = new JPanel();
|
||||
|
||||
public static void setDefaultCloseOperation(JFrame frame) {
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
}
|
||||
|
||||
public static void setText(JTextField field) {
|
||||
field.setText("0");
|
||||
}
|
||||
|
||||
public static void setEditable(JTextField field) {
|
||||
field.setEditable(false);
|
||||
}
|
||||
|
||||
public static boolean isDigitKeyChar(KeyEvent event) {
|
||||
return !Character.isDigit(event.getKeyChar());
|
||||
}
|
||||
|
||||
public static void keyTyped(KeyEvent event) {
|
||||
Stream.of(event)
|
||||
.parallel()
|
||||
.filter(Interact::isDigitKeyChar)
|
||||
.forEach(KeyEvent::consume)
|
||||
;
|
||||
}
|
||||
|
||||
public static void addKeyListener(JTextField field) {
|
||||
field.addKeyListener((FunctionalKeyListener.Typed) Interact::keyTyped);
|
||||
}
|
||||
|
||||
public static String mapText(String text) {
|
||||
return text.isEmpty()
|
||||
? "1"
|
||||
: String.valueOf(Long.valueOf(text) + 1)
|
||||
;
|
||||
}
|
||||
|
||||
public static void actionPerformedOnIncrementButton(ActionEvent event) {
|
||||
Stream.of(FIELD)
|
||||
.parallel()
|
||||
.map(JTextField::getText)
|
||||
.map(Interact::mapText)
|
||||
.forEach(FIELD::setText)
|
||||
;
|
||||
}
|
||||
|
||||
public static void addActionListenerToIncrementButton(JButton button) {
|
||||
button.addActionListener(Interact::actionPerformedOnIncrementButton);
|
||||
}
|
||||
|
||||
public static void addIncrementButton(JPanel panel) {
|
||||
Stream.of("Increment")
|
||||
.parallel()
|
||||
.map(JButton::new)
|
||||
.peek(Interact::addActionListenerToIncrementButton)
|
||||
.forEach(panel::add)
|
||||
;
|
||||
}
|
||||
|
||||
public static int showConfirmDialog(String question) {
|
||||
return JOptionPane.showConfirmDialog(null, question);
|
||||
}
|
||||
|
||||
public static void setFieldText(int integer) {
|
||||
FIELD.setText(
|
||||
String.valueOf(
|
||||
(long) (Math.random() * Long.MAX_VALUE))
|
||||
)
|
||||
;
|
||||
}
|
||||
|
||||
public static void actionPerformedOnRandomButton(ActionEvent event) {
|
||||
Stream.of("Are you sure?")
|
||||
.parallel()
|
||||
.map(Interact::showConfirmDialog)
|
||||
.filter(Predicate.isEqual(JOptionPane.YES_OPTION))
|
||||
.forEach(Interact::setFieldText)
|
||||
;
|
||||
}
|
||||
|
||||
public static void addActionListenerToRandomButton(JButton button) {
|
||||
button.addActionListener(Interact::actionPerformedOnRandomButton);
|
||||
}
|
||||
|
||||
public static void addRandomButton(JPanel panel) {
|
||||
Stream.of("Random")
|
||||
.parallel()
|
||||
.map(JButton::new)
|
||||
.peek(Interact::addActionListenerToRandomButton)
|
||||
.forEach(panel::add)
|
||||
;
|
||||
}
|
||||
|
||||
public static void acceptField(Consumer<JTextField> consumer) {
|
||||
consumer.accept(FIELD);
|
||||
}
|
||||
|
||||
public static void prepareField(JTextField field) {
|
||||
Stream.<Consumer<JTextField>>of(
|
||||
Interact::setEditable,
|
||||
Interact::setText,
|
||||
Interact::addKeyListener
|
||||
)
|
||||
.parallel()
|
||||
.forEach(Interact::acceptField)
|
||||
;
|
||||
}
|
||||
|
||||
public static void addField(JFrame frame) {
|
||||
Stream.of(FIELD)
|
||||
.parallel()
|
||||
.peek(Interact::prepareField)
|
||||
.forEach(frame::add)
|
||||
;
|
||||
}
|
||||
|
||||
public static void acceptPanel(Consumer<JPanel> consumer) {
|
||||
consumer.accept(PANEL);
|
||||
}
|
||||
|
||||
public static void processPanel(JPanel panel) {
|
||||
Stream.<Consumer<JPanel>>of(
|
||||
Interact::setLayout,
|
||||
Interact::addIncrementButton,
|
||||
Interact::addRandomButton
|
||||
)
|
||||
.parallel()
|
||||
.forEach(Interact::acceptPanel)
|
||||
;
|
||||
}
|
||||
|
||||
public static void addPanel(JFrame frame) {
|
||||
Stream.of(PANEL)
|
||||
.parallel()
|
||||
.peek(Interact::processPanel)
|
||||
.forEach(frame::add)
|
||||
;
|
||||
}
|
||||
|
||||
public static void setLayout(JFrame frame) {
|
||||
frame.setLayout(new GridLayout(2, 1));
|
||||
}
|
||||
|
||||
public static void setLayout(JPanel panel) {
|
||||
panel.setLayout(new GridLayout(1, 2));
|
||||
}
|
||||
|
||||
public static void setVisible(JFrame frame) {
|
||||
frame.setVisible(true);
|
||||
}
|
||||
|
||||
public static void acceptFrame(Consumer<JFrame> consumer) {
|
||||
consumer.accept(FRAME);
|
||||
}
|
||||
|
||||
public static void processField(JFrame frame) {
|
||||
Stream.<Consumer<JFrame>>of(
|
||||
Interact::setDefaultCloseOperation,
|
||||
Interact::setLayout,
|
||||
Interact::addField,
|
||||
Interact::addPanel,
|
||||
Interact::setVisible
|
||||
)
|
||||
.parallel()
|
||||
.forEach(Interact::acceptFrame)
|
||||
;
|
||||
}
|
||||
|
||||
public static void main(String... arguments) {
|
||||
Stream.of(FRAME)
|
||||
.parallel()
|
||||
.peek(Interact::processField)
|
||||
.forEach(JFrame::pack)
|
||||
;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,68 @@
|
|||
import
|
||||
gtk2, gdk2, glib2, strutils, math
|
||||
|
||||
var valu: int = 0
|
||||
var chngd_txt_hndler: gulong = 0
|
||||
|
||||
proc thisDestroy(widget: pWidget, data: pgpointer) {.cdecl.} =
|
||||
main_quit()
|
||||
|
||||
randomize()
|
||||
nimrod_init()
|
||||
var win = window_new(gtk2.WINDOW_TOPLEVEL)
|
||||
var content = vbox_new(TRUE,10)
|
||||
var hbox1 = hbox_new(TRUE,10)
|
||||
var hbox2 = hbox_new(FALSE,1)
|
||||
var lbl = label_new("Value:")
|
||||
var entry_fld = entry_new()
|
||||
entry_fld.set_text("0")
|
||||
var btn_quit = button_new("Quit")
|
||||
var btn_inc = button_new("Increment")
|
||||
var btn_rnd = button_new("Random")
|
||||
add(hbox2,lbl)
|
||||
add(hbox2,entry_fld)
|
||||
add(hbox1,btn_inc)
|
||||
add(hbox1,btn_rnd)
|
||||
pack_start(content, hbox2, TRUE, TRUE, 0)
|
||||
pack_start(content, hbox1, TRUE, TRUE, 0)
|
||||
pack_start(content, btn_quit, TRUE, TRUE, 0)
|
||||
set_border_width(Win, 5)
|
||||
add(win, content)
|
||||
|
||||
proc on_question_clicked: Bool =
|
||||
var dialog = win.message_dialog_new(0, MESSAGE_QUESTION,
|
||||
BUTTONS_YES_NO, "Use a Random number?")
|
||||
var response = dialog.run()
|
||||
if response == RESPONSE_YES:
|
||||
result = True
|
||||
elif response == RESPONSE_NO:
|
||||
result = False
|
||||
dialog.destroy()
|
||||
|
||||
proc thisInc(widget: pWidget, data: pgpointer){.cdecl.} =
|
||||
inc(valu)
|
||||
entry_fld.set_text($valu)
|
||||
|
||||
proc thisRnd(widget: pWidget, data: pgpointer){.cdecl.} =
|
||||
if on_question_clicked():
|
||||
valu = random(20)
|
||||
entry_fld.set_text($valu)
|
||||
|
||||
proc thisTextChanged(widget: pWidget, data: pgpointer) {.cdecl.} =
|
||||
#signal_handler_block(entry_fld, chngd_txt_hndler)
|
||||
try:
|
||||
valu = parseInt($entry_fld.get_text())
|
||||
except EInvalidValue:
|
||||
valu = 0
|
||||
entry_fld.set_text($valu)
|
||||
#signal_handler_unblock(entry_fld, chngd_txt_hndler)
|
||||
#signal_emit_stop(entry_fld, signal_lookup("changed",TYPE_EDITABLE()),0)
|
||||
|
||||
discard signal_connect(win, "destroy", SIGNAL_FUNC(thisDestroy), nil)
|
||||
discard signal_connect(btn_quit, "clicked", SIGNAL_FUNC(thisDestroy), nil)
|
||||
discard signal_connect(btn_inc, "clicked", SIGNAL_FUNC(thisInc), nil)
|
||||
discard signal_connect(btn_rnd, "clicked", SIGNAL_FUNC(thisRnd), nil)
|
||||
chngd_txt_hndler = signal_connect(entry_fld, "changed", SIGNAL_FUNC(thisTextChanged), nil)
|
||||
|
||||
win.show_all()
|
||||
main()
|
||||
|
|
@ -6,9 +6,12 @@ object Interact extends SimpleSwingApplication {
|
|||
def top = new MainFrame {
|
||||
title = "Rosetta Code >>> Task: component interaction | Language: Scala"
|
||||
|
||||
val numberField = new TextField { text = "0" } // start at 0
|
||||
val numberField = new TextField {
|
||||
text = "0" // start at 0
|
||||
horizontalAlignment = Alignment.Right
|
||||
}
|
||||
|
||||
val incButton = new Button { text = "Increment" }
|
||||
val incButton = new Button { text = "Increment" }
|
||||
val randButton = new Button { text = "Random" }
|
||||
|
||||
// arrange buttons in a grid with 1 row, 2 columns
|
||||
|
|
@ -17,30 +20,26 @@ object Interact extends SimpleSwingApplication {
|
|||
}
|
||||
|
||||
// arrange text field and button panel in a grid with 2 row, 1 column
|
||||
contents = new GridPanel(2, 1) {
|
||||
contents ++= List(numberField, buttonPanel)
|
||||
}
|
||||
contents = new GridPanel(2, 1) { contents ++= List(numberField, buttonPanel) }
|
||||
|
||||
// listen for keys pressed in numberField and button clicks
|
||||
listenTo(numberField.keys, incButton, randButton)
|
||||
reactions += {
|
||||
case kt: KeyTyped if (!kt.char.isDigit) => // if the entered char isn't a digit ...
|
||||
kt.consume // ... eat the event (i.e. stop it from being processed)
|
||||
case kt: KeyTyped if (!kt.char.isDigit) => // if the entered char isn't a digit …
|
||||
kt.consume // … eat the event (i.e. stop it from being processed)
|
||||
case ButtonClicked(`incButton`) =>
|
||||
if (numberField.text.isEmpty()) numberField.text = "0"
|
||||
if (numberField.text.isEmpty) numberField.text = "0"
|
||||
// we use BigInt to avoid long overflow/number format exception
|
||||
val biNumber = new BigInt(new java.math.BigInteger(numberField.text))
|
||||
numberField.text = "" + (biNumber+1)
|
||||
numberField.text = (BigInt(numberField.text) + 1).toString
|
||||
case ButtonClicked(`randButton`) =>
|
||||
import Dialog._
|
||||
numberField.text = showOptions(buttonPanel,
|
||||
message = "Are you sure?",
|
||||
title = "Choose an option!",
|
||||
entries = List("Yes", "No", "Cancel"),
|
||||
initial = 2) match {
|
||||
case Result.Yes => (Long.MaxValue * Math.random).toLong.toString
|
||||
case _ => numberField.text
|
||||
}
|
||||
numberField.text = showOptions(buttonPanel, message = "Are you sure?",
|
||||
title = "Choose an option!", entries = List("Yes", "No", "Cancel"),
|
||||
initial = 2) match {
|
||||
case Result.Yes => (Long.MaxValue * Math.random).toLong.toString
|
||||
case _ => numberField.text
|
||||
}
|
||||
}
|
||||
centerOnScreen()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue