对于我的功课,我要创建一个JTabbedPane并添加两个Jpanels。我感觉自己非常接近,但是仍然无法编译。当我运行它时,两个JPanels都打开,但是JTabbedPane没有打开。我收到很多未知源错误。我怀疑这时我在JPanels本身中遇到了问题,因为它们最初是JFrames,并且我尝试(我认为未成功)将JFrames转换为JPanels。

JTabbedPaneAssignment应该创建JTabbedPane,并在一个选项卡上使用DayGui应用程序,在另一个选项卡上使用OfficeAreaCalculator填充两个窗格。我在这里仅包括JTabbedPaneAssignment和DayGui类。如果它的代码太多,我对此表示歉意,我删节了很多我认为无关紧要的内容,但可能仍然太多。

这是调用类JTabbedPaneAssignment

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class JTabbedPaneAssignment extends JPanel
{
    public JTabbedPaneAssignment()
    {
        //super(new GridLayout(1, 1));

        JTabbedPane tabbedPane = new JTabbedPane();
        DayGui pnlDay = new DayGui();
        OfficeAreaCalculator  pnlOffice = new OfficeAreaCalculator ();
        this.add(tabbedPane);

        JPanel panel1 = new JPanel();
        JPanel panel2 = new JPanel();
        tabbedPane.add(panel1,"First panel");
        tabbedPane.add(panel2,"Second panel");


        //JComponent panel1 = makeTextPanel("Pane #1");
        panel1.setPreferredSize(new Dimension(300, 150));
        tabbedPane.addTab("DayGui", panel1);
        tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);

        //JComponent panel2 = makeTextPanel("Pane #2");
        panel2.setPreferredSize(new Dimension(410, 50));
        tabbedPane.addTab("OfficeAreaCalculator", panel2);
        tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);
        add(tabbedPane);
    }

    protected JComponent makeTextPanel(String text)
    {
        JPanel panel = new JPanel(false);
        JLabel filler = new JLabel(text);
        filler.setHorizontalAlignment(JLabel.CENTER);
        panel.setLayout(new GridLayout(1, 1));
        panel.add(filler);
        return panel;
    }

    // Create JTabbedPane
    private static void createAndShowGUI()
    {
        // Create and set up the window.
        JFrame frame = new JFrame("JTabbedPane");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new JTabbedPaneAssignment(), BorderLayout.CENTER);
        JTabbedPane DayGui = new JTabbedPane();
        JTabbedPane OfficeAreaCalculator = new JTabbedPane();
        frame.pack();
        frame.setVisible(true);
    }


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


这是DayGui课。它最初是作为JFrame开始的,但我正在尝试将其转换为JPanel。我认为我的问题在这段代码中,但我不知道

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

//public class DayGui extends JPanel
public class DayGui extends JPanel
{
    private JPanel dayPanel;
    private JButton cmdGood;
    private JButton cmdBad;

    public DayGui()
    {
        //dayPanel = new JPanel("Messages");

        cmdGood = new JButton("Good");
        cmdBad = new JButton("Bad");

        Container c = dayPanel.getRootPane();
        c.setLayout(new FlowLayout());

        c.add(cmdGood);
        c.add(cmdBad);

        dayPanel.setSize(300, 150);

        ButtonsHandler bhandler = new ButtonsHandler();
        cmdGood.addActionListener(bhandler);
        cmdBad.addActionListener(bhandler);

        dayPanel.setVisible(true);
        dayPanel.getRootPane().setBackground(Color.CYAN);

    }

    class ButtonsHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            if (e.getSource() == cmdGood)
                JOptionPane.showMessageDialog(null, "Today is a good day!",
                        "Event Handler Message",
                        JOptionPane.INFORMATION_MESSAGE);
            if (e.getSource() == cmdBad)
                JOptionPane.showMessageDialog(null, "Today is a bad day!",
                        "Event Handler Message",
                        JOptionPane.INFORMATION_MESSAGE);
        }
    }

}

最佳答案

好的,这是第二次了。现在您已经建立了DayGui类,我们注意到您正在尝试使用dayPanel进行初始化。即您无处说dayPanel = new JPanel();。这导致在您的代码中抛出NullPointerException,从而破坏了正常的执行过程。解决此问题,您的DayGui类将在运行时正常运行。

在您的代码库中,您还会做一些非常时髦的事情,即开始使用某些东西,但从不动手完成它。例如,您扩展JPanel,但要使用类型为JPanel的成员变量来完成其余工作。建议将所有实例变量全部废弃,并使用从扩展JPanel继承的方法。

现在,让我们进入您的JTabbedPaneAssignment类。尽管代码并不可怕,但它的代码可读性不高,并且会在许多地方重复(多次添加标签面板)。如果要将此代码进一步带入更大的项目,维护和调试将很快成为问题。

为了向您显示代码中的混乱程度,我快速重写了提供的代码以使其可编译,并且应该大致按照您的工作进行。

JTabbedPaneAssignment

import java.awt.BorderLayout;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;



public class JTabbedPaneAssignment extends JPanel
{
    public JTabbedPaneAssignment()
    {
        final JTabbedPane tabbedPane = new JTabbedPane();
        final DayPanel dayPanel = new DayPanel();
        final JPanel officePanel = new JPanel();

        tabbedPane.add("DayGui", dayPanel);
        tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);

        tabbedPane.add("OfficeAreaCalculator", officePanel);
        tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);

        add(tabbedPane);
    }



    // Create JTabbedPane
    private static void createAndShowGUI()
    {
        // Create and set up the window.
        JFrame frame = new JFrame("JTabbedPane");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new JTabbedPaneAssignment(), BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
    }


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


DayGui - renamed as DayPanel

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

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class DayPanel extends JPanel {

    public DayPanel() {
        super();
        setBackground(Color.CYAN);

        final JButton goodButton = new JButton("Good");
        final JButton badButton = new JButton("Bad");

        ActionListener listener = new DayPanel.ButtonsHandler();
        goodButton.addActionListener(listener);
        badButton.addActionListener(listener);

        add(goodButton);
        add(badButton);
    }

    class ButtonsHandler implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {

            final String command = ((JButton) e.getSource()).getActionCommand();

            if (command.equals("Good")) {
                showMessage("Today is a good day!");
            } else if (command.equals("Bad")) {
                showMessage("Today is a bad day!");
            }
        }

        // Show popup message
        private void showMessage(String message) {
            JOptionPane.showMessageDialog(null, message,
                    "Event Handler Message", JOptionPane.INFORMATION_MESSAGE);
        }
    }

}


那么这些有什么不同呢?基本上,我已经删除了您进入NullPointerExceptionDayGui。除了您尝试使代码混乱之外,我所做的任何其他更改都是为了让您真正了解代码中正在发生的事情。

希望这可以帮助。

09-26 12:07