我有一个代码:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
class ReadPanel extends JPanel{
private static final long serialVersionUID = 1L;
private JTextArea textArea;
public ReadPanel(){
super();
setBackground(new Color(255, 182, 193));
JScrollPane scrollPane = new JScrollPane();
GroupLayout gl_panel_main = new GroupLayout(this);
gl_panel_main.setHorizontalGroup(
gl_panel_main.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel_main.createSequentialGroup()
.addContainerGap()
.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 402, Short.MAX_VALUE)
.addContainerGap())
);
gl_panel_main.setVerticalGroup(
gl_panel_main.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel_main.createSequentialGroup()
.addContainerGap()
.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
.addContainerGap())
);
textArea = new JTextArea();
scrollPane.setViewportView(textArea);
setLayout(gl_panel_main);
}
public void setNews(String news){
textArea.setText(news);
}
public String getNews(){
return textArea.getText();
}
//another complex method here...
}
public class SetContentPanelFrame extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private ReadPanel panel_main;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SetContentPanelFrame frame = new SetContentPanelFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public void goFullReadMode(){
/*ReadPanel copy = new ReadPanel();
copy.setNews(panel_main.getNews());
copy.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
exitFullReadMode();
}
});
//Set another property here...
this.setContentPane(copy);*/
this.setContentPane(panel_main);
this.repaint();
this.revalidate();
}
public void exitFullReadMode(){
this.setContentPane(contentPane);
//contentPane.add(panel_main);
this.repaint();
this.revalidate();
}
/**
* Create the frame.
*/
public SetContentPanelFrame() {
setTitle("Set Content Frame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 741, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JPanel panel_menu = new JPanel();
panel_menu.setBackground(new Color(240, 248, 255));
JPanel panel_ads = new JPanel();
panel_ads.setBackground(new Color(255, 239, 213));
panel_main = new ReadPanel();
panel_main.setNews("This is a news\r\n1\r\n\r\n\r\n\r\n\r\n\r\n\r\n"
+ "This is a news\r\n2\r\n\r\n\r\n\r\n"
+ "This is a news\r\n3\r\n\r\n\r\n"
+ "This is a news\r\n4");
panel_main.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
exitFullReadMode();
}
});
JButton btnViewFull = new JButton("View full");
btnViewFull.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
goFullReadMode();
}
});
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(panel_menu, GroupLayout.PREFERRED_SIZE, 140, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(btnViewFull)
.addContainerGap())
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(panel_main, GroupLayout.DEFAULT_SIZE, 422, Short.MAX_VALUE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(panel_ads, GroupLayout.PREFERRED_SIZE, 141, GroupLayout.PREFERRED_SIZE))))
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(btnViewFull)
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(panel_main, GroupLayout.DEFAULT_SIZE, 222, Short.MAX_VALUE)
.addComponent(panel_ads, GroupLayout.DEFAULT_SIZE, 222, Short.MAX_VALUE)
.addComponent(panel_menu, GroupLayout.DEFAULT_SIZE, 222, Short.MAX_VALUE)))
);
panel_ads.setLayout(new BorderLayout(0, 0));
JLabel lblAds = new JLabel("Ads...");
lblAds.setHorizontalAlignment(SwingConstants.CENTER);
panel_ads.add(lblAds, BorderLayout.CENTER);
panel_menu.setLayout(new BorderLayout(0, 0));
JLabel lblMenu = new JLabel("Menu");
lblMenu.setHorizontalAlignment(SwingConstants.CENTER);
panel_menu.add(lblMenu, BorderLayout.CENTER);
contentPane.setLayout(gl_contentPane);
}
}
我想使用setContentPane方法在两个JPanel之间切换(contentPane和panel_main)。
有2个重要的方法:goFullReadMode()和exitFullReadMode()。
当我运行该程序时,goFullReadMode()可以正常运行,但是当切换回默认面板(单击文本外部的粉红色区域)时,中间面板(panel_main)消失了。为什么它不再显示?
我认为它已经移到某个地方,因此在exitFullReadMode方法中,我尝试:
contentPane.add(panel_main);
但是它不会显示在正确的位置,因为容器具有GroupLayout,并且重新组合所有组件不是一个好主意。
后来的解决方案是将panel_main和setContentPane复制到此副本,但是我们需要将所有内容(数据,子组件,...)复制到此复制对象(如goFullReadMode方法中的注释)。因此,这并非易事。
有什么解决方案不需要复制面板并在我们切换回默认视图时仍保留该面板吗?
谢谢。
最佳答案
由于在调用panel_main
时仅使用了对this.setContentPane(panel_main2);
的引用,因此它将自身从contentPane
面板中删除,并将其自身添加为Frame
作为其主要Panel
,因此您不能退出全屏模式后,可在contentPane
面板中看到它。
解:
由于您无法将其调整为其状态,因此可以创建2个ReadPanel
实例,一个实例用于contentPane
,另一个实例用于全屏
panel_main = new ReadPanel();
panel_main2 = new ReadPanel();
panel_main.setNews("This is a news\r\n1\r\n\r\n\r\n\r\n\r\n\r\n\r\n"
+ "This is a news\r\n2\r\n\r\n\r\n\r\n"
+ "This is a news\r\n3\r\n\r\n\r\n"
+ "This is a news\r\n4");
panel_main2.setNews("This is a news\r\n1\r\n\r\n\r\n\r\n\r\n\r\n\r\n"
+ "This is a news\r\n2\r\n\r\n\r\n\r\n"
+ "This is a news\r\n3\r\n\r\n\r\n"
+ "This is a news\r\n4");
panel_main2.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
exitFullReadMode();
}
全屏播放
public void goFullReadMode(){
this.setContentPane(panel_main2);
this.repaint();
this.revalidate();
}
现在,当您更新
panel_main
的文本时,还必须更新panel_main2
。