2016-04-04 9 views
1

には2つのJPanelコンポーネントがあります。最初のパネル(firstP)には、ニュース・サーバーからメッセージ・ヘッダーをロードするJListが含まれています。 2番目のパネル(secondP)では、メッセージの本文を読み込みたいと思います。JPanelsは同じウィンドウに情報を表示します

リストから項目を選択すると、メッセージの本文がロードされるニュースパネルが開きます(これは現在の動作です)が、これは気に入らないです。

secondPのメッセージの場合、同じフレーム内のfirstPのヘッダーを選択すると、本文を読み込みたいと思います。私は働きたいどのよう

を:それは今どのように動作するか

ここでは、2枚のパネル

public GroupViewer(NewsGroupList groupl, NewsGroup group) 
    { 
     super(group.name()); 
     addWindowListener(new WindowAdapter() { 
      public void windowClosing(WindowEvent e) 
      { 
       Close(); 
      } 
     }); 
     JMenu M1; 
     JMenuBar Bar; 

     GroupList=groupl; 
     this.group=group; 
       JPanel container = new JPanel(); 
     JPanel mainP=new JPanel(); 
       JPanel secondPanel= new JPanel(); 

       container.setLayout(new GridLayout(1,2)); 

       GridBagLayout grid=new GridBagLayout(); 
       GridBagConstraints c=new GridBagConstraints(); 

     secondPanel.setLayout(grid); 

     c.weightx=0; 
     c.weighty=0; 
     c.gridwidth=1; 
     c.anchor=GridBagConstraints.EAST; 
     c.fill=GridBagConstraints.NONE; 
     JLabel lbl=new JLabel("Subject :"); 
     grid.setConstraints(lbl, c); 
     secondPanel.add(lbl); 

     c.gridwidth=GridBagConstraints.REMAINDER; 
     c.anchor=GridBagConstraints.WEST; 
     c.fill=GridBagConstraints.HORIZONTAL; 
     Subject=new JTextField(""); 
     grid.setConstraints(Subject, c); 
     secondPanel.add(Subject); 
     if (Type==VIEW) 
      Subject.setEditable(false); 

     c.gridwidth=1; 
     c.anchor=GridBagConstraints.EAST; 
     c.fill=GridBagConstraints.NONE; 
     JLabel lbl1=new JLabel("Newsgroups :"); 
     grid.setConstraints(lbl1, c); 
     secondPanel.add(lbl1); 

     c.gridwidth=GridBagConstraints.REMAINDER; 
     c.anchor=GridBagConstraints.WEST; 
     c.fill=GridBagConstraints.HORIZONTAL; 
     if (((Type&NEWS)!=0)||((Type&VIEW)!=0)) 
      NewsGroups=new JTextField(group.name()); 
     else 
      NewsGroups=new JTextField(""); 
     if (Type==VIEW) 
      NewsGroups.setEditable(false); 
     grid.setConstraints(NewsGroups, c); 
     secondPanel.add(NewsGroups); 

     c.gridwidth=1; 
     c.anchor=GridBagConstraints.EAST; 
     c.fill=GridBagConstraints.NONE; 
     JLabel lbl2=new JLabel("To :"); 
     grid.setConstraints(lbl2, c); 
     secondPanel.add(lbl2); 

     c.gridwidth=GridBagConstraints.REMAINDER; 
     c.anchor=GridBagConstraints.WEST; 
     c.fill=GridBagConstraints.HORIZONTAL; 
     To=new JTextField(""); 
     grid.setConstraints(To, c); 
     secondPanel.add(To); 
     if (Type==VIEW) 
      To.setEditable(false); 

     if (Type==VIEW) 
     { 
      c.gridwidth=1; 
      c.anchor=GridBagConstraints.EAST; 
      c.fill=GridBagConstraints.NONE; 
      JLabel lbl3=new JLabel("Date :"); 
      grid.setConstraints(lbl3, c); 
      secondPanel.add(lbl3); 

      c.gridwidth=GridBagConstraints.REMAINDER; 
      c.anchor=GridBagConstraints.WEST; 
      c.fill=GridBagConstraints.HORIZONTAL; 
      Date=new JTextField(formatDate(ref.getDate())); 
      Date.setEditable(false); 
      grid.setConstraints(Date, c); 
      secondPanel.add(Date); 
     } 

     c.weightx=1; 
     c.weighty=1; 
     c.gridwidth=GridBagConstraints.REMAINDER; 
     c.fill=GridBagConstraints.BOTH; 
     Body=new JTextArea("", 40, 80); 
       grid.setConstraints(Body, c); 
     secondPanel.add(Body); 

       c.weightx = 1; 
       c.weighty = 1; 
       c.fill = GridBagConstraints.BOTH; 
       JScrollPane scrollPane = new JScrollPane(Body); 
       scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); 
       grid.setConstraints(scrollPane, c); 
       secondPanel.add(scrollPane); 



       if (Type==VIEW) 
      Body.setEditable(false); 

     if (((Type&REPLY)!=0)||((Type&VIEW)!=0)) 
     { 
      if ((Type&REPLY)!=0) 
      { 
       int pos=-1, pos1; 
       boolean last=false; 
       String OrigBody; 
       try 
       { 
        OrigBody=RefArticle.getBody(Group.getServer()); 
        do 
        { 
         pos1=OrigBody.indexOf('\n', pos+1); 
         if (pos1==-1) 
         { 
          last=true; 
          pos1=OrigBody.length()-1; 
         } 
        Body.append(">"+OrigBody.substring(pos+1, pos1+1)); 
        pos=pos1; 
        } 
        while (!last); 
       } 
       catch (Exception ex) 
       { 
        System.out.println("Unable to get body for this article\n"+ ex.getMessage()); 
       } 
      } 
      else 
      { 
       try 
       { 
        Body.setText(RefArticle.getBody(Group.getServer())); 
       } 
       catch (Exception ex) 
       { 
        System.out.println("Unable to get body for this article\n"+ ex.getMessage()); 
       } 
      } 
      String subject=RefArticle.getHeaderField("Subject"); 
      if (subject.toLowerCase().startsWith("re: ")||((Type&VIEW)!=0)) 
       Subject.setText(subject); 
      else 
       Subject.setText("Re: "+subject); 
      if (((Type)!=0)||((Type&VIEW)!=0)) 
       To.setText(RefArticle.getHeaderField("From")); 
     } 
     attachments=new JPanel(); 
     attachments.add("Center", new JLabel("No attachments yet .")); 
     split=new JSplitPane(JSplitPane.VERTICAL_SPLIT, true, secondPanel, attachments); 
     split.setOneTouchExpandable(true); 
     getContentPane().add(split); 

     if (Type==VIEW) 
     { 
      HViewer=new HeaderViewer(RefArticle.getHeaderField("Message-ID")); 
      HViewer.setArticle(RefArticle); 
     } 
     split.setDividerLocation(0.9); 




     listModel=new DefaultListModel(); 
     lst=new JList(listModel); 
     JScrollPane scroller=new JScrollPane(lst); 
     lst.addListSelectionListener(this); 
     mainP.add("Center", scroller); 

     Bar=new JMenuBar(); 
     M1=new JMenu("Group"); 
     I_Refresh=new JMenuItem("View"); 
     I_Refresh.addActionListener(this); 
     M1.add(I_Refresh); 

     JMenuItem jmi; 
     M1.addSeparator(); 
     M1.add(jmi=new JMenuItem("Close")); 
     jmi.addActionListener(this); 
       Bar.add(M1); 
     Bar.add(M1); 
     setJMenuBar(Bar); 
     getContentPane().add("Center", container); 



       container.add(mainP); 
       container.add(secondPanel); 

     pack(); 
     show(); 
} 

