Java 在Swing应用程序中从侦听器返回信息
嗨,我有Java 在Swing应用程序中从侦听器返回信息,java,swing,actionlistener,Java,Swing,Actionlistener,嗨,我有大型机课程: public class MainFrame extends JFrame{ private JLabel originalLabel; private JLabel filteredImage; public MainFrame(){ setExtendedState(JFrame.MAXIMIZED_BOTH); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
大型机
课程:
public class MainFrame extends JFrame{
private JLabel originalLabel;
private JLabel filteredImage;
public MainFrame(){
setExtendedState(JFrame.MAXIMIZED_BOTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
initComponents();
setVisible(true);
}
private void initComponents(){
ImageFilterMenuBar menuBar = new ImageFilterMenuBar();
originalLabel = new JLabel("Test1");
filteredImage = new JLabel("Test2");
Component verticalStrut = Box.createVerticalStrut(10);
JPanel central = new JPanel();
central.add(originalLabel);
central.add(verticalStrut);
central.add(filteredImage);
add(new RadioButtonsPanel(), BorderLayout.SOUTH);
add(central, BorderLayout.CENTER);
setJMenuBar(menuBar);
}
}
public class ImageFilterMenuBar extends JMenuBar{
private JMenu fileMenu;
private JMenuItem openImage;
private JMenuItem exit;
public ImageFilterMenuBar(){
initCompoments();
}
private void initCompoments() {
fileMenu = new JMenu("File");
setMenuItems();
add(fileMenu);
}
private void setMenuItems(){
openImage = new JMenuItem("Open Image");
exit = new JMenuItem("Exit");
openImage.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,
InputEvent.CTRL_MASK));
exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_CANCEL,InputEvent.SHIFT_MASK));
openImage.addActionListener(new OpenListener());
exit.addActionListener(new ExiteListener());
fileMenu.add(openImage);
fileMenu.add(exit);
}
}
和MenuBar
class:
public class MainFrame extends JFrame{
private JLabel originalLabel;
private JLabel filteredImage;
public MainFrame(){
setExtendedState(JFrame.MAXIMIZED_BOTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
initComponents();
setVisible(true);
}
private void initComponents(){
ImageFilterMenuBar menuBar = new ImageFilterMenuBar();
originalLabel = new JLabel("Test1");
filteredImage = new JLabel("Test2");
Component verticalStrut = Box.createVerticalStrut(10);
JPanel central = new JPanel();
central.add(originalLabel);
central.add(verticalStrut);
central.add(filteredImage);
add(new RadioButtonsPanel(), BorderLayout.SOUTH);
add(central, BorderLayout.CENTER);
setJMenuBar(menuBar);
}
}
public class ImageFilterMenuBar extends JMenuBar{
private JMenu fileMenu;
private JMenuItem openImage;
private JMenuItem exit;
public ImageFilterMenuBar(){
initCompoments();
}
private void initCompoments() {
fileMenu = new JMenu("File");
setMenuItems();
add(fileMenu);
}
private void setMenuItems(){
openImage = new JMenuItem("Open Image");
exit = new JMenuItem("Exit");
openImage.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,
InputEvent.CTRL_MASK));
exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_CANCEL,InputEvent.SHIFT_MASK));
openImage.addActionListener(new OpenListener());
exit.addActionListener(new ExiteListener());
fileMenu.add(openImage);
fileMenu.add(exit);
}
}
在MenuBar类中,您可以使用OpenButton。它是OpenJFileChooser,并返回我选择的文件的url。
所以现在我不知道如何将这个url发送到我要显示这个文件的大型机类中。。
有什么想法吗?最简单的解决方案是将对
大型机的引用传递到图像过滤器菜单栏
:
ImageFilterMenuBar menuBar = new ImageFilterMenuBar(this);
然后,在大型机中添加如下方法:
public void setImageFile(File file) {
// do whatever here
}
在ImageFilterMenuBar
中,您将在成员变量中保留对大型机的引用,并在JFileChooser
返回文件后使用它调用setImageFile()
一个更难实现的解决方案是实现observer模式。这就是听众在摇摆中的工作方式。
将主框架注册为侦听器,另一个类将通知文件选择的更改
要打开文件选择器并获取所选文件,请执行以下操作:
JFileChooser chooser = new JFileChooser(path);
int result = chooser.showOpenDialog(mainFrame);
File file = chooser.getSelectedFile();
if (result == JFileChooser.APPROVE_OPTION && file != null && file.exists()) {
mainFrame.setImageFile(file);
}
最简单的解决方案是将对大型机的引用传递到ImageFilterMenuBar
:
ImageFilterMenuBar menuBar = new ImageFilterMenuBar(this);
然后,在大型机中添加如下方法:
public void setImageFile(File file) {
// do whatever here
}
在ImageFilterMenuBar
中,您将在成员变量中保留对大型机的引用,并在JFileChooser
返回文件后使用它调用setImageFile()
一个更难实现的解决方案是实现observer模式。这就是听众在摇摆中的工作方式。
将主框架注册为侦听器,另一个类将通知文件选择的更改
要打开文件选择器并获取所选文件,请执行以下操作:
JFileChooser chooser = new JFileChooser(path);
int result = chooser.showOpenDialog(mainFrame);
File file = chooser.getSelectedFile();
if (result == JFileChooser.APPROVE_OPTION && file != null && file.exists()) {
mainFrame.setImageFile(file);
}
不要扩展菜单栏或框架,只需在同一应用程序中保留对它们的引用
我正要添加“具有URL类属性的”,但不清楚您是如何使用URL的。如果是针对JEditorPane
的,我可能会声明它的实例来代替URL属性,然后直接在侦听器中的选择中设置页面。不要扩展菜单栏或框架,只需在同一个应用程序中保留对它们的引用即可
我正要添加“具有URL类属性的”,但不清楚您是如何使用URL的。如果是针对JEditorPane
的,我可能会声明它的实例来代替URL属性,然后直接在侦听器中的选择中设置页面。ImageFilterManubar类如何知道文件选择器选择文件?请参阅我编辑的答案。如果您还有更多问题,请分别提问。ImageFilterManubar类如何知道文件选择器选择文件?请参阅我编辑的答案。如果你有更多的问题,请分别提问。我写的问题是直截了当的。我发送图像的url。而不是展示它。这是我在SWING的第一次体验,所以可能是我做错了什么或者程序结构不正确。。。所以如果你有建议,欢迎你。你知道互联网上有什么好例子吗?所有的信息都会对我的问题有帮助。我发送图像的url。而不是展示它。这是我在SWING的第一次体验,所以可能是我做错了什么或者程序结构不正确。。。所以如果你有建议,欢迎你。你知道互联网上有什么好例子吗?所有信息都将是有用的