【发布时间】:2013-04-25 12:28:50
【问题描述】:
我有一个问题,我希望程序在同一帧中的多个面板之间切换。我遇到的问题是,当面板切换和切换内容逐像素降低后,我无法设置布局。这是我的代码。
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import javax.swing.JFrame;
public class Main {
public static boolean logged_in = false;
public static int width = 200, height = 400;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Dimension d = new Dimension(width, height);
First frame = new First();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(d);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setResizable(true);
frame.setLayout(new FlowLayout());
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
这里有两个持有框架的类。
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class First extends JFrame {
JPanel contentPane;
private JButton button_1;
private JTextField text;
public First(){
contentPane = new JPanel();
setContentPane(contentPane);
button_1 = new JButton("Second frame");
button_1.setVisible(true);
text = new JTextField(20);
text.setVisible(true);
contentPane.add(button_1);
contentPane.add(text);
thehandler handler = new thehandler();
button_1.addActionListener(handler);
}
private class thehandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (event.getSource() == button_1) {
contentPane.removeAll();
contentPane.invalidate();
Second frame = new Second();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setResizable(true);
frame.setLayout(new FlowLayout());
frame.contentPane.setVisible(true);
contentPane.add(frame.contentPane);
((JPanel) contentPane).revalidate();
contentPane.setSize(200, 400);
contentPane.repaint();
}
}
}
}
第二个
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Second extends JFrame {
JPanel contentPane;
private JButton button_1;
private JTextField text;
public Second(){
contentPane = new JPanel();
setContentPane(contentPane);
button_1 = new JButton("First frame");
button_1.setVisible(true);
text = new JTextField(20);
text.setVisible(true);
contentPane.add(button_1);
contentPane.add(text);
thehandler handler = new thehandler();
button_1.addActionListener(handler);
}
private class thehandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (event.getSource() == button_1) {
contentPane.removeAll();
contentPane.invalidate();
First frame = new First();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setResizable(true);
frame.setLayout(new FlowLayout());
frame.contentPane.setVisible(true);
contentPane.add(frame.contentPane);
((JPanel) contentPane).revalidate();
contentPane.setSize(200, 400);
contentPane.repaint();
}
}
}
}
感谢任何帮助。请记住,我对 java GUI 不太擅长。泰。
编辑经过大量时间寻找答案后,我得到了答案。它可能并不完美,但我会将其发布以供将来参考或其他人需要此解决方案。这是代码。
容纳面板的主框架:
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainFrame extends JFrame {
public static JPanel main_panel;
private static FirstFrame first;
public MainFrame(){
setLayout(null);
setSize(400, 400);
setLocationRelativeTo(null);
setResizable(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
main_panel = new JPanel();
main_panel.setBounds(0, 0, 400, 400);
add(main_panel);
main_panel.invalidate();
main_panel.removeAll();
first = new FirstFrame();
main_panel.add(first);
main_panel.revalidate();
main_panel.repaint();
}
public static void main(String[] args) {
MainFrame frame = new MainFrame();
frame.setVisible(true);
}
}
第一个面板:
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
public class FirstFrame extends JPanel {
private JButton button;
public FirstFrame() {
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(400, 400));
panel.setLayout(null);
button = new JButton("First");
button.setBounds(20, 20, 200, 40);
button.setVisible(true);
thehandler handler = new thehandler();
button.addActionListener(handler);
panel.add(button);
add(panel);
revalidate();
repaint();
}
private class thehandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (event.getSource() == button) {
MainFrame.main_panel.invalidate();
MainFrame.main_panel.removeAll();
SecondFrame frame = new SecondFrame();
MainFrame.main_panel.add(frame);
MainFrame.main_panel.revalidate();
MainFrame.main_panel.repaint();
}
}
}
}
第二:
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
public class SecondFrame extends JPanel {
private JButton button;
public SecondFrame() {
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(400, 400));
panel.setLayout(null);
button = new JButton("Second");
button.setBounds(20, 20, 200, 40);
button.setVisible(true);
thehandler handler = new thehandler();
button.addActionListener(handler);
panel.add(button);
add(panel);
revalidate();
repaint();
}
private class thehandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (event.getSource() == button) {
MainFrame.main_panel.invalidate();
MainFrame.main_panel.removeAll();
ThirdFrame frame = new ThirdFrame();
MainFrame.main_panel.add(frame);
MainFrame.main_panel.revalidate();
MainFrame.main_panel.repaint();
}
}
}
}
第三个:
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
public class ThirdFrame extends JPanel {
private JButton button;
public ThirdFrame() {
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(400, 400));
panel.setLayout(null);
button = new JButton("Third");
button.setBounds(20, 20, 200, 40);
button.setVisible(true);
thehandler handler = new thehandler();
button.addActionListener(handler);
panel.add(button);
add(panel);
revalidate();
repaint();
}
private class thehandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (event.getSource() == button) {
MainFrame.main_panel.invalidate();
MainFrame.main_panel.removeAll();
FirstFrame frame = new FirstFrame();
MainFrame.main_panel.add(frame);
MainFrame.main_panel.revalidate();
MainFrame.main_panel.repaint();
}
}
}
}
如您所见,您可以从面板 1 ->2 ->3 切换回 1,但不能切换到第二个面板。 Ty all 对于您的回答,他们很有帮助。欢迎任何进一步的建议。
【问题讨论】:
-
+1 很好的问题 1. CardLayout 存在的原因,2. 以及其他一些(重要的也许不是,也许......)错误
-
你为什么不切换内容窗格而不是
revalidate() -
为您的内容窗格尝试
CardLayout。 -
对于每个操作,您在内容窗格中放置另一个框架(然后在内容窗格中的框架中......),即层次结构越来越深。
-
@Howard 所以这就是它每次都降低的原因?如果是这样,任何人都可以尝试修复代码以使其正常工作。泰
标签: java swing layout frames multiple-instances