Ahoj, chtěl bych vás poprosit upravit kód, abych vstup z JTextField dostal do JTextArea při kliku na JButton přes proměnou. Nějak se k tomu nemůžu dopátrat.
Pak nad tím chci dělat výpočet, ale k tomu se dopracuju asi až pozdějc :-)
Díky,
Nicolas
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Ko {
public static void main (String []arg) {
okno Okno = new okno();
}
}
class okno extends JFrame implements ActionListener {
JTextField te = new JTextField("", 25);
JButton tl = new JButton("Start");
JTextArea to = new JTextArea("", 5, 30);
public okno () {
super ("Nadpis okna");
setSize (400,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
Container kon = getContentPane ();
kon.setBackground(Color.green);
FlowLayout srf = new FlowLayout();
kon.setLayout(srf);
tl.addActionListener(this);
kon.add(tl);
kon.add(te);
kon.add(to);
setContentPane(kon);
}
public void actionPerformed (ActionEvent event) {
to.setText(te.getText());
}
}
Fórum › Java
JTextField -> proměnná -> JTextArea

Ještě bych chtěl poprosit o další pomoc.
Výpočet už mám - což je super. Ale ještě bych chtěl, aby se pro JButton tl1 a tl2 přiřadil jin výpočet.
Pro tl1 to funguje a je to i + i
Pro tl2 bych chtěl třebas i - i
Pomůžete mně upravit kód? Nějak se mi nedaří to z knížek nastudovat...
Jako cíl jsem si stanovil vytvořit jednoduchou kalkulačku - jsem samouk.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Ko {
public static void main (String []arg) {
okno Okno = new okno();
}
}
class okno extends JFrame implements ActionListener {
JTextField te = new JTextField("", 25);
JButton tl1 = new JButton("+");
JButton tl2 = new JButton("-");
JTextArea to = new JTextArea("", 5, 30);
public okno () {
super ("Nadpis okna");
setSize (400,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
Container kon = getContentPane ();
kon.setBackground(Color.green);
FlowLayout srf = new FlowLayout();
kon.setLayout(srf);
kon.add(tl1);
kon.add(tl2);
kon.add(te);
kon.add(to);
setContentPane(kon);
tl1.addActionListener(this);
}
public void actionPerformed (ActionEvent e) {
String vstup = te.getText();
int i = Integer.parseInt(vstup);
int j = i + i;
String vystup = Integer.toString(j);
to.setText(vystup);
}
}
To Anonymní uživatel : pro kazde talcitko udelej svojeho listenera nebo jednoho a tam poznej ktere to vyvolalo
To Nicolas : tam je snad u tlacitka addactionlisteners a bude udelej vnitrni anonymni tridu nebo nejakou jinaci tridu ktera implementuje actionlisteners

Přidej příspěvek
Ano, opravdu chci reagovat → zobrazí formulář pro přidání příspěvku
×Vložení zdrojáku
×Vložení obrázku
×Vložení videa
































Uživatelé prohlížející si toto vlákno
Podobná vlákna
JTextArea se scrollbary — založil Dan
JScrollPane v JTextArea — založil Nicolas
Tisk textu z jTextArea — založil Roumen
PHP proměnná do proměnná Javascript — založil Marek
JTextField - pozicovani — založil polerok
Moderátoři diskuze