1

注意:我已经在整个网络(本网站和其他网站)上进行了搜索,但我自己无法回答。

好,朋友们。我是一名新的 Java 程序员,我们刚刚完成了对选项卡式窗格的介绍。这是我上交作业的状态:它不起作用,我不知道为什么。我已经改变了很多垃圾,我不能再把它放在脑海里了,但我知道这可能是一件非常简单的事情。

对于代码的长度,我深表歉意,但我试图给你我的全部代码,这样你就可以告诉我我把它放在哪里了。

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

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

1 回答 1

2

您添加了您JTabbedPaneJPanel.

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

由于默认设置为 a,因此您遇到了这个问题JPanelFlowLayout将您的布​​局设置JPanel为 BorderLayout,问题将得到解决。

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

编辑:

此外,如果您不想覆盖方法或定义新方法,请避免使用 Swing 组件扩展您的类。更喜欢组合而不是那个。我也有同样的坏习惯。例如,与其使用 扩展旅游TabbedPane1JPanel,不如只创建一个返回自定义的方法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);

这样,您的类将被“打开”以进行继承。

于 2013-06-06T23:43:44.690 回答