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



我还在学习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");

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

相关内容

  • 没有找到相关文章

最新更新