如何在 JPanel 中的操作中交换 JPanel。

huangapple go评论81阅读模式
英文:

How to swap JPanel's from an action in a JPanel

问题

// MainFrame.java
public class MainFrame extends JFrame {

    public MainFrame(){
        setTitle("Swing Application");
        setSize(1200, 800);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setVisible(true);

        // 初始页面切换
        getContentPane().add(new FirstPage());
    }
}

// FirstPage.java
public class FirstPage extends JPanel {

    public FirstPage() {
        setVisible(true);
        JButton clickBtn = new JButton("点击");
        clickBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                // 在这里切换到 SecondPage JPanel。
            }
        });
        add(clickBtn);
    }
}

// SecondPage.java
public class SecondPage extends JPanel {

    public SecondPage() {
        setVisible(true);
        add(new JLabel("欢迎来到第二页"));
    }
}

如需更多信息,请提问,谢谢。

英文:

I am new(ish) to Java Swing but I have not been able to find an elegant solution to my issue so I thought I'd raise a question here.

I am trying to make my current JPanel change to another JPanel based on a button click event from within the current JPanel. In essence just hiding one panel and displaying the other. I feel this can be done within my MainFrame class however I'm not sure how to communicate this back to it. Nothing I am trying simply seems to do as desired, I'd appreciate any support. Thanks

App.java

public static void main(final String[] args) {
    MainFrame mf = new MainFrame();
}

MainFrame.java

public class MainFrame extends JFrame {

    public MainFrame(){
        setTitle("Swing Application");
        setSize(1200, 800);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setVisible(true);

        // First Page Frame switch
        getContentPane().add(new FirstPage());

    }
}

FirstPage.java

public class FirstPage extends JPanel {

    public FirstPage() {
        setVisible(true);
        JButton clickBtn = new JButton("Click");
        clickBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                // Change to SecondPage JPanel here.
            }
        });
        add(clickBtn);
    }
}

SecondPage.java

public class SecondPage extends JPanel {

    public SecondPage() {
        setVisible(true);
        add(new JLabel("Welcome to the Second Page"));

    }
}

Any more information needed, please ask thanks 如何在 JPanel 中的操作中交换 JPanel。

答案1

得分: 4

以下是您提供的代码的翻译部分:

我认为最好的方法是使用 [CardLayout][1]它是专为这种情况而创建的请查看我的示例

    public class MainFrame extends JFrame {
    	private CardLayout cardLayout;
    
    	public MainFrame() {
    		super("frame");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    		cardLayout = new CardLayout();
    		getContentPane().setLayout(cardLayout);
    
    		getContentPane().add(new FirstPage(this::showPage), Pages.FIRST_PAGE);
    		getContentPane().add(new SecondPage(this::showPage), Pages.SECOND_PAGE);
    
    		setLocationByPlatform(true);
    		pack();
    	}
    
    	public void showPage(String pageName) {
    		cardLayout.show(getContentPane(), pageName);
    	}
    
    	public static interface PageContainer {
    		void showPage(String pageName);
    	}
    
    	public static interface Pages {
    		String FIRST_PAGE = "first_page";
    		String SECOND_PAGE = "second_page";
    	}
    
    	public static class FirstPage extends JPanel {
    		public FirstPage(PageContainer pageContainer) {
    			super(new FlowLayout());
    
    			JButton button = new JButton("下一页");
    			button.addActionListener(e -> pageContainer.showPage(Pages.SECOND_PAGE));
    			add(button);
    		}
    	}
    
    	public static class SecondPage extends JPanel {
    		public SecondPage(PageContainer pageContainer) {
    			super(new FlowLayout());
    
    			add(new JLabel("这是第二页。"));
    
    			JButton button = new JButton("回到第一页");
    			button.addActionListener(e -> pageContainer.showPage(Pages.FIRST_PAGE));
    			add(button);
    		}
    	}
    
    	public static void main(String[] args) {
    		SwingUtilities.invokeLater(() -> new MainFrame().setVisible(true));
    	}
    }

  [1]: https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html
英文:

I think the best way is to use CardLayout. It is created for such cases. Check my example:

