JFrame在单独的类中,ActionListener怎么样? [英] JFrame in separate class, what about the ActionListener?
问题描述
我正在尝试将Swing GUI与实际代码分开。简而言之,我希望用户启动一个过程(根据用户的选择);在这种情况下,将不再需要JFrame。
我无法弄清楚如何与GUI共享用户对GUI.class的选择。 b。对于我有什么建议吗?
这是我的代码:
public class Main {
public static void main(String [] args){
// Show GUI
java .awt.EventQueue.invokeLater(new Runnable(){
public void run(){
GUI gui = new GUI(templates);
gui.setVisible(true);
}
});
//根据用户的选择启动流程
}
}
公共类GUI扩展JFrame {
private static final long serialVersionUID = 1L;
public GUI(Object [] objects){
setTitle(GUI);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100,100,350,100);
setLocationRelativeTo(null);
JPanel cp = new JPanel();
cp.setBorder(new EmptyBorder(10,10,10,10));
setContentPane(cp);
JLabel lbl = new JLabel(选择:);
cp.add(lbl);
final JComboBox< String> comboBox = new JComboBox< String>(new String [] {One,Two,Three});
comboBox.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
setVisible(false);
dispose();
//与Main.class共享所选项目
}
});
cp.add(comboBox);
}
}
A这样做的好方法是使用 回调
机制。
步骤按照:
-
创建一个回调接口
interface Callback {
void execute(Object result);
}
-
GUI
class将实现Callback
接口,但不提供任何实现 -
Make
GUI
classabstract
抽象类GUI扩展JFrame实现Callback
-
现在创建一个
GUI的对象
提供回调的实际实现的类
界面 -
在这里你可以使用匿名类
GUI gui = new GUI(){
@Override
public void execute(对象结果) ){
System.out.println(你选择了+结果);
}
};
-
你可以传递任何东西
execute()
方法回调
。comboBox.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
setVisible(false);
dispose();
//与所选项目共享Main.class
//回调
执行(comboBox.getSelectedItem());
}
});
这里 Main
类负责捕获由 GUI
类定向的 Callback
的响应。
以下是代码:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
公共类Main {
public static void main(String [] args){
//显示GUI
java.awt.EventQueue.invokeLater(new Runnable( ){
public void run(){
GUI gui = new GUI(){
@Override
public void execute(Object result){
System.out.println(你选择了+结果);
}
};
gui.setVisible(true);
}
});
//根据用户的选择启动流程
}
}
接口回调{
void execute(对象结果) ;
}
抽象类GUI扩展JFrame实现Callback {
private static final long serialVersionUID = 1L;
public GUI(){
setTitle(GUI);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100,100,350,100);
setLocationRelativeTo(null);
JPanel cp = new JPanel();
cp.setBorder(new EmptyBorder(10,10,10,10));
setContentPane(cp);
JLabel lbl = new JLabel(选择:);
cp.add(lbl);
final JComboBox comboBox = new JComboBox(new String [] {One,Two,Three});
comboBox.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
setVisible(false);
dispose();
//与Main.class共享所选项目
execute(comboBox.getSelectedItem());
}
});
cp.add(comboBox);
}
}
I'm trying to separate my Swing GUI from my actual code. In short, I want the user to kick off a process (based on the user's selections); in this case, the JFrame will no longer be needed.
What I couldn't figure out is how to share the user's selection from the GUI.class with the Main.class.
Do you have any advice for me?
Here's my code:
public class Main {
public static void main(String[] args) {
// Show GUI
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
GUI gui = new GUI(templates);
gui.setVisible(true);
}
});
// Kick off a process based on the user's selection
}
}
public class GUI extends JFrame {
private static final long serialVersionUID = 1L;
public GUI(Object[] objects) {
setTitle("GUI");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 350, 100);
setLocationRelativeTo(null);
JPanel cp = new JPanel();
cp.setBorder(new EmptyBorder(10, 10, 10, 10));
setContentPane(cp);
JLabel lbl = new JLabel("Selection:");
cp.add(lbl);
final JComboBox<String> comboBox = new JComboBox<String>(new String[] { "One", "Two", "Three" });
comboBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setVisible(false);
dispose();
// Share the selected item with Main.class
}
});
cp.add(comboBox);
}
}
A Good way of doing this is use Callback
mechanism.
Steps to follow:
create a callback interface
interface Callback { void execute(Object result); }
GUI
class will implementCallback
interface but without providing any implementationMake
GUI
classabstract
abstract class GUI extends JFrame implements Callback
Now create an object of
GUI
class providing actual implementation ofCallback
interfaceHere you can use Anonymous class
GUI gui = new GUI() { @Override public void execute(Object result) { System.out.println("You have selected " + result); } };
You can pass any thing in
execute()
method ofCallback
.comboBox.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { setVisible(false); dispose(); // Share the selected item with Main.class // Callback execute(comboBox.getSelectedItem()); } });
Here Main
class is responsible for capturing the response of Callback
that is directed by GUI
class.
Here is the code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class Main {
public static void main(String[] args) {
// Show GUI
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
GUI gui = new GUI() {
@Override
public void execute(Object result) {
System.out.println("You have selected " + result);
}
};
gui.setVisible(true);
}
});
// Kick off a process based on the user's selection
}
}
interface Callback {
void execute(Object result);
}
abstract class GUI extends JFrame implements Callback {
private static final long serialVersionUID = 1L;
public GUI() {
setTitle("GUI");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 350, 100);
setLocationRelativeTo(null);
JPanel cp = new JPanel();
cp.setBorder(new EmptyBorder(10, 10, 10, 10));
setContentPane(cp);
JLabel lbl = new JLabel("Selection:");
cp.add(lbl);
final JComboBox comboBox = new JComboBox(new String[] { "One", "Two", "Three" });
comboBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setVisible(false);
dispose();
// Share the selected item with Main.class
execute(comboBox.getSelectedItem());
}
});
cp.add(comboBox);
}
}
这篇关于JFrame在单独的类中,ActionListener怎么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!