当我在主页或第2页每东西看起来不错,但是当我点击第三页和调整框架(窗口)的第3页内容会消失,将鼠标移到出现。
在那之后,如果我单击到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;
}
}
答案 0 :(得分:0)
因此,问题出在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();
}
});