2014-01-10 1 views
0

после поиска ответа в течение 3 часов, я вот-вот должен отказаться от этой идеи:Java JDialogs Как передавать информацию между?

Я делаю приложение, которое отображает последователей стримера Twitch. Несколько функций, которые я пытаюсь добавить:

рамка дисплея представляет собой отдельное окно из рамки управления. Я пытаюсь использовать (JFrame как окно отображения) (JDialog как панель управления) И кроме того: Настройки находятся в другом JDialog (этот имеет Modal (true)) Настройки должны иметь возможность отправлять информацию JFrame, такую ​​как: «имя пользователя» и «цвет текста» И настройки JDialog будут отображаться только при нажатии «настроек» на элементах управления JDialog. Он установитVisible (false), когда вы нажимаете «Сохранить настройки» или X. На элементах управления JDialog (b_console) должен получать сообщения об ошибках и такую ​​информацию. И на том же JDialog, «filler» должен получить счетчик следов и тому подобное.

Здесь следует моему коду с участием трансфертов, перечисленных выше:

package javafollowernotifier; 
import java.awt.*; 
import java.awt.event.*; 
import java.awt.Graphics.*; 
import javax.swing.*; 
import java.io.*; 
import java.net.URL; 

public class JavaFollowerNotifier extends JFrame implements ComponentListener 
{ 
    Settings settings = new Settings(); 
    ControlPanel ctrlPnl = new ControlPanel(); 

    public JavaFollowerNotifier() 
    {   
     try 
     { 
      settings.readSettings(); 
     } 
     catch(Exception e) 
     { 
      ctrlPnl.b_console.setText("Error"); 
      System.out.println(e); 
     } 
    } 

    public void grabFollower() 
    { 
     ctrlPnl.b_console.setText("Retrieving Info..."); 

     try 
     { 
      URL twitch = new URL("https://api.twitch.tv/kraken/channels/" + savedSettings[1] + "/follows?limit=1&offset=0"); 

      ctrlPnl.b_console.setText("Retrieved"); 
     } 
     catch(Exception e) 
     { 
      ctrlPnl.b_console.setText("Error"); 
      System.out.println(e); 
     } 
    } 

    public void grabStats() 
    {    
     ctrlPnl.b_console.setText("Retrieving Info..."); 

     try 
     { 
      URL twitch = new URL("https://api.twitch.tv/kraken/channels/" + savedSettings[1] + "/follows?limit=1&offset=0"); 

      ctrlPnl.filler.setText("Followers: " + totalFollowers + "\nLatest: " + lastFollower); 
      ctrlPnl.b_console.setText("Retrieved"); 
     } 
     catch(Exception e) 
     { 
      ctrlPnl.b_console.setText("Error"); 
      System.out.println(e); 
     } 
    } 

    public void componentMoved(ComponentEvent arg0) 
    { 
     //this is only to *attach this JDialog to the JFrame and make it move together my plan is to have it undecorated as well 
     int x = this.getX() + this.getWidth(); 
     int y = this.getY(); 
     ctrlPnl.movePanel(x, y); 
    } 

    public void paint(Graphics g) 
    { 
     if(clearPaint == false) 
     { 
      //any "savedSettings[n]" are saved in Settings.java (just not in this version) 
      g.setColor(Color.decode(savedSettings[3])); 
      scaledFont = scaleFont(follower + " followed!", bounds, g, new Font(savedSettings[2], Font.PLAIN, 200)); 
     } 
    } 
} 



package javafollowernotifier; 
import java.awt.*; 
import java.awt.event.*; 
import java.io.*; 
import javax.imageio.ImageIO; 
import javax.swing.*; 

public class Settings extends JDialog implements ActionListener 
{ 
    JavaFollowerNotifier jfollow = new JavaFollowerNotifier(); 
    ControlPanel ctrlPnl = new ControlPanel(); 

    //here are the settings mention above 
    String[] savedSettings = {"imgs/b_b.jpg","username","font","color","Nightbot"}; 

    public Settings() 
    {   
     try 
     { 
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
     } 
     catch (Exception e) 
     { 
      ctrlPnl.b_console.setText("Error"); 
      System.out.println(e); 
     } 
    } 