ためのコードであり、ここで私が再利用する方法私がリストから項目を選択すると、メッセージの本文が表示されます。問題は、それが新しいウィンドウを開き、私は新しいウィンドウを望んでいないということです。

Article Composerコンストラクタを変更して新しいウィンドウを開かないようにする方法がわかりません。 (私はJFrameの左部分には、メッセージのヘッダを選択して、それが新しいウィンドウを開かずにフレームの右側に読み取るための体を開き、MozillaのThunderbirdのようなGUIの何かをしたい)

public void valueChanged(ListSelectionEvent e) 
{ 
    int index=((JList)e.getSource()).getSelectedIndex(); 

    if (index != -1) 
    { 
     CurrentArticle=(NewsArticle)lst.getSelectedValue(); 
     group.setArticleRead(CurrentArticle); 
     if (CurrentArticle != null) 
     { 
      if (viewer==null) 
      { 
       ***secondPanel.add(new ArticleComposer(GroupList, group, CurrentArticle, ArticleComposer.VIEW));*** 
      } 
      else 
      { 
       viewer.setArticle(CurrentArticle); 
       if (viewer.isShowing()==false) 
        viewer.show(); 
      } 
     } 
    } 
    else 
    { 
     CurrentArticle=null; 
     if (viewer!=null) 
      viewer.clearText(); 
    } 
} 
+1

