2017-03-16 85 views
0

我試圖一點一點地學習Java,重新創建一個我在Python中完成的應用程序 - 一個非常基本的庫控制軟件。不過,我遇到了問題,主要是因爲(我認爲)我完全不知道太多關於Swing的內容,但是在我去的時候把它弄清楚了。事件處理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); 
     } 

你們可以流下了光嗎?

+0

順便說一句,目前爲止的主要內容僅僅是確定尺寸,使框架可見,真正的基礎。 – NicolasB

+0

注意:當試圖找出如何將動作偵聽器添加到按鈕時,該示例需要一個按鈕和一個動作偵聽器,而不是4個按鈕,3個文本區域,一堆標籤和文本字段,3個列表,一個圖像圖標.. *「順便說一句,目前爲止的主要內容是調整大小並使框架可見」*所以我們需要在編譯和嘗試代碼之前對其進行更改。爲了儘快提供更好的幫助,請發佈[MCVE]或[簡短,獨立,正確的示例](http://www.sscce.org/)。它應該包括一個'main'方法和進口,但儘可能多地留下其他組件。 –

回答

1

核心問題可能是上下文之一,LocaHandler可能沒有任何上下文SEHBVloca_commit按鈕,所以你不能引用它(它是超出上下文)。

有幾個你可能會解決這個問題的方法...

你可以...

傳遞的loca_commitLocaHandler實例的引用,但除非你打算利用LocaHandler到處理多個動作,真的沒有任何意義,這意味着...

你可以...

LocaHandler只負責做一兩件事,什麼都loca_commit需要它。這導致你進入Actions API

如何過......

你可以...

利用actionCommand屬性支持的JButton的和ActionEvent領域

loca_commit = new JButton("Realizar Locação"); 
loca_commit.setActionCommand("locaCommit"); 

//... 

private class LocaHandler implements ActionListener{ 
    public void actionPerformed(ActionEvent event){ 
     String string = ""; 

     if("locaCommit".equals(event.getActionCommand())) 
      string=String.format("Botão Apertado"); 

     JOptionPane.showMessageDialog(null, string); 
} 

這意味着您可以使用相同的LocaHandler實例來處理多個命令(通過擴展if語句)

我個人的偏好是使用操作API或者匿名類,注重處理到一個責任,如果做不好,反而會增加類的可重用性

+0

謝謝,第三個解決方案應該可以工作。每個JPanel(每個選項卡)執行多個功能,所以它對我的情況是最佳的。 – NicolasB

0

一個使它的工作方式是附加初始化它後,按鈕上的動作監聽器。在你的情況下,請嘗試以下操作:

loca_commit = new JButton("Realizar Locação"); 
//other code 

loca_commit.addActionListener(new ActionListener() { 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      JOptionPane.showMessageDialog(null, "Hello"); 
     } 
}); 
loca_oper.add(loca_commit, o); 

請記住,這只是一種方法。還有其他多種方法可以實現相同的結果。