(Java)为什么此选项卡式窗格无法正常工作?

时间:2013-06-06 23:30:55

标签: java

注意:我在网上(本网站和其他网站)进行了搜索,但我自己也无法回复。

好的伙计们。我是一名新的Java程序员,我们刚刚完成覆盖标签窗格。这是我转入我的任务的状态:它不起作用,我无法弄清楚为什么。我已经改变了这么多垃圾,我不能再把它直接放在我脑海里了,但我知道它可能非常简单。

我为代码的长度道歉,但我试图给你完整的代码,这样你就可以告诉我在哪里搞砸了。

提前致谢。 - 另外,我知道还有其他警告(即未使用的进口),但我并不担心这些。而且,这不会影响我的成绩(正如我所说,已提交),但我想知道wtf我做错了!

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.DecimalFormat;
import java.awt.Color;

import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPanel;
import javax.swing.JComponent;

public class TabbedPane1 extends JPanel
{
public TabbedPane1()
{   
    JTabbedPane tabbedPane = new JTabbedPane();
    tabbedPane.addTab("DayGui", new DayGui());
    tabbedPane.addTab("OfficeCalc", new OfficeAreaCalculator());
    add(tabbedPane);
}

public static void main(String[] args)
{
    JFrame myFrame = new JFrame("Tabbed Programs");
    myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    myFrame.add(new TabbedPane1(), BorderLayout.CENTER);
    myFrame.pack();
    myFrame.setVisible(true);
}


class DayGui extends JPanel
{
private JPanel mainFrame;
private JButton cmdGood;
private JButton cmdBad;

public DayGui()
{
    mainFrame = new JPanel();

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

    Container myContainer = mainFrame;
    myContainer.setLayout(new FlowLayout());

    myContainer.add(cmdGood);
    myContainer.add(cmdBad);

    cmdGood.setMnemonic('G');
    cmdBad.setMnemonic('B');

    mainFrame.setSize(300, 100);

    myContainer.setBackground(Color.blue);
    cmdGood.setBackground(Color.cyan);
    cmdBad.setBackground(Color.cyan);

    /*mainFrame.addWindowListener(new WindowAdapter()
    {
        public void windowClosing(WindowEvent e)
        {
            System.exit(0);
        }
    });*/

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

    //mainFrame.setVisible(true);
}

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);
    }
}
}

class OfficeAreaCalculator extends JPanel
{
private JPanel mainFrame;
private JButton calculateButton;
private JButton exitButton;
private JTextField lengthField;
private JTextField widthField;
private JTextField areaField;
private JLabel lengthLabel;
private JLabel widthLabel;
private JLabel areaLabel;

public OfficeAreaCalculator()
{
    mainFrame = new JPanel();

    exitButton = new JButton("Exit");
    calculateButton = new JButton("Calculate");
    lengthField = new JTextField(5);
    widthField = new JTextField(5);
    lengthLabel = new JLabel("Enter the length of the office:");
    widthLabel = new JLabel("Enter the width of the office:");
    areaLabel = new JLabel("Office area:");
    areaField = new JTextField(5);
    areaField.setEditable(false);

    Container c = mainFrame;
    c.setLayout(new FlowLayout());
    c.setBackground(Color.green);

    c.add(lengthLabel);
    c.add(lengthField);
    c.add(widthLabel);
    c.add(widthField);
    c.add(areaLabel);
    c.add(areaField);
    c.add(calculateButton); 
    c.add(exitButton);

    calculateButton.setMnemonic('C');
    exitButton.setMnemonic('x');

    mainFrame.setSize(260, 150);

    /*mainFrame.addWindowListener(new WindowAdapter()
    {
        public void windowClosing(WindowEvent e)
        {
            System.exit(0);
        }
    });*/

    CalculateButtonHandler chandler = new CalculateButtonHandler();
    calculateButton.addActionListener(chandler);

    ExitButtonHandler ehandler = new ExitButtonHandler();
    exitButton.addActionListener(ehandler);

    FocusHandler fhandler = new FocusHandler();
    lengthField.addFocusListener(fhandler);
    widthField.addFocusListener(fhandler);
    areaField.addFocusListener(fhandler);

    //mainFrame.setVisible(true);
}   

class CalculateButtonHandler implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        DecimalFormat num = new DecimalFormat(",###.##");
        double width, length, area;
        String instring;

        instring = lengthField.getText();
        if(instring.equals(""))
        {
            instring = ("0");
            lengthField.setText("0");
        }
        length = Double.parseDouble(instring);

        instring = widthField.getText();
        if(instring.equals(""))
        {
            instring = ("0");
            widthField.setText("0");
        }
        width = Double.parseDouble(instring);

        area = length * width;
        areaField.setText(num.format(area));
    }
}

class ExitButtonHandler implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        System.exit(0);
    }
}

class FocusHandler implements FocusListener
{
    public void focusGained(FocusEvent e)
    {
        if(e.getSource() == lengthField || e.getSource() == widthField)
        {
            areaField.setText("");
        }
        else if(e.getSource() == areaField)
        {
            calculateButton.requestFocus();
        }
    }

    public void focusLost(FocusEvent e)
    {
        if(e.getSource() == widthField)
        {
            calculateButton.requestFocus();
        }
    }
}
}
}

1 个答案:

答案 0 :(得分:2)

您在JTabbedPane上添加了JPanel

JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("DayGui", new DayGui());
tabbedPane.addTab("OfficeCalc", new OfficeAreaCalculator());
add(tabbedPane);

由于JPanel默认为FlowLayout,因此您遇到此问题。将JPanel的布局设置为BorderLayout,问题将得到解决。

setLayout(new BorderLayout()); //Here
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("DayGui", new DayGui());
tabbedPane.addTab("OfficeCalc", new OfficeAreaCalculator());
add(tabbedPane);

修改

另外,如果您不想覆盖方法或定义新方法,请避免使用swing组件扩展类。喜欢组成而不是那个。我有同样糟糕的习惯。 例如,不要使用TabbedPane1扩展tour JPanel类,最好只创建一个返回自定义JTabbedPane的方法。像这样:

public JTabbedPane getTabbedPane() {
        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.addTab("DayGui", new DayGui());
        tabbedPane.addTab("OfficeCalc", new OfficeAreaCalculator());
        return tabbedPane;
}

要打电话:

myFrame.add(new TabbedPane1().getTabbedPane(), BorderLayout.CENTER);

这样你的类将被“打开”继承。