[英]Java netbeans : how to send a different value to JFrame in netbeans(Resume)
so I have a frame 1 and frame 2 in the frame 1 by having 4 and has 1 JButton JTextField at 4 JTextField a user to input an integer value .. after the user input, the user presses a JButton and JFrame will feature 2 .. 因此,我在第1帧中有第1帧,在第2帧中有第2帧,并且在4 JTextField处有1个JButton JTextField用户输入整数值..在用户输入后,用户按下JButton,JFrame将具有2 ..
and in the second frame I have 1 JTextArea which will print out a value that a user input 在第二帧中,我有1个JTextArea,它将打印出用户输入的值
so how to send values from frame 1 to frame 2? 那么如何将值从第1帧发送到第2帧?
actually in this project I've given constructor in which I made into a class and in Jframe1 "coba.java" I make new objeck with this code: 实际上,在这个项目中,我给了我构造成一个类的构造函数,并在Jframe1“ coba.java”中使用以下代码创建了新的objeck:
coba ar = new coba();
in a Jframe1 I have a method in which DDA has a code: 在Jframe1中,我有一个DDA具有代码的方法:
double X0 = Double.parseDouble (x0.getText ());
double X1 = Double.parseDouble (x1.getText ());
double Y0 = Double.parseDouble (y0.getText ());
double Y1 = Double.parseDouble (y1.getText ());
int no = 1;
ar.X0 = X0;
ar.X1 = X1;
ar.Y0 = Y0;
ar.Y1 = Y1;
You can make a thread that can check some static variables for changes and update the frames accordingly. 您可以创建一个线程,该线程可以检查某些静态变量的更改并相应地更新框架。 For example somewhere after showing the two frames:
例如,显示两个框架后的某处:
new Thread(new Runnable(){
public void run(){
try{
while(true){
if(coba.HASNEWVALUE){
updateFrame(); // some function that does the updating and communicating
coba.HASNEWVALUE = false;
}
}
}
catch(Exception e){
e.printStackTrace();
}
}
}).start();
Whenever you want to pass a new value, set the appropriate values and set coba.HASNEWVALUE to true, this way your frame will fetch the required update automatically through the updateFrame() function everytime coba.HASNEWVALUE is TRUE. 每当您要传递新值时,都设置适当的值并将coba.HASNEWVALUE设置为true,这样,每次coba.HASNEWVALUE为TRUE时,框架将通过updateFrame()函数自动获取所需的更新。
Basically, Frame 1
will need a reference to Frame 2
either directly or indirectly via some kind of observer implementation... 基本上,
Frame 1
需要通过某种观察者实现直接或间接引用Frame 2
...
This way, all you need to do is provide some kind of method in Frame 2
that will allow you to pass the information you need to it so it can update the text area in question. 这样,您所需要做的就是在第
Frame 2
提供某种方法,该方法使您可以将所需的信息传递给它,以便可以更新有问题的文本区域。
Personally, I prefer to use interface
s for this, as it prevents the caller from messing with things it shouldn't 就我个人而言,我更喜欢使用
interface
,因为它可以防止调用者弄乱它不应该使用的东西
Oh, and you might also want to have a read through The Use of Multiple JFrames: Good or Bad Practice? 哦,您可能还想通读《使用多个JFrame:好做法还是坏做法》?
For example... 例如...
The NotePad
interface
prevents SecretaryPane
from making changes to the underlying implementation, because the only method it actually knows about is the addNote
method NotePad
interface
可防止SecretaryPane
更改基础实现,因为它实际上知道的唯一方法是addNote
方法。
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class CrossCalling {
public static void main(String[] args) {
new CrossCalling();
}
public CrossCalling() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
NotePadPane notePadPane = new NotePadPane();
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new SecretaryPane(notePadPane));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
JFrame noteFrame = new JFrame("Testing");
noteFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
noteFrame.setLayout(new BorderLayout());
noteFrame.add(notePadPane);
noteFrame.pack();
noteFrame.setLocation(frame.getX(), frame.getY() + frame.getHeight());
noteFrame.setVisible(true);
}
});
}
public interface NotePad {
public void addNote(String note);
}
public class SecretaryPane extends JPanel {
private NotePad notePad;
public SecretaryPane(NotePad pad) {
this.notePad = pad;
setLayout(new GridBagLayout());
JButton btn = new JButton("Make note");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
pad.addNote(DateFormat.getTimeInstance().format(new Date()));
}
});
add(btn);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
public class NotePadPane extends JPanel implements NotePad {
private JTextArea ta;
public NotePadPane() {
setLayout(new BorderLayout());
ta = new JTextArea(10, 20);
add(new JScrollPane(ta));
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
public void addNote(String note) {
ta.append(note + "\n");
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.