将一个 JPanel class 放入另一个 class 的 JFrame 中
Put a JPanel class into a JFrame of another class
我必须插入另一个 class 的 JFrame 和 JPanel。
我在 JFrame 中有一个 jMenuItem,我希望当我单击 JMenuItem 时,JPanel 将出现。
private void searchStudMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
searchStud s = new searchStud();
s.setVisible(true);
changePanel(s);
}
private void changePanel(JPanel panel) {
getContentPane().removeAll();
getContentPane().add(panel);
getContentPane().doLayout();
update(getGraphics());
}
searchStud 是包含 JPanel 的 class。
当我执行程序并单击 JMenuItem 时,没有任何反应......
我尝试在线搜索,但我发现没有用。
实际组件 - 我想是 JFrame - 已更改,必须重新验证:
private void changePanel(JPanel panel) {
getContentPane().removeAll();
getContentPane().add(panel);
revalidate();
}
刚刚用这个最少的代码进行了测试:
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
@SuppressWarnings("serial")
public class Test extends JFrame {
public static void main(String[] args) {
new Test();
}
private static class searchStud extends JPanel {
searchStud() {
add(new JLabel("SEARCH STUD"));
}
}
private Test() {
SwingUtilities.invokeLater(this::initGUI);
}
private void initGUI() {
JButton button = new JButton("Search");
button.addActionListener(this::searchStudMenuItemActionPerformed);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
add(button);
setSize(300, 200);
validate();
setLocationRelativeTo(null);
setVisible(true);
}
private void searchStudMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
searchStud s = new searchStud();
s.setVisible(true);
changePanel(s);
}
private void changePanel(JPanel panel) {
getContentPane().removeAll();
getContentPane().add(panel);
revalidate();
}
}
我必须插入另一个 class 的 JFrame 和 JPanel。 我在 JFrame 中有一个 jMenuItem,我希望当我单击 JMenuItem 时,JPanel 将出现。
private void searchStudMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
searchStud s = new searchStud();
s.setVisible(true);
changePanel(s);
}
private void changePanel(JPanel panel) {
getContentPane().removeAll();
getContentPane().add(panel);
getContentPane().doLayout();
update(getGraphics());
}
searchStud 是包含 JPanel 的 class。 当我执行程序并单击 JMenuItem 时,没有任何反应...... 我尝试在线搜索,但我发现没有用。
实际组件 - 我想是 JFrame - 已更改,必须重新验证:
private void changePanel(JPanel panel) {
getContentPane().removeAll();
getContentPane().add(panel);
revalidate();
}
刚刚用这个最少的代码进行了测试:
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
@SuppressWarnings("serial")
public class Test extends JFrame {
public static void main(String[] args) {
new Test();
}
private static class searchStud extends JPanel {
searchStud() {
add(new JLabel("SEARCH STUD"));
}
}
private Test() {
SwingUtilities.invokeLater(this::initGUI);
}
private void initGUI() {
JButton button = new JButton("Search");
button.addActionListener(this::searchStudMenuItemActionPerformed);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
add(button);
setSize(300, 200);
validate();
setLocationRelativeTo(null);
setVisible(true);
}
private void searchStudMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
searchStud s = new searchStud();
s.setVisible(true);
changePanel(s);
}
private void changePanel(JPanel panel) {
getContentPane().removeAll();
getContentPane().add(panel);
revalidate();
}
}