首页 \ 问答 \ 事件处理Swing组件(Event Handling Swing components)

事件处理Swing组件(Event Handling Swing components)

我正在尝试一点一点地学习Java,重新创建我用Python完成的应用程序 - 一个非常基础的库控制软件。 但是,我遇到了问题,因为事件处理,主要是因为(我认为)我在没有太多了解情况的情况下全面了解了Swing,但在我去的时候搞清楚了。

到目前为止,这是我的代码:

public class SEHBV extends JFrame{

    public SEHBV(){

        super("SEHBV Biblio 2.0");
        ImageIcon img = new ImageIcon("books.ico");
        setIconImage(img.getImage());

        JPanel p_ini, locar, devolver, buscar, administrar;
        JLabel l_dia, l_mes, l_ano, loca_cs, loca_cl, loca_prazo, loca_cb, locado_state;
        JTextField dia, mes, ano, loca_cs_tf, loca_cl_tf, loca_prazo_tf, loca_cb_tf, devolve_cod;
        JTextArea loca_prazo_data, loca_oper_res, mostra_multa;
        JButton data, loca_cb_bt, loca_commit, ver_multa;
        JList<String> loca_s_res, loca_cb_res, atrasos, locados;


        p_ini = new JPanel(new GridBagLayout());
        GridBagConstraints i = new GridBagConstraints();
        l_dia = new JLabel("Dia: ");
        l_mes = new JLabel("Mês: ");
        l_ano = new JLabel("Ano: ");
        dia = new JTextField(6);
        mes = new JTextField(6);
        ano = new JTextField(6);
        data = new JButton("Afirmar Data");
        atrasos = new JList<String>();  
        atrasos.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        atrasos.setLayoutOrientation(JList.VERTICAL);
        atrasos.setVisibleRowCount(10);
        JScrollPane scroll_atrasos = new JScrollPane(atrasos);
        atrasos.setBackground(Color.WHITE);
        atrasos.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));

        i.fill = GridBagConstraints.HORIZONTAL;
        i.gridx = 0;
        i.gridy = 0;
        p_ini.add(l_dia, i);
        i.gridx = 1;
        p_ini.add(dia, i);
        i.gridx = 2;
        p_ini.add(l_mes, i);
        i.gridx = 3;
        p_ini.add(mes, i);
        i.gridx = 4;
        p_ini.add(l_ano, i);
        i.gridx = 5;
        p_ini.add(ano, i);
        i.gridx = 6;
        p_ini.add(data, i);
        i.gridy = 1;
        i.gridx = 0;
        i.gridwidth = 7;
        p_ini.add(scroll_atrasos, i);

        //GUI Locação 
        locar = new JPanel(new GridBagLayout());
        GridBagConstraints l = new GridBagConstraints();
        l.gridx = 0;
        l.gridy = 0;
        JPanel loca_socios = new JPanel(new FlowLayout());
        JPanel loca_oper = new JPanel(new GridBagLayout());
        GridBagConstraints o = new GridBagConstraints();
        JPanel loca_busca = new JPanel(new GridBagLayout());
        GridBagConstraints b = new GridBagConstraints();


        locar.add(loca_socios, l); //Busca de Sócios na janela de Locação

        loca_s_res = new JList<String>();
        loca_s_res.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        loca_s_res.setLayoutOrientation(JList.VERTICAL);
        loca_s_res.setModel(Runner.nome_socios);
        loca_s_res.setVisibleRowCount(25);
        JScrollPane scroll_loca_s = new JScrollPane(loca_s_res);
        loca_s_res.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
        loca_socios.add(scroll_loca_s);

        l.gridx = 1;
        locar.add(loca_oper, l); //Locação propriamente dita
        o.weighty = 1;
        o.weightx = 1;
        o.anchor = GridBagConstraints.NORTHWEST;
        o.insets = new Insets(1,1,1,1);
        loca_cs = new JLabel("Código do Sócio: ");
        o.fill = GridBagConstraints.HORIZONTAL;
        o.gridx = 0;
        o.gridy = 0;
        loca_oper.add(loca_cs, o);

        loca_cs_tf = new JTextField(5);
        loca_cs_tf.setEditable(false);
        loca_cs_tf.setBackground(Color.WHITE);
        o.fill = GridBagConstraints.BOTH;
        o.gridx = 1;
        o.gridy = 0;
        loca_oper.add(loca_cs_tf, o);

        loca_cl = new JLabel("Código do Livro: ");
        o.fill = GridBagConstraints.BOTH;
        o.gridx = 0;
        o.gridy = 1;
        loca_oper.add(loca_cl, o);

        loca_cl_tf = new JTextField(5);
        o.fill = GridBagConstraints.BOTH;
        o.gridx = 1;
        o.gridy = 1;
        loca_oper.add(loca_cl_tf, o);

        loca_prazo = new JLabel("Prazo para devolução (em dias): ");
        o.fill = GridBagConstraints.BOTH;
        o.gridx = 0;
        o.gridy = 2;
        loca_oper.add(loca_prazo, o);

        loca_prazo_tf = new JTextField(5);
        o.fill = GridBagConstraints.BOTH;
        o.gridx = 1;
        o.gridy = 2;
        loca_oper.add(loca_prazo_tf, o);

        loca_prazo_data = new JTextArea();
        loca_prazo_data.setBackground(getForeground());
        o.gridx = 0;
        o.gridy = 3;
        o.gridwidth = 3;
        loca_oper.add(loca_prazo_data, o);

        loca_commit = new JButton("Realizar Locação");
        o.fill = GridBagConstraints.BOTH;
        o.gridx = 0;
        o.gridy = 4;
        o.gridwidth = 3;
        loca_oper.add(loca_commit, o);


        loca_oper_res = new JTextArea();
        loca_oper_res.setBackground(getForeground());
        o.gridy = 5;
        loca_oper.add(loca_oper_res, o);

        l.gridx = 2;
        locar.add(loca_busca, l);
        loca_cb = new JLabel("Chave de Busca: ");
        b.fill = GridBagConstraints.HORIZONTAL;
        b.gridx = 0;
        b.gridy = 0;
        loca_busca.add(loca_cb, b);

        loca_cb_tf = new JTextField(20);
        b.fill = GridBagConstraints.HORIZONTAL;
        b.gridx = 1;
        b.gridy = 0;
        loca_busca.add(loca_cb_tf, b);

        loca_cb_bt = new JButton("Busca Rápida");
        b.fill = GridBagConstraints.HORIZONTAL;
        b.gridx = 2;
        b.gridy = 0;
        loca_busca.add(loca_cb_bt, b);

        loca_cb_res = new JList<String>();
        loca_cb_res.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        loca_cb_res.setLayoutOrientation(JList.VERTICAL);
        b.fill = GridBagConstraints.HORIZONTAL;
        loca_cb_res.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
        b.gridx = 0;
        b.gridy = 1;
        b.gridwidth = 3;
        b.gridheight = 2;
        loca_cb_res.setVisibleRowCount(25);
        JScrollPane scroll_loca_cb = new JScrollPane(loca_cb_res);
        loca_cb_res.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
        loca_busca.add(scroll_loca_cb, b);


    // Other Tabs of the GUI


    //GUI Geral
    JTabbedPane tp = new JTabbedPane();

    tp.addTab("Página inicial (Alt+I)", null, p_ini, "pág. inicial");
    tp.addTab("Locação (Alt+L)", locar);
    tp.addTab("Devolução (Alt+D)", devolver);
    tp.addTab("Busca Avançada (Alt+B)", buscar);
    tp.addTab("Administração (Alt+A)", administrar);
    tp.setMnemonicAt(0, KeyEvent.VK_I);
    tp.setMnemonicAt(1, KeyEvent.VK_L);
    tp.setMnemonicAt(2, KeyEvent.VK_D);
    tp.setMnemonicAt(3, KeyEvent.VK_B);
    tp.setMnemonicAt(4, KeyEvent.VK_A);

    add(tp);
}

