Java 如何将代码整理成类文件和方法

Java 如何将代码整理成类文件和方法,java,swing,user-interface,Java,Swing,User Interface,我仍在学习Java,目前正在Swing中创建一个程序。对于何时何地应该使用方法和类文件,我一直感到困惑。我创建了一个应用程序,它有两张卡,卡1:homeJPanel和卡2:guestFixturesJPanel,我希望这些卡在单击按钮时可以相互切换,我已经在一定程度上做到了这一点。然而,我的代码看起来非常混乱,很难查看,因为所有JPanel都在一个方法中。我想知道是否有任何方法可以将guestFixturesJPanel放入一个单独的方法或类文件中,并且仍然能够在单击按钮时调用卡。这可能吗?还有

我仍在学习Java,目前正在Swing中创建一个程序。对于何时何地应该使用方法和类文件,我一直感到困惑。我创建了一个应用程序,它有两张卡,卡1:
homeJPanel
和卡2:
guestFixturesJPanel
,我希望这些卡在单击按钮时可以相互切换,我已经在一定程度上做到了这一点。然而,我的代码看起来非常混乱,很难查看,因为所有JPanel都在一个方法中。我想知道是否有任何方法可以将
guestFixturesJPanel
放入一个单独的方法或类文件中,并且仍然能够在单击按钮时调用卡。这可能吗?还有,有谁知道有什么好的教程可以解释方法和类文件吗?我一直很困惑,这可能是解决我问题的方法

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import javax.xml.crypto.dsig.spec.C14NMethodParameterSpec;   

public class Main 
{
    protected static final Component c1 = null;
    private JButton viewFixturesButton, loginButton, guestBackButton;
    private JLabel testTextJLabel, testTextJLabel2;
    JPanel container = new JPanel();
    CardLayout cardLayout = new CardLayout();

    public Main()
    {
        final JFrame window = new JFrame ("Main Game");

        final CardLayout c1 = new CardLayout();
        final JPanel container = new JPanel(c1);
        JPanel homeJPanel = new JPanel(new BorderLayout());
        container.add(homeJPanel);

        JPanel centerJPanel = new JPanel(new BorderLayout());
        testTextJLabel = new JLabel("TEST");
        centerJPanel.add(testTextJLabel);

        JPanel southPanel = new JPanel(new FlowLayout());
        viewFixturesButton = new JButton("View Fixtures");
        loginButton = new JButton("Login");
        southPanel.add(viewFixturesButton);
        southPanel.add(loginButton);

        homeJPanel.add(centerJPanel, BorderLayout.CENTER);
        homeJPanel.add(southPanel, BorderLayout.SOUTH);

        centerJPanel.setBackground(Color.BLUE);
        southPanel.setBackground(Color.GREEN);

        JPanel guestFixturesJPanel = new JPanel(new BorderLayout());
        container.add(guestFixturesJPanel);

        JPanel guestCenterJPanel = new JPanel(new BorderLayout());

        JPanel guestSouthPanel = new JPanel(new FlowLayout());
        guestBackButton = new JButton("Back");
        guestSouthPanel.add(guestBackButton);

        guestFixturesJPanel.add(guestCenterJPanel, BorderLayout.CENTER);
        guestFixturesJPanel.add(guestSouthPanel, BorderLayout.SOUTH);

        guestCenterJPanel.setBackground(Color.BLUE);
        guestSouthPanel.setBackground(Color.GREEN);

        viewFixturesButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                c1.show(container, "2");
            }
        });

        container.add(homeJPanel, "1");
        container.add(guestFixturesJPanel, "2");
        c1.show(container, "1");

        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.getContentPane().add(container);
        window.setSize(600, 500);
        window.setLocationRelativeTo(null);
        window.setVisible(true);
        window.setResizable(false);

    }

    public static void main(String args[])
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new Main();
            }
        });
    }
}

您可以将
guestFixturesPanel
提取到自己的类中,如下所示:

public class GuestFixturesPanel extends JPanel {
    public GuestFixturesPanel() {
        this.setLayout(new BorderLayout());

        JPanel guestCenterJPanel = new JPanel(new BorderLayout());

        JPanel guestSouthPanel = new JPanel(new FlowLayout());
        JButton guestBackButton = new JButton("Back");
        guestSouthPanel.add(guestBackButton);

        add(guestCenterJPanel, BorderLayout.CENTER);
        add(guestSouthPanel, BorderLayout.SOUTH);

        guestCenterJPanel.setBackground(Color.BLUE);
        guestSouthPanel.setBackground(Color.GREEN);
    }
}
然后在
Main
类中,可以实例化
GuestFixturesPanel
并将其添加到容器中。这将保留您现在拥有的功能,并从
Main
类中提取代码

GuestFixturesPanel guestFixturesPanel = new GuestFixturesPanel();
container.add(guestFixturesPanel, "2");

不确定这是否解决了您的问题,但我希望这会有所帮助。

有人能解释一下为什么这个问题会被否决吗?简而言之,是的,您可以将您的
guestFixturesJPanel
拉到一个单独的类中。你的问题是你不知道怎么做吗?我也觉得反对票有点不公平。代码并没有那么糟糕,设置控件通常是在一大块中完成的。Netbeans也会生成这样的代码。您已经对一些行进行了分组,可以将一些行移动到新方法。但是我只想添加一些注释。是的,之前我确实尝试过通过类似这样的
GuestClass guestClassCall=new GuestClass()
,调用该类,但后来我不知道如何获取
guestFixturesJPanel
并将其添加到
容器中。add(guestFixturesJPanel,“3”)次要细节:当你说“类文件”时,我想你指的是“类”。是一个较低层次的细节,与这种代码结构讨论无关。这显然更像我需要的。我明天会测试这个,我现在没有时间。我会让你知道结果,并希望它能起作用。谢谢。很抱歉延迟回复。我拖延了一整天,然后花了几个小时试图帮助另一个人,所以-我现在明白了为什么当我的问题格式不正确时人们会如此沮丧:>。不管怎么说,代码工作得很完美,我确实像上面的代码一样尝试过,但是当我尝试时,我创建的代码与
Main
完全相同,没有
homeJPanel
。谢谢另外,通过这样做,我现在尝试在GuestFixers中创建一个返回到
homeJPanel
的按钮,但是c1给出了错误
c1无法解决
@HarryKitchener您无法从新的panel类访问c1。很难在评论中解释。。。但是您需要将对
Main
类的引用传递给子面板。在main上编写一个方法,该方法将影响
goHome()
,它将选择它作为活动面板。就像我在评论中说的,很难解释,但如果你仍然不清楚,请发布一个新问题,我会看一看。你有没有可能在这里发布?