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

时间:2014-05-03 22:28:12

标签: java swing user-interface

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

1 个答案:

答案 0 :(得分:1)

你可以像这样将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");

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