所以,假设我正在尝试处理loca_commit JButton的点击。 我正在尝试创建一个事件处理程序 - 根据Java教程和其他StackOverflow问题/答案 - 但处理程序不识别loca_commit 。 现在我只是想让它工作,然后我会用它来调用一个方法,但如果我不能让它创建一个弹出窗口,那么,你明白了。

所以,到目前为止我的处理程序代码是这样的:

    private class LocaHandler implements ActionListener{
        public void actionPerformed(ActionEvent event){

            String string = "";

            if(event.getSource()==loca_commit)
                string=String.format("Botão Apertado");

        JOptionPane.showMessageDialog(null, string);
        }

你们能在这里点亮一下吗?


I'm trying to learn Java bit by bit, recreating an application I completed in Python - a library control software, very basic. I am having problems, though, with Event Handling, primarily because (I think) I went full-blown in to Swing without knowing much about it, but figuring it out as I went.

Here's my code, so far:

public class SEHBV extends JFrame{

    public SEHBV(){

        super("SEHBV Biblio 2.0");
        ImageIcon img = new ImageIcon("books.ico");
        setIconImage(img.getImage());

        JPanel p_ini, locar, devolver, buscar, administrar;
        JLabel l_dia, l_mes, l_ano, loca_cs, loca_cl, loca_prazo, loca_cb, locado_state;
        JTextField dia, mes, ano, loca_cs_tf, loca_cl_tf, loca_prazo_tf, loca_cb_tf, devolve_cod;
        JTextArea loca_prazo_data, loca_oper_res, mostra_multa;
        JButton data, loca_cb_bt, loca_commit, ver_multa;
        JList<String> loca_s_res, loca_cb_res, atrasos, locados;


        p_ini = new JPanel(new GridBagLayout());
        GridBagConstraints i = new GridBagConstraints();
        l_dia = new JLabel("Dia: ");
        l_mes = new JLabel("Mês: ");
        l_ano = new JLabel("Ano: ");
        dia = new JTextField(6);
        mes = new JTextField(6);
        ano = new JTextField(6);
        data = new JButton("Afirmar Data");
        atrasos = new JList<String>();  
        atrasos.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        atrasos.setLayoutOrientation(JList.VERTICAL);
        atrasos.setVisibleRowCount(10);
        JScrollPane scroll_atrasos = new JScrollPane(atrasos);
        atrasos.setBackground(Color.WHITE);
        atrasos.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));

        i.fill = GridBagConstraints.HORIZONTAL;
        i.gridx = 0;
        i.gridy = 0;
        p_ini.add(l_dia, i);
        i.gridx = 1;
        p_ini.add(dia, i);
        i.gridx = 2;
        p_ini.add(l_mes, i);
        i.gridx = 3;
        p_ini.add(mes, i);
        i.gridx = 4;
        p_ini.add(l_ano, i);
        i.gridx = 5;
        p_ini.add(ano, i);
        i.gridx = 6;
        p_ini.add(data, i);
        i.gridy = 1;
        i.gridx = 0;
        i.gridwidth = 7;
        p_ini.add(scroll_atrasos, i);

        //GUI Locação 
        locar = new JPanel(new GridBagLayout());
        GridBagConstraints l = new GridBagConstraints();
        l.gridx = 0;
        l.gridy = 0;
        JPanel loca_socios = new JPanel(new FlowLayout());
        JPanel loca_oper = new JPanel(new GridBagLayout());
        GridBagConstraints o = new GridBagConstraints();
        JPanel loca_busca = new JPanel(new GridBagLayout());
        GridBagConstraints b = new GridBagConstraints();


        locar.add(loca_socios, l); //Busca de Sócios na janela de Locação

        loca_s_res = new JList<String>();
        loca_s_res.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        loca_s_res.setLayoutOrientation(JList.VERTICAL);
        loca_s_res.setModel(Runner.nome_socios);
        loca_s_res.setVisibleRowCount(25);
        JScrollPane scroll_loca_s = new JScrollPane(loca_s_res);
        loca_s_res.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
        loca_socios.add(scroll_loca_s);

        l.gridx = 1;
        locar.add(loca_oper, l); //Locação propriamente dita
        o.weighty = 1;
        o.weightx = 1;
        o.anchor = GridBagConstraints.NORTHWEST;
        o.insets = new Insets(1,1,1,1);
        loca_cs = new JLabel("Código do Sócio: ");
        o.fill = GridBagConstraints.HORIZONTAL;
        o.gridx = 0;
        o.gridy = 0;
        loca_oper.add(loca_cs, o);

        loca_cs_tf = new JTextField(5);
        loca_cs_tf.setEditable(false);
        loca_cs_tf.setBackground(Color.WHITE);
        o.fill = GridBagConstraints.BOTH;
        o.gridx = 1;
        o.gridy = 0;
        loca_oper.add(loca_cs_tf, o);

        loca_cl = new JLabel("Código do Livro: ");
        o.fill = GridBagConstraints.BOTH;
        o.gridx = 0;
        o.gridy = 1;
        loca_oper.add(loca_cl, o);

        loca_cl_tf = new JTextField(5);
        o.fill = GridBagConstraints.BOTH;
        o.gridx = 1;
        o.gridy = 1;
        loca_oper.add(loca_cl_tf, o);

        loca_prazo = new JLabel("Prazo para devolução (em dias): ");
        o.fill = GridBagConstraints.BOTH;
        o.gridx = 0;
        o.gridy = 2;
        loca_oper.add(loca_prazo, o);

        loca_prazo_tf = new JTextField(5);
        o.fill = GridBagConstraints.BOTH;
        o.gridx = 1;
        o.gridy = 2;
        loca_oper.add(loca_prazo_tf, o);

        loca_prazo_data = new JTextArea();
        loca_prazo_data.setBackground(getForeground());
        o.gridx = 0;
        o.gridy = 3;
        o.gridwidth = 3;
        loca_oper.add(loca_prazo_data, o);

        loca_commit = new JButton("Realizar Locação");
        o.fill = GridBagConstraints.BOTH;
        o.gridx = 0;
        o.gridy = 4;
        o.gridwidth = 3;
        loca_oper.add(loca_commit, o);


        loca_oper_res = new JTextArea();
        loca_oper_res.setBackground(getForeground());
        o.gridy = 5;
        loca_oper.add(loca_oper_res, o);

        l.gridx = 2;
        locar.add(loca_busca, l);
        loca_cb = new JLabel("Chave de Busca: ");
        b.fill = GridBagConstraints.HORIZONTAL;
        b.gridx = 0;
        b.gridy = 0;
        loca_busca.add(loca_cb, b);

        loca_cb_tf = new JTextField(20);
        b.fill = GridBagConstraints.HORIZONTAL;
        b.gridx = 1;
        b.gridy = 0;
        loca_busca.add(loca_cb_tf, b);

        loca_cb_bt = new JButton("Busca Rápida");
        b.fill = GridBagConstraints.HORIZONTAL;
        b.gridx = 2;
        b.gridy = 0;
        loca_busca.add(loca_cb_bt, b);

        loca_cb_res = new JList<String>();
        loca_cb_res.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        loca_cb_res.setLayoutOrientation(JList.VERTICAL);
        b.fill = GridBagConstraints.HORIZONTAL;
        loca_cb_res.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
        b.gridx = 0;
        b.gridy = 1;
        b.gridwidth = 3;
        b.gridheight = 2;
        loca_cb_res.setVisibleRowCount(25);
        JScrollPane scroll_loca_cb = new JScrollPane(loca_cb_res);
        loca_cb_res.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
        loca_busca.add(scroll_loca_cb, b);


    // Other Tabs of the GUI


    //GUI Geral
    JTabbedPane tp = new JTabbedPane();

    tp.addTab("Página inicial (Alt+I)", null, p_ini, "pág. inicial");
    tp.addTab("Locação (Alt+L)", locar);
    tp.addTab("Devolução (Alt+D)", devolver);
    tp.addTab("Busca Avançada (Alt+B)", buscar);
    tp.addTab("Administração (Alt+A)", administrar);
    tp.setMnemonicAt(0, KeyEvent.VK_I);
    tp.setMnemonicAt(1, KeyEvent.VK_L);
    tp.setMnemonicAt(2, KeyEvent.VK_D);
    tp.setMnemonicAt(3, KeyEvent.VK_B);
    tp.setMnemonicAt(4, KeyEvent.VK_A);

    add(tp);
}

