JPanels在同一窗口中加载信息

时间:2016-04-04 10:33:22

标签: java swing jframe

我在JPanel中有两个JFrame个组件。第一个面板(firstP)包含一个JList,它从新闻服务器加载邮件头。在第二个面板(secondP)中,我想加载消息的正文。

当我从列表中选择一个项目时,它会打开一个新闻面板,其中加载了消息正文(这是它现在的工作方式),但我不喜欢这样。

我想在secondP中选择firstP中的标题(在同一帧中)时加载正文消息。

现在如何运作:

我想如何工作:

以下是两个面板的代码

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构造函数以不打开新窗口。 (我想要一个类似于Mozilla Thunderbird的GUI,在JFrame的左侧部分选择消息的标题,它打开正文以便在框架的右侧读取而不打开新窗口

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 个答案:

答案 0 :(得分:0)

以下是两个JPanels的代码

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();
}