のコードは私である「推測」で、あなたは 'JFrame'かから2番目のコンポーネントを拡張しましたいずれかの新しい' JFrame'(または他のウィンドウ)を作成している – MadProgrammer

+0

Subject、Date、Bodyなどの情報が別のクラスからロードされているJPanelは、作成する...私は変更し、私はJListから要素を選択するとき(私はJListの横にあるJPanelの情報をロードする)新しいウィンドウを開くしたくない。私はJPanelを作成しましたが、新しいウィンドウを開かずにJListから要素を選択すると、情報を埋め込む方法がわかりません。 –

+0

別のトップレベルコンテナ(別の 'JFrame'など)を開くための多くの選択肢については、[複数のJFramesの使用、良い/悪い習慣]を参照してください(http://stackoverflow.com/q/9554636/418556)。 * this *のケースでどのように実装されるかについての詳細は、このアプリケーションで最も良いと思われる代替アプローチに依存します。 –

答えて

0
ここで

が2つのJPanel

public GroupViewer(NewsGroupList groupl, NewsGroup group) 
    { 
     super(group.name()); 
     addWindowListener(new WindowAdapter() { 
      public void windowClosing(WindowEvent e) 
      { 
       Close(); 
      } 
     }); 
     JMenu M1; 
     JMenuBar Bar; 

     GroupList=groupl; 
     this.group=group; 
       JPanel container = new JPanel(); 
     JPanel mainP=new JPanel(); 
       JPanel secondPanel= new JPanel(); 

       container.setLayout(new GridLayout(1,2)); 

       GridBagLayout grid=new GridBagLayout(); 
       GridBagConstraints c=new GridBagConstraints(); 

     secondPanel.setLayout(grid); 

     c.weightx=0; 
     c.weighty=0; 
     c.gridwidth=1; 
     c.anchor=GridBagConstraints.EAST; 
     c.fill=GridBagConstraints.NONE; 
     JLabel lbl=new JLabel("Subject :"); 
     grid.setConstraints(lbl, c); 
     secondPanel.add(lbl); 

     c.gridwidth=GridBagConstraints.REMAINDER; 
     c.anchor=GridBagConstraints.WEST; 
     c.fill=GridBagConstraints.HORIZONTAL; 
     Subject=new JTextField(""); 
     grid.setConstraints(Subject, c); 
     secondPanel.add(Subject); 
     if (Type==VIEW) 
      Subject.setEditable(false); 

     c.gridwidth=1; 
     c.anchor=GridBagConstraints.EAST; 
     c.fill=GridBagConstraints.NONE; 
     JLabel lbl1=new JLabel("Newsgroups :"); 
     grid.setConstraints(lbl1, c); 
     secondPanel.add(lbl1); 

     c.gridwidth=GridBagConstraints.REMAINDER; 
     c.anchor=GridBagConstraints.WEST; 
     c.fill=GridBagConstraints.HORIZONTAL; 
     if (((Type&NEWS)!=0)||((Type&VIEW)!=0)) 
      NewsGroups=new JTextField(group.name()); 
     else 
      NewsGroups=new JTextField(""); 
     if (Type==VIEW) 
      NewsGroups.setEditable(false); 
     grid.setConstraints(NewsGroups, c); 
     secondPanel.add(NewsGroups); 

     c.gridwidth=1; 
     c.anchor=GridBagConstraints.EAST; 
     c.fill=GridBagConstraints.NONE; 
     JLabel lbl2=new JLabel("To :"); 
     grid.setConstraints(lbl2, c); 
     secondPanel.add(lbl2); 

     c.gridwidth=GridBagConstraints.REMAINDER; 
     c.anchor=GridBagConstraints.WEST; 
     c.fill=GridBagConstraints.HORIZONTAL; 
     To=new JTextField(""); 
     grid.setConstraints(To, c); 
     secondPanel.add(To); 
     if (Type==VIEW) 
      To.setEditable(false); 

     if (Type==VIEW) 
     { 
      c.gridwidth=1; 
      c.anchor=GridBagConstraints.EAST; 
      c.fill=GridBagConstraints.NONE; 
      JLabel lbl3=new JLabel("Date :"); 
      grid.setConstraints(lbl3, c); 
      secondPanel.add(lbl3); 

      c.gridwidth=GridBagConstraints.REMAINDER; 
      c.anchor=GridBagConstraints.WEST; 
      c.fill=GridBagConstraints.HORIZONTAL; 
      Date=new JTextField(formatDate(ref.getDate())); 
      Date.setEditable(false); 
      grid.setConstraints(Date, c); 
      secondPanel.add(Date); 
     } 

     c.weightx=1; 
     c.weighty=1; 
     c.gridwidth=GridBagConstraints.REMAINDER; 
     c.fill=GridBagConstraints.BOTH; 
     Body=new JTextArea("", 40, 80); 
       grid.setConstraints(Body, c); 
     secondPanel.add(Body); 

       c.weightx = 1; 
       c.weighty = 1; 
       c.fill = GridBagConstraints.BOTH; 
       JScrollPane scrollPane = new JScrollPane(Body); 
       scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); 
       grid.setConstraints(scrollPane, c); 
       secondPanel.add(scrollPane); 



       if (Type==VIEW) 
      Body.setEditable(false); 

     if (((Type&REPLY)!=0)||((Type&VIEW)!=0)) 
     { 
      if ((Type&REPLY)!=0) 
      { 
       int pos=-1, pos1; 
       boolean last=false; 
       String OrigBody; 
       try 
       { 
        OrigBody=RefArticle.getBody(Group.getServer()); 
        do 
        { 
         pos1=OrigBody.indexOf('\n', pos+1); 
         if (pos1==-1) 
         { 
          last=true; 
          pos1=OrigBody.length()-1; 
         } 
        Body.append(">"+OrigBody.substring(pos+1, pos1+1)); 
        pos=pos1; 
        } 
        while (!last); 
       } 
       catch (Exception ex) 
       { 
        System.out.println("Unable to get body for this article\n"+ ex.getMessage()); 
       } 
      } 
      else 
      { 
       try 
       { 
        Body.setText(RefArticle.getBody(Group.getServer())); 
       } 
       catch (Exception ex) 
       { 
        System.out.println("Unable to get body for this article\n"+ ex.getMessage()); 
       } 
      } 
      String subject=RefArticle.getHeaderField("Subject"); 
      if (subject.toLowerCase().startsWith("re: ")||((Type&VIEW)!=0)) 
       Subject.setText(subject); 
      else 
       Subject.setText("Re: "+subject); 
      if (((Type)!=0)||((Type&VIEW)!=0)) 
       To.setText(RefArticle.getHeaderField("From")); 
     } 
     attachments=new JPanel(); 
     attachments.add("Center", new JLabel("No attachments yet .")); 
     split=new JSplitPane(JSplitPane.VERTICAL_SPLIT, true, secondPanel, attachments); 
     split.setOneTouchExpandable(true); 
     getContentPane().add(split); 

     if (Type==VIEW) 
     { 
      HViewer=new HeaderViewer(RefArticle.getHeaderField("Message-ID")); 
      HViewer.setArticle(RefArticle); 
     } 
     split.setDividerLocation(0.9); 




     listModel=new DefaultListModel(); 
     lst=new JList(listModel); 
     JScrollPane scroller=new JScrollPane(lst); 
     lst.addListSelectionListener(this); 
     mainP.add("Center", scroller); 

     Bar=new JMenuBar(); 
     M1=new JMenu("Group"); 
     I_Refresh=new JMenuItem("View"); 
     I_Refresh.addActionListener(this); 
     M1.add(I_Refresh); 

     JMenuItem jmi; 
     M1.addSeparator(); 
     M1.add(jmi=new JMenuItem("Close")); 
     jmi.addActionListener(this); 
       Bar.add(M1); 
     Bar.add(M1); 
     setJMenuBar(Bar); 
     getContentPane().add("Center", container); 



       container.add(mainP); 
       container.add(secondPanel); 

     pack(); 
     show(); 
} 
関連する問題