So, lets say I'm trying to handle the clicking of the loca_commit JButton. I'm trying to create an Event Handler - according to Java tutorials and other StackOverflow questions/answers - but the handler does not identify loca_commit. Right now I'm just trying to get it to work, then I'll use it for calling a method, but if I can't make it create a pop up, well, you get my point.

So, my code for the handler so far is this:

    private class LocaHandler implements ActionListener{
        public void actionPerformed(ActionEvent event){

            String string = "";

            if(event.getSource()==loca_commit)
                string=String.format("Botão Apertado");

        JOptionPane.showMessageDialog(null, string);
        }

Can you guys shed a light here?


原文:https://stackoverflow.com/questions/42825348
更新时间:2022-09-05 06:09

最满意答案

我不认为按照你描述的方式做到这一点。 最后我检查过,应用程序只能在他们的沙盒目录中写入,并且无法在其他应用程序的沙箱中读取(尽管IIRC曾经是其他一些可写目录)。

究竟,你想要达到的最终结果是什么?

如果您知道要读取数据的应用程序,则可以使用自定义URL方案。

如果您有某种类型的文件(扩展/内容类型,我认为),您可以使用UIDocumentInteractionController让用户选择一个应用程序来打开它。

如果您只想与任何应用程序共享一些数据,我能想到的最接近的是自定义UIPasteboard。 OpenUDID这样做是为了一个略微可疑的目的。


I don't think it's possible to do it the way you describe. Last I checked, apps can only write inside their sandbox dir and cannot read inside other apps' sandboxes (though IIRC there used to be a few other writable directories).

What, exactly, is the end result you are trying to achieve?

If you know the app you want to read the data, you can use a custom URL scheme.

If you have a file of a certain type (extension/content-type, I think), you can use UIDocumentInteractionController to let the user pick an app to open it in.

If you just want to share some data with any app, the closest I can think of is a custom UIPasteboard. OpenUDID does this for a slightly dubious purpose.

相关问答

更多
  • 在Android中,没有像ios那样的中央通知中心。 但你基本上可以使用Observable和Observer对象来实现你的任务。 你可以像下面这样定义一个类,只需修改它以供单例使用,并为同时使用添加同步,但其想法是相同的: public class ObservingService { HashMap observables; public ObservingService() { observables = new HashMap ...
  • 回答原始问题: Grand Central Dispatch(GCD)提供了一种在后台执行任务的机制,尽管它与AsyncTask的结构不同。 要异步执行某些操作,您只需要创建一个队列(如线程),然后将一个块传递给dispatch_async() ,以便在后台执行。 我发现它比AsyncTask更干净,因为没有涉及到子类; 无论您是否在后台执行代码,无论是在即插即用。 一个例子: dispatch_queue_t queue = dispatch_queue_create("com.yourdomain.yo ...
  • 这是我找到的最简单的解决方案: //--Init int myvar = 12; //--SAVE Data SharedPreferences preferences = context.getSharedPreferences("MyPreferences", Context.MODE_PRIVATE); SharedPreferences.Editor editor = preferences.edit(); editor.putInt("var1", myvar); editor.commi ...
  • 如果您要在应用程序之间共享数据,应用程序组可以成为您的案例的一个可能的解决方案。 否则,创建扩展并允许用户将数据存储在云存储中可能是一种可行的解决方案。 https://www.centerdevice.de/document-and-share-extensions-in-ios/ App groups can be a possible solution for your case if you are going to share data among your apps. Otherwise cre ...
  • 我使用NSURLConnection解决了这个问题。 并获得该代表的响应数据。 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response I solved the problem using NSURLConnection. And get response data by this delegate. - (void)connection:(NSURLConnection * ...
  • 如果您愿意,可以使用ToggleButton ,或者可能是CheckBox 。 从Android 4.0开始,还有Switch 。 ToggleButton, or possibly CheckBox if you prefer. As of Android 4.0, there is also Switch.
  • 检查android.webkit.WebView用于显示, WebViewClient用于处理链接点击。 Check out android.webkit.WebView for the display, and WebViewClient for dealing with clicks on links.
  • Android中的核心UI元素是View The core UI element in Android is the View
  • 我不认为按照你描述的方式做到这一点。 最后我检查过,应用程序只能在他们的沙盒目录中写入,并且无法在其他应用程序的沙箱中读取(尽管IIRC曾经是其他一些可写目录)。 究竟,你想要达到的最终结果是什么? 如果您知道要读取数据的应用程序,则可以使用自定义URL方案。 如果您有某种类型的文件(扩展/内容类型,我认为),您可以使用UIDocumentInteractionController让用户选择一个应用程序来打开它。 如果您只想与任何应用程序共享一些数据,我能想到的最接近的是自定义UIPasteboard。 O ...
  • 你有没有试过在NSString UIKit Additions类中使用sizeWithFont:方法,这里引用 ? 编辑: 我可以用的最好的方法是编写一个算法,使用这些sizeWithFont:方法尝试每个字符串的长度,直到字符串不再适合框。 此时,您知道您需要返回三个字符才能留出省略号的空间。 您应该能够将此算法优化为O(log(n))。 Have you tried using the sizeWithFont: methods in the NSString UIKit Additions categ ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。