调整框架(窗口)大小后组件消失
Posted
技术标签:
【中文标题】调整框架(窗口)大小后组件消失【英文标题】:Components disappear after resizing the frame(window) 【发布时间】:2019-06-27 00:44:53 【问题描述】:当我在主页或 page2 上时,一切看起来都不错,但是当我点击 page3 并调整框架(窗口)的大小时,page3 的内容将消失,并会在鼠标悬停时出现。
之后,如果我点击 page2 或主页,它们的内容也不会出现。
我正在用 java 制作一个测验应用程序。
package quizPro;
import javax.swing.*;
public class mainclass
public static void main(String[] args)
SwingUtilities.invokeLater(new Runnable()
@Override
public void run()
try
new mainclass();
catch (Exception e)
e.printStackTrace();
);
public mainclass()
new MLayout();
package quizPro;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.sun.prism.Graphics;
import net.miginfocom.swing.MigLayout;
public class MLayout
JFrame frame = new JFrame("Employees");
JPanel panel = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JButton btnHome = new JButton("Home");
JButton btnPage2 = new JButton("Page 2");
JLabel lblDsc = new JLabel("Description : ");
JTextArea txaDsc = new JTextArea(10,70);
//JTextArea txaDsc2 = new JTextArea(10,50);
Page2 page3 = new Page2();
public MLayout()
frame.setVisible(true);
panel.setLayout(new MigLayout());
panel2.setLayout(new MigLayout());
panel3.setLayout(new MigLayout());
panel.add(btnHome, "skip, split2");
btnHome.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent arg0)
frame.add(panel2);
frame.remove(panel3);
page3.remove();
btnPage2.setEnabled(true);
panel2.revalidate();
panel2.repaint();
);
panel.add(btnPage2, "wrap");
btnPage2.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent arg0)
frame.remove(panel2);
page3.thirdpage(frame, panel3);
frame.add(panel3);
page3.remove();
panel3.revalidate();
panel3.repaint();
);
panel2.add(lblDsc, "top");
panel2.add(new JScrollPane(txaDsc), "push, grow");
//panel3.add(new JScrollPane(page3.thirdpage(panel3)), "push, grow");
frame.add(panel, BorderLayout.NORTH);
frame.add(panel2);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setVisible(true);
package quizPro;
import java.awt.Component;
import java.awt.event.*;
import javax.swing.*;
import net.miginfocom.swing.MigLayout;
public class Page2
JButton btnPage3 = new JButton("Page 3");
JPanel panel4 = new JPanel();
Page3 medium = new Page3();
public Component thirdpage(JFrame frame, JPanel panel3)
panel4.setLayout(new MigLayout());
panel3.add(btnPage3, "wrap");
btnPage3.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent arg0)
panel3.removeAll();
medium.mediumquiz(panel4);
frame.add(panel4);
panel4.revalidate();
panel4.repaint();
);
return null;
public void remove()
panel4.removeAll();
package quizPro;
import java.awt.Component;
import javax.swing.*;
public class Page3
JButton newbtn12 = new JButton("Now resize the frame or window");
public Component mediumquiz(JPanel panel4)
panel4.add(newbtn12, "wrap");
return null;
【问题讨论】:
【参考方案1】:所以,问题出在 btnPage3。因为您正在创建一个新面板(panel4)并将其添加到 panel3 后面。
你可以这样解决:
btnPage3.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent arg0)
medium.mediumquiz(panel4);
frame.add(panel4);
frame.remove(panel3);
frame.revalidate();
);
或者你可以再次使用 panel3,像这样:
panel3.removeAll();
panel3.add(btnPage3, "wrap");
btnPage3.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent arg0)
panel3.removeAll();
medium.mediumquiz(panel3);
panel3.revalidate();
panel3.repaint();
);
【讨论】:
以上是关于调整框架(窗口)大小后组件消失的主要内容,如果未能解决你的问题,请参考以下文章