Java JPanels在同一窗口中加载信息
我在Java JPanels在同一窗口中加载信息,java,swing,jframe,Java,Swing,Jframe,我在JFrame中有两个JPanel组件。第一个面板(firstP)包含一个从新闻服务器加载消息头的JList。在第二个面板(secondP)中,我想加载消息体 当我从列表中选择一个项目时,它会打开一个新闻面板,其中加载了消息的主体(这就是它现在的工作方式),但我不喜欢这样 从firstP(在同一帧中)选择标题时,我希望加载secondP中的body if消息 它现在是如何工作的: 我希望如何工作: 这是两个面板的代码 public GroupViewer(NewsGroupList gro
JFrame
中有两个JPanel
组件。第一个面板(firstP
)包含一个从新闻服务器加载消息头的JList
。在第二个面板(secondP
)中,我想加载消息体
当我从列表中选择一个项目时,它会打开一个新闻面板,其中加载了消息的主体(这就是它现在的工作方式),但我不喜欢这样
从firstP
(在同一帧中)选择标题时,我希望加载secondP
中的body if消息
它现在是如何工作的:
我希望如何工作:
这是两个面板的代码
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的G.U.I,在JFrame
的左侧,选择消息的标题,它会在框架的右侧打开正文进行阅读,而无需打开新窗口)
下面是两个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();
}
下面是两个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();
}
我的“猜测”是,您要么是从一个
JFrame
扩展了第二个组件,要么是创建了一个新的JFrame
(或其他窗口)打开的JPanel,其中包含主题、日期、正文等信息,从单独的类加载,我调用构造函数来创建……然后我修改了,当我从JList中选择一个元素时,我不想打开一个新窗口(我想在JList旁边的JPanel中加载信息)。我已经创建了JPanel,但我不知道如何在不打开新窗口的情况下从JList中选择一个元素时用信息填充它。有关打开另一个顶级容器(例如另一个JFrame
)的许多替代方法,请参阅。在这种情况下如何实现的细节取决于您认为最适合此应用程序的替代方法。pack()
My spider sense(和常识)告诉我此代码位于扩展顶级容器(如JFrame
)的类中。这可能是由您的IDE“提示”的,但这是一种错误的方式……”它在框架的右侧打开主体以供阅读,而无需打开新窗口)“看,我可以使用。。一个Font
列表,在选中时显示RHS的详细信息,但这里的核心问题是,代码是使用功能强大的IDE生成的,这会产生不好的建议(例如,扩展框架或面板以使用D'n'D GUI设计器)。一旦你准备放弃它,我们就可以使用..Java代码来解决这个问题。我的“猜测”是,你要么是从JFrame
扩展了第二个组件,要么是创建了一个新的JFrame
(或其他窗口)打开的JPanel,其中包含主题、日期、主体等信息,从单独的类加载,我调用构造函数来创建……然后我修改了,当我从JList中选择一个元素时,我不想打开一个新窗口(我想在JList旁边的JPanel中加载信息)。我已经创建了JPanel,但我不知道如何在不打开新窗口的情况下从JList中选择一个元素时用信息填充它。有关打开另一个顶级容器(例如另一个JFrame
)的许多替代方法,请参阅。在这种情况下如何实现的细节取决于您认为最适合此应用程序的替代方法。pack()
My spider sense(和常识)告诉我此代码位于扩展顶级容器(如JFrame
)的类中。这可能是由您的IDE“提示”的,但这是一种错误的方式……”它在框架的右侧打开主体以供阅读,而无需打开新窗口)“看,我可以使用。。一个Font
列表,在选中时显示RHS的详细信息,但这里的核心问题是,代码是使用功能强大的IDE生成的,这会产生不好的建议(例如,扩展框架或面板以使用D'n'D GUI设计器)。一旦您准备放弃它,我们就可以使用..Java代码找到解决方案。