gpt4 book ai didi

java从另一个类打开一个框架

转载 作者:行者123 更新时间:2023-11-30 02:56:34 25 4
gpt4 key购买 nike

所以我试图做到这一点,以便当我单击按钮时,它将打开另一个类的框架,而我的文件选择器框架将无法打开。

这是一个名为 mainFrame 的类:

package javatut;

import java.awt.EventQueue;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;


public class mainFrame {

private JFrame frmMainwindow;
private JTextField textField;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
mainFrame window = new mainFrame();
window.frmMainwindow.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the application.
*/
public mainFrame() {
initialize();
}

/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmMainwindow = new JFrame();
frmMainwindow.setTitle("Mainwindow");
frmMainwindow.setBounds(100, 100, 280, 150);
frmMainwindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmMainwindow.getContentPane().setLayout(null);

JButton btnBrowse = new JButton("Browse");
btnBrowse.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
myButtonAction();
}
});
btnBrowse.setBounds(155, 11, 100, 23);
frmMainwindow.getContentPane().add(btnBrowse);

textField = new JTextField();
textField.setBounds(10, 12, 135, 20);
frmMainwindow.getContentPane().add(textField);
textField.setColumns(10);
}

private void myButtonAction(){
Toolkit.getDefaultToolkit().beep();
}
}

这是 FileChooser 类,称为简单的框架:

package javatut;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFileChooser;
import javax.swing.JFrame;

public class frame {


private JFrame frmFilechooser;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frame window = new frame();
window.frmFilechooser.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the application.
*/
public frame() {
initialize();
}

/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmFilechooser = new JFrame();
frmFilechooser.setTitle("FileChooser");
frmFilechooser.setBounds(100, 100, 470, 350);
frmFilechooser.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmFilechooser.getContentPane().setLayout(new BorderLayout(0, 0));

JFileChooser fileChooser = new JFileChooser();
frmFilechooser.getContentPane().add(fileChooser);

}

}

最佳答案

最简单、最合乎逻辑的解决方案是将 myButtonAction() 方法更改为:

private void myButtonAction() {
JFileChooser fileChooser = new JFileChooser();
int result = fileChooser.showOpenDialog(frmMainwindow);
if (result==JFileChooser.APPROVE_OPTION) {
// do something with the chosen file ..
}
}

关于java从另一个类打开一个框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37089090/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com