public class MainFrame extends JFrame {
private CardLayout cardLayout;
public MainFrame() {
super("frame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cardLayout = new CardLayout();
getContentPane().setLayout(cardLayout);
getContentPane().add(new FirstPage(this::showPage), Pages.FIRST_PAGE);
getContentPane().add(new SecondPage(this::showPage), Pages.SECOND_PAGE);
setLocationByPlatform(true);
pack();
}
public void showPage(String pageName) {
cardLayout.show(getContentPane(), pageName);
}
public static interface PageContainer {
void showPage(String pageName);
}
public static interface Pages {
String FIRST_PAGE = "first_page";
String SECOND_PAGE = "second_page";
}
public static class FirstPage extends JPanel {
public FirstPage(PageContainer pageContainer) {
super(new FlowLayout());
JButton button = new JButton("next Page");
button.addActionListener(e -> pageContainer.showPage(Pages.SECOND_PAGE));
add(button);
}
}
public static class SecondPage extends JPanel {
public SecondPage(PageContainer pageContainer) {
super(new FlowLayout());
add(new JLabel("This is second page."));
JButton button = new JButton("Go to first page");
button.addActionListener(e -> pageContainer.showPage(Pages.FIRST_PAGE));
add(button);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new MainFrame().setVisible(true));
}
}

答案2

得分: 3

CardLayout是完成任务的正确工具。
您可以简单地创建用于在JFrame类中交换页面的ActionListener,并将其引用传递给FirstPage

import java.awt.CardLayout;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MainFrame extends JFrame {

    public MainFrame(){
        setTitle("Swing Application");
        setSize(1200, 800);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLocationByPlatform(true);

        //创建卡片布局并将其设置为内容面板
        CardLayout cLayout = new  CardLayout();
        setLayout(cLayout);

        //创建并将第二个页面添加到内容面板
        JPanel secondPage = new SecondPage();
        add("SECOND",secondPage);

        //创建用于交换页面的动作侦听器
        ActionListener listener = actionEvent -> {
            cLayout.show(getContentPane(), "SECOND");
        };

        //在FirstPage中使用动作侦听器
        JPanel firstPage = new FirstPage(listener);
        add("FIRST", firstPage);
        cLayout.show(getContentPane(), "FIRST");
        setVisible(true);
    }

    public static void main(String[] args) {
        new MainFrame();
    }
}

class FirstPage extends JPanel {

    public FirstPage(ActionListener listener) {
        JButton clickBtn = new JButton("Click");
        clickBtn.addActionListener(listener);
        add(clickBtn);
    }
}

class SecondPage extends JPanel {
    public SecondPage() {
        add(new JLabel("Welcome to the Second Page"));
    }
}

请注意,由于您要求只返回翻译好的代码部分,因此我只提供了代码的翻译部分,不包含任何其他内容。如果您有其他问题或需要进一步的帮助,请随时提问。

英文:

CardLayout is the right tool for the job.
You can simply create the ActionListener used to swap pages in JFrame class, and pass a reference of it to FirstPage:

import java.awt.CardLayout;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class MainFrame extends JFrame {
public MainFrame(){
setTitle("Swing Application");
setSize(1200, 800);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setLocationByPlatform(true);
//Create card layout and set it to the content pane 
CardLayout cLayout = new  CardLayout();
setLayout(cLayout);
//create and add second page to the content pane 
JPanel secondPage = new SecondPage();
add("SECOND",secondPage);
//create an action listener to swap pages 
ActionListener listener = actionEvent ->{
cLayout.show(getContentPane(), "SECOND");
};
//use the action listener in FirstPage
JPanel firstPage = new FirstPage(listener);
add("FIRST", firstPage);
cLayout.show(getContentPane(), "FIRST");
setVisible(true);
}
public static void main(String[] args) {
new MainFrame();
}
}
class FirstPage extends JPanel {
public FirstPage(ActionListener listener) {
JButton clickBtn = new JButton("Click");
clickBtn.addActionListener(listener);
add(clickBtn);
}
}
class SecondPage extends JPanel {
public SecondPage() {
add(new JLabel("Welcome to the Second Page"));
}
}

huangapple
  • 本文由 发表于 2020年4月9日 19:16:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/61119914.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定