Was mache ich verkehrt? ich bekomme nur "null" Werte aus dem Dialog zurück!!
MyFrame wird ducrch eine Klasse mit main Funktion aufgerufen.
Klassen:
MyFrame:
mport java.awt.Color;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class MyFrame extends JFrame implements ActionListener{
public JLabel l_result;
public JButton b_start;
public MyPoint point;
public MyFrame() throws HeadlessException {
this.setBounds(100, 100, 300, 300);
this.setLayout(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
point = new MyPoint();
l_result = new JLabel("noch leer");
l_result.setBounds(20, 20, 260, 150);
l_result.setBackground(Color.white);
b_start = new JButton("Start");
b_start.setBounds(20, 200, 260, 25);
b_start.addActionListener(this);
b_start.setActionCommand("Start");
this.add(l_result);
this.add(b_start);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Start")){
MyDialog md = new MyDialog();
md.setVisible(true);
l_result.setText(md.point.toText());
}
}
}
Klasse: MyDialog
mport java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JTextField;
public class MyDialog extends JDialog implements ActionListener{
public JTextField tf_a, tf_b;
public JButton b_transfer;
public MyPoint point;
public MyDialog() {
this.setLayout(null);
this.setBounds(100, 100, 200, 200);
point = new MyPoint();
tf_a = new JTextField("a");
tf_a.setBounds(20, 20, 40, 20);
tf_b = new JTextField("b");
tf_b.setBounds(20, 60, 40, 20);
b_transfer = new JButton("Transfer");
b_transfer.setBounds(20, 100, 80, 25);
b_transfer.addActionListener(this);
b_transfer.setActionCommand("trans");
this.add(tf_a);
this.add(tf_b);
this.add(b_transfer);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equalsIgnoreCase("trans")){
point.setA(tf_a.getText());
point.setB(tf_b.getText());
this.dispose();
}
}
}
Klasse: MyPoint(hier zum Testen mit Strings)
public class MyPoint {
public String a,b;
public MyPoint() {
}
public MyPoint(String a, String b){
this.a = a;
this.b = b;
}
public String getA() {
return a;
}
public void setA(String a) {
this.a = a;
}
public String getB() {
return b;
}
public void setB(String b) {
this.b = b;
}
public String toText(){
return ""+a+" : "+b;
}
}
MyFrame wird ducrch eine Klasse mit main Funktion aufgerufen.
Klassen:
MyFrame:
mport java.awt.Color;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class MyFrame extends JFrame implements ActionListener{
public JLabel l_result;
public JButton b_start;
public MyPoint point;
public MyFrame() throws HeadlessException {
this.setBounds(100, 100, 300, 300);
this.setLayout(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
point = new MyPoint();
l_result = new JLabel("noch leer");
l_result.setBounds(20, 20, 260, 150);
l_result.setBackground(Color.white);
b_start = new JButton("Start");
b_start.setBounds(20, 200, 260, 25);
b_start.addActionListener(this);
b_start.setActionCommand("Start");
this.add(l_result);
this.add(b_start);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Start")){
MyDialog md = new MyDialog();
md.setVisible(true);
l_result.setText(md.point.toText());
}
}
}
Klasse: MyDialog
mport java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JTextField;
public class MyDialog extends JDialog implements ActionListener{
public JTextField tf_a, tf_b;
public JButton b_transfer;
public MyPoint point;
public MyDialog() {
this.setLayout(null);
this.setBounds(100, 100, 200, 200);
point = new MyPoint();
tf_a = new JTextField("a");
tf_a.setBounds(20, 20, 40, 20);
tf_b = new JTextField("b");
tf_b.setBounds(20, 60, 40, 20);
b_transfer = new JButton("Transfer");
b_transfer.setBounds(20, 100, 80, 25);
b_transfer.addActionListener(this);
b_transfer.setActionCommand("trans");
this.add(tf_a);
this.add(tf_b);
this.add(b_transfer);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equalsIgnoreCase("trans")){
point.setA(tf_a.getText());
point.setB(tf_b.getText());
this.dispose();
}
}
}
Klasse: MyPoint(hier zum Testen mit Strings)
public class MyPoint {
public String a,b;
public MyPoint() {
}
public MyPoint(String a, String b){
this.a = a;
this.b = b;
}
public String getA() {
return a;
}
public void setA(String a) {
this.a = a;
}
public String getB() {
return b;
}
public void setB(String b) {
this.b = b;
}
public String toText(){
return ""+a+" : "+b;
}
}