当我在主页或第2页每东西看起来不错,但是当我点击第三页和调整框架(窗口)的第3页内容会消失,会随着鼠标出现。
之后,如果我点击了第2页或主页的内容没有出现过。
我正在用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;
}
}
所以,问题是在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();
}
});