public void keyPressed(KeyEvent e) {
if (e.getKeyCode()==KeyEvent.VK_ENTER) {
if (comp1.isFocusOwner()) comp2.requestFocusInWindow();
else if (comp2.isFocusOwner()) comp1.requestFocusInWindow();
}
}
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode()==KeyEvent.VK_ENTER) {
if (comp1.isFocusOwner()) comp2.requestFocusInWindow();
else if (comp2.isFocusOwner()) comp1.requestFocusInWindow();
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyFrame extends JFrame {
private JTextField tf1, tf2;
private JLabel lab;
public MyFrame(String title) {
super(title);
setSize(250, 100);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
lab = new JLabel("Enter drücken für Focuswechsel");
lab.setHorizontalAlignment(SwingConstants.CENTER);
add(lab, BorderLayout.CENTER);
tf1 = new JTextField();
tf1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tf2.requestFocus();
}
});
add(tf1, BorderLayout.NORTH);
tf2 = new JTextField();
tf2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tf1.requestFocus();
}
});
add(tf2, BorderLayout.SOUTH);
setVisible(true);
}
public static void main(String[] args) {
new MyFrame("TextFocus");
}
}
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER)
{
day.setText("test");
}
}});
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyFrame extends JFrame {
private JTextField tf1, tf2;
private JLabel lab;
public MyFrame(String title) {
super(title);
setSize(250, 100);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
lab = new JLabel("Enter drücken für Focuswechsel");
lab.setHorizontalAlignment(SwingConstants.CENTER);
add(lab, BorderLayout.CENTER);
tf1 = new JTextField();
tf1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tf2.requestFocus();
lab.setText("Focus: tf2");
}
});
add(tf1, BorderLayout.NORTH);
tf2 = new JTextField();
tf2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tf1.requestFocus();
lab.setText("Focus: tf1");
}
});
add(tf2, BorderLayout.SOUTH);
setVisible(true);
}
public static void main(String[] args) {
new MyFrame("TextFocus");
}
}
class TextFrame extends JFrame {
private JLabel label;
public TextFrame(){
day = new JTextField("");
day.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
month.requestFocus();
}
});
add(day, BorderLayout.NORTH);
month = new JTextField("");
month.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
day.requestFocus();
label.setText("test");
}
});
add(month, BorderLayout.SOUTH);
class NAVIpanel extends JPanel
{
public NAVIpanel()
{
JLabel label = new JLabel("")
add(label);
}
}
class NAVIpanel extends JPanel {
private labi lab;
private String name;
lab = new labi(this);
public String getName() {
return name;
}
}
class labi extends JLabel{
private NAVIpanel panel3;
public labi(NAVIpanel panel3){
this.panel3 = panel3;
}
public String getHerstellerName() {
panel3.getName();
}
}
//Methode in NAVIPanel
public void setTextOnMyLabel(String text) {
myLabel.setText(text);
}
NAVIPanel naviPanel;
[...]
//irgendwo in TextFrame
naviPanel.setTextOnMyLabel("So einfach geht das!");
public class KSAVE {
public static void main(String[] args) {
TextFrame frame1 = new TextFrame();
frame1.setVisible(true);
TestFrame frame3 = new TestFrame();
frame3.setVisible(true);
}
}
public class KSAVE {
public static void main(String[] args) {
AndererFrame andererFrame = new AndererFrame();
andererFrame.setVisible(true);
TextFrame textFrame = new TextFrame(andererFrame);
textFrame.setVisible(true);
}
}
public class TextFrame extends JFrame {
private AndererFrame frame;
[...]
public TextFrame(AndererFrame frame) {
this.frame = frame;
[...]
}
private void setTextOnLabel(String text) {
frame.setTextOnLabel(text);
}
[...]
}
public class AndererFrame extends JFrame {
[...]
public void setTextOnLabel(String text) {
label.setText(text); // JLabel auf dem NAVIPanel
}
[...]
}
public class TextFrame extends JFrame {
private AndererFrame frame;
[...]
public TextFrame(AndererFrame andererFrame) {
frame = andererFrame; //es ist quasi nur eine Zuweisung einer Speicheradresse der VM
[...]
}
private void setTextOnLabel(String text) {
frame.setTextOnLabel(text); // Zugriff auf die Instanz von AndererFrame über die Referenz namens "frame"
}
[...]
}