G
Gravity
Gast
also ich habe ein Textfield erstellt, und es im ActionListener für einen Button verwendet.
Jedoch kann ich es im Button nur dann verwenden wenn das Textfield final ist. das will ich aber nicht, da ich es noch in anderen klassen verwenden muss und es static sein sollte.
hier der code:
Also ich will dass TExtfield NICHT final sein muss damit das programm funktioniert.
mfg Gravity =)
Jedoch kann ich es im Button nur dann verwenden wenn das Textfield final ist. das will ich aber nicht, da ich es noch in anderen klassen verwenden muss und es static sein sollte.
hier der code:
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Grafik extends JFrame{
Grafik(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JPanel panel5 = new JPanel();
JLabel label1 = new JLabel();
JLabel label2 = new JLabel();
JLabel label3 = new JLabel();
JLabel label4 = new JLabel();
JButton eins = new JButton("1");
JButton zwei = new JButton("2");
JButton drei = new JButton("3");
JButton vier = new JButton("4");
JButton fünf = new JButton("5");
JButton sechs = new JButton("6");
JButton sieben = new JButton("7");
JButton acht = new JButton("8");
JButton neun = new JButton("9");
JButton zero = new JButton("0");
JButton clear = new JButton(" C ");
JButton div = new JButton("/");
JButton mult = new JButton("*");
JButton minus = new JButton("-");
JButton plus = new JButton("+");
JButton gleich = new JButton("=");
JButton wurzel = new JButton("w");
JButton quadrat = new JButton("^");
JTextField textfeld = new JTextField("0,", 21);
textfeld.setHorizontalAlignment(JTextField.RIGHT);
getContentPane().setLayout(new GridLayout(5,1));
panel1.setLayout(new FlowLayout());
panel2.setLayout(new FlowLayout(FlowLayout.LEFT));
panel3.setLayout(new FlowLayout(FlowLayout.LEFT));
panel4.setLayout(new FlowLayout(FlowLayout.LEFT));
panel5.setLayout(new FlowLayout(FlowLayout.LEFT));
getContentPane().add(panel1);
getContentPane().add(panel2);
getContentPane().add(panel3);
getContentPane().add(panel4);
getContentPane().add(panel5);
panel1.add(textfeld);
panel2.add(label1);
panel2.add(sieben);
panel2.add(acht);
panel2.add(neun);
panel2.add(div);
panel2.add(wurzel);
panel3.add(label2);
panel3.add(vier);
panel3.add(fünf);
panel3.add(sechs);
panel3.add(mult);
panel3.add(quadrat);
panel4.add(label3);
panel4.add(eins);
panel4.add(zwei);
panel4.add(drei);
panel4.add(minus);
panel5.add(label4);
panel5.add(zero);
panel5.add(clear);
panel5.add(plus);
panel5.add(gleich);
zero.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
textfeld.setText("0");
}
});
eins.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
textfeld.setText("1");
}
});
zwei.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
textfeld.setText("2");
}
});
drei.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
textfeld.setText("3");
}
});
vier.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
textfeld.setText("4");
}
});
fünf.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
textfeld.setText("5");
}
});
sechs.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
textfeld.setText("6");
}
});
sieben.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
textfeld.setText("7");
}
});
acht.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
textfeld.setText("8");
}
});
neun.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
textfeld.setText("9");
}
});
clear.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
textfeld.setText("0,");
}
});
}
public static void main(String args[]){
Grafik fenster = new Grafik();
fenster.setVisible(true);
fenster.pack();
}
}
Also ich will dass TExtfield NICHT final sein muss damit das programm funktioniert.
mfg Gravity =)