您好我想知道如何从另一个处理jFrame,因为我想在其textfields中创建具有新值的该类的新实例,因此第一个jFrame是这样的:
public class Frame1 extends javax.swing.JFrame implements ActionListener {
Frame2 f;
public Frame1() {
initComponents();
this.setLocationRelativeTo(null);
}
private void rbtnShowFrame2ActionPerformed(java.awt.event.ActionEvent evt) {
f = new Frame2();
f.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
this.dispose(); //I TRIED TO DISPOSING IT HERE BUT DOESN'T WORK
}
}
所以我想在其他jFrame中只有当我触发一个botton执行的事件动作时才处理jFrame1,如果没有发生我不想处理它,我不知道我是否可以这样做使用ActionListener,这是第二个jFrame:
public class Frame2 extends javax.swing.JFrame {
public Frame2() {
initComponents();
this.setLocationRelativeTo(null);
Frame1 f1 = new Frame1();
this.cmdOk.addActionListener(cGUI);
}
private void cmdOkActionPerformed(java.awt.event.ActionEvent evt) {
//Here is where i want to dispose() the other jFrame1
//to create a new instance and pass the value using public static jTextFields
f1.labelNumeroCotizacion.setText(this.labelNumCotizacionEnviar.getText());
f1.setVisible(true);
}
}
对不起我的代码,我是使用OOP的新手!谢谢所有人......
答案 0 :(得分:1)
以下是如何从另一个JFrame配置JFrame的示例:
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class Demo
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
FrameA one = new FrameA();
FrameB two = new FrameB(one);
one.setVisible(true);
two.setVisible(true);
}
});
}
}
class FrameA extends JFrame
{
private static final long serialVersionUID = 1812279930292019387L;
public FrameA()
{
super("Frame A");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);
setLocationRelativeTo(null);
setResizable(false);
}
}
class FrameB extends JFrame
{
private static final long serialVersionUID = 5126089271972476434L;
public FrameB(final JFrame otherFrame)
{
super("Frame B");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);
setLayout(new GridBagLayout());
setLocationRelativeTo(otherFrame);
JButton button = new JButton("Dispose Other");
button.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
otherFrame.dispose();
}
});
add(button);
setResizable(false);
}
}