    public void saveSettings() 
    { 
      savedSettings[4] = jfollow.lastFollower; 

     try 
     { 
      PrintWriter save = new PrintWriter("config.cfg"); 
      ctrlPnl.b_console.setText("Saving..."); 
      for(int i = 0; i < 5; i++) 
      { 
       save.println(savedSettings[i]); 
      } 
      save.close(); 
      ctrlPnl.b_console.setText("Saved"); 
     } 
     catch(Exception e) 
     { 
      ctrlPnl.b_console.setText("Error"); 
      System.out.println(e); 
      canClose = false; 
     } 

     readSettings(); 
     this.repaint(); 
    } 

    public void readSettings() 
    { 
     ctrlPnl.b_console.setText("Loading..."); 

     try 
     { 
     } 
     catch(Exception e) 
     { 
      ctrlPnl.b_console.setText("Error"); 
      System.out.println(e); 
     } 

     jfollow.lastFollower = savedSettings[4]; 

     try 
     { 
     } 
     catch(Exception e) 
     { 
      ctrlPnl.b_console.setText("Error"); 
      System.out.println(e); 
     } 

     ctrlPnl.b_console.setText("Loaded Settings"); 
    } 
} 



package javafollowernotifier; 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class ControlPanel extends JDialog implements ActionListener 
{  
    public ControlPanel() 
    { 
     try 
     { 
     } 
     catch (Exception e) 
     { 
      b_console.setText("Error"); 
      System.out.println(e); 
     } 
    } 

    public void movePanel(int x, int y) 
    { 
     //here is where i *attach the JDialog to the JFrame 
     controlPanel.setLocation(x, y); 
    } 

    public void actionPerformed(ActionEvent ie) 
    { 
     if(ie.getSource() == b_settings) 
     { 
      settings.frame.setVisible(true); 
     } 
    } 
} 
+1

И какова реальная проблема/вопрос? –

+0

Да, @peeskillet спросил его уже. В чем твоя проблема? – Diversity

+0

Также объясните, какая информация о пшенице вам нужна. –

ответ

2

Я пытался исправить вашу программу, но я не был слишком уверен, его поток. Поэтому я создал еще один простой. То, что я сделал, это передать метки из основного фрейма в конструкторы диалогов. В диалоговом окне я взял эти метки и изменил их с текстом, введенным в их текстовые поля. Если вы нажмете клавишу ВВОД после ввода текста в диалоговом окне, вы увидите текст в смене кадра

public class JavaFollowerNotifier1 extends JFrame{ 

    private JLabel controlDialogLabel = new JLabel(" "); 
    private JLabel settingDialogLabel = new JLabel(" "); 
    private ControlDialog control; 
    private SettingsDialog settings; 

    public JavaFollowerNotifier1() { 
     control = new ControlDialog(this, true, controlDialogLabel); 
     settings = new SettingsDialog(this, true, settingDialogLabel); 

.... 

class ControlDialog extends JDialog { 
    private JLabel label; 

    public ControlDialog(final Frame frame, boolean modal, final JLabel label) { 
     super(frame, modal); 
     this.label = label; 

.... 

class SettingsDialog extends JDialog { 
    private JLabel label; 

    public SettingsDialog(final Frame frame, boolean modal, final JLabel label) { 
     super(frame, modal); 
     this.label = label; 

проверить его и дайте мне знать, если у вас есть какие-либо вопросы

import java.awt.BorderLayout; 
import java.awt.Frame; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JButton; 
import javax.swing.JDialog; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 
import javax.swing.SwingUtilities; 

public class JavaFollowerNotifier1 extends JFrame{ 

    private JLabel controlDialogLabel = new JLabel(" "); 
    private JLabel settingDialogLabel = new JLabel(" "); 

    private JButton showControl = new JButton("Show Control"); 
    private JButton showSetting = new JButton("Show Settings"); 

    private ControlDialog control; 
    private SettingsDialog settings; 

    public JavaFollowerNotifier1() { 
     control = new ControlDialog(this, true, controlDialogLabel); 
     settings = new SettingsDialog(this, true, settingDialogLabel); 


     showControl.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent e) { 
       control.setVisible(true); 
      } 
     }); 
     showSetting.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent e) { 
       settings.setVisible(true); 
      } 
     }); 

     JPanel buttonPanel = new JPanel(); 
     buttonPanel.add(showControl); 
     buttonPanel.add(showSetting); 

     add(buttonPanel, BorderLayout.SOUTH); 
     add(controlDialogLabel, BorderLayout.NORTH); 
     add(settingDialogLabel, BorderLayout.CENTER); 

     pack(); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     setLocationRelativeTo(null); 
     setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       new JavaFollowerNotifier1(); 
      } 
     }); 
    } 

} 

class ControlDialog extends JDialog { 
    private JLabel label; 
    private JTextField field = new JTextField(15); 
    private JButton button = new JButton("Close"); 
    private String s = ""; 

    public ControlDialog(final Frame frame, boolean modal, final JLabel label) { 
     super(frame, modal); 
     this.label = label; 

     setLayout(new BorderLayout()); 
     add(field, BorderLayout.NORTH); 
     add(button, BorderLayout.CENTER); 

     pack(); 
     setLocationRelativeTo(frame); 

     field.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent e) { 
       s = field.getText(); 
       label.setText("Message from Control Dialog: " + s); 
      } 
     }); 
     button.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent e) { 
       ControlDialog.this.setVisible(false); 
      } 
     }); 
    } 
} 

class SettingsDialog extends JDialog { 
    private JLabel label; 
    private JTextField field = new JTextField(15); 
    private JButton button = new JButton("Close"); 
    private String s = ""; 

    public SettingsDialog(final Frame frame, boolean modal, final JLabel label) { 
     super(frame, modal); 
     this.label = label; 

     setLayout(new BorderLayout()); 
     add(field, BorderLayout.NORTH); 
     add(button, BorderLayout.CENTER); 

     pack(); 
     setLocationRelativeTo(frame); 

     field.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent e) { 
       s = field.getText(); 
       label.setText("Message from Settings Dialog: " + s); 
      } 
     }); 
     button.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent e) { 
       SettingsDialog.this.setVisible(false); 
      } 
     }); 
    } 
} 

enter image description here

+0

Спасибо большое! Это показывает мне, как это исправить: D – DaCoder

0

Обычно, когда я строй графического интерфейса, которые используют модальные диалоговые окна для ввода данных пользователя, я строй своего собственного класса, который расширяет JDialog или в некоторых случаях JFrame. В этом классе я выставляю метод getter для объекта, который я обычно вызываю DialgResult. Этот объект действует как Модель для ввода, который я собираю от пользователя. В классе, у которого есть кнопка или какой-либо элемент управления, который вызывает запрос пользователя для информации, я создаю его, показываю его как модальный диалог, а затем, когда он закрыт, я извлекаю объект, используя тот же getter.

Это очень примитивный пример:

package arg; 

import java.awt.Component; 
import java.awt.Dimension; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 

import javax.swing.BoxLayout; 
import javax.swing.JButton; 
import javax.swing.JDialog; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 

public class asdfas extends JFrame { 


public static void main(String[] args) { 
    asdfas ex = new asdfas(); 
    ex.setVisible(true); 
} 

public asdfas() { 
    init(); 
} 

private void init() { 
    this.setDefaultCloseOperation(EXIT_ON_CLOSE); 
    this.setBounds(100,100,200,200); 

    final JButton button = new JButton("Show modal dialog"); 
    button.addActionListener(new ActionListener() { 

     @Override 
     public void actionPerformed(ActionEvent arg0) { 
      Dialog d = new Dialog(); 
      d.setVisible(true);   

      button.setText(d.getDialogResult().value); 
      revalidate(); 
      repaint(); 
     } 
    }); 

    this.add(button);  
} 
class DialogResult { 
    public String value; 
} 
class Dialog extends JDialog { 
    JTextField tf = new JTextField(20); 
    private DialogResult result = new DialogResult(); 
    public Dialog() { 
     super(); 
     init(); 
    } 

    private void init() { 
     this.setModal(true); 

     this.setSize(new Dimension(100,100)); 
     JButton ok = new JButton("ok"); 
     ok.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent arg0) { 
       result = new DialogResult(); 
       result.value = tf.getText(); 
       setVisible(false); 
      } 

     }); 
     JPanel p = new JPanel(); 
     p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS)); 
     p.add(tf); 
     p.add(ok); 
     this.add(p); 
    } 

    public DialogResult getDialogResult() { 
     return result; 
    } 
} 
}