因此,如果我单击“第一帧”中存在的按钮,则JTextField
中存在于另一个第二帧中的数字将增加。
第一帧包含JButton
,第二帧包含JTextField
。
请帮忙。
最佳答案
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
public class FirstFrame extends JFrame {
private static final long serialVersionUID = 1L;
private SecondFrame secondFrame;
private boolean flag = new Boolean(false);
private volatile Integer number;
@SuppressWarnings("unused")
private SwingWorker<Void, Void> swingWorker;
public FirstFrame() {
init();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new FirstFrame();
}
});
}
private void init() {
setTitle("First Frame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new GridLayout(2, 1));
setBounds(500, 300, 250, 110);
final JButton increaseBtn = new JButton("Increase Number");
final JButton start = new JButton("Start");
final JButton stop = new JButton("Stop");
stop.setEnabled(false);
JPanel panel = new JPanel();
increaseBtn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent evt) {
if (!flag) {
number = secondFrame.getNumber();
secondFrame.setNumber(number + 1);
}
}
});
start.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
flag = true;
startContinuousIncreasing();
start.setEnabled(false);
stop.setEnabled(true);
increaseBtn.setEnabled(false);
}
});
stop.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
flag = false;
stop.setEnabled(false);
start.setEnabled(true);
increaseBtn.setEnabled(true);
}
});
getContentPane().add(increaseBtn);
getContentPane().add(panel);
panel.add(start);
panel.add(stop);
setVisible(true);
secondFrame = SecondFrame.getSecondFrame();
increaseBtn.requestFocus();
}
private void startContinuousIncreasing() {
swingWorker = new SwingWorker<Void, Void>() {
@Override
protected Void doInBackground() throws Exception {
while (flag) {
number = secondFrame.getNumber();
secondFrame.setNumber(number + 1);
}
return null;
}
};
swingWorker.execute();
}
}
class SecondFrame extends JFrame {
private static final long serialVersionUID = 1L;
private static Integer number;
private JTextField textField;
private static SecondFrame secondFrame;
private SecondFrame() {
init();
}
public synchronized static SecondFrame getSecondFrame() {
if (secondFrame == null)
secondFrame = new SecondFrame();
return secondFrame;
}
private void init() {
setTitle("Second Frame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new GridLayout(1, 1));
setBounds(500, 412, 250, 70);
number = new Integer(0);
textField = new JTextField(String.valueOf(number));
textField.setFont(new Font("Dialog", Font.BOLD + Font.ITALIC, 22));
getContentPane().add(textField);
setVisible(true);
}
public synchronized Integer getNumber() {
return number;
}
public synchronized void setNumber(Integer num) {
try {
number = num;
this.textField.setText(String.valueOf(number));
} catch (NumberFormatException nfe) {
number = 0;
this.textField.setText(String.valueOf(number));
}
}
}