尝试在独立的框架类中使用GridBagConstraints

时间:2014-08-20 19:09:32

标签: java user-interface gridbaglayout jtabbedpane

以下代码最终将成为选项卡式面板的面板之一。我只使用过一次GridBagConstraints,因为我有一个Pane,所以它更容易。我会使用像

这样的东西
mainFrame.getContentPane().add(panel, BorderLayout.NORTH);  

如果我想要这个代码的gridbaglayout怎么办?我没有窗格。我怎样才能完成添加GridBagConstraints?

package Week4;

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
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.io.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class OfficeAreaCalculator extends JPanel{

private JButton calculateButton;
private JTextField lengthField;
private JTextField widthField;
private JTextField areaField;
private JLabel lengthLabel;
private JLabel widthLabel;
private JLabel areaLabel;

public OfficeAreaCalculator(){

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

    add(lengthLabel);
    add(lengthField);
    add(widthLabel);
    add(widthField);
    add(areaLabel);
    add(areaField);
    add(calculateButton);

    calculateButton.setMnemonic('C');
    CalculateButtonHandler chandler = new CalculateButtonHandler();
    calculateButton.addActionListener(chandler);

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

    class CalculateButtonHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            try{

            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));    
            }
            catch( Exception d){
                lengthField.setText("Invalid");
                widthField.setText("Invalid");
            }
        }   
    }

    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 :(得分:0)

我想我可能已经找到了自己问题的答案。在我开始弄乱网格坐标之前,我需要这样做:

package Week6;

import java.awt.*;

import javax.swing.*;

import java.io.*;

public class Index extends JPanel{

    private JLabel searchLabel;
    private JTextField searchField;
    private JLabel charLabel;
    private JTextField charField;


public Index(){

    GridBagLayout gridbag = new GridBagLayout();
    GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.NORTH;
    setBackground(Color.WHITE);

    super.setLayout(gridbag);

    searchLabel = new JLabel("Enter text to be searched:");
    searchField = new JTextField("");
    charLabel = new JLabel("Exter a character:");
    charField = new JTextField("");


    c.insets = new Insets(10, 10, 10, 10);

    c.gridx = 0;
    c.gridy = 0;    
    add(searchLabel, c);
    c.gridx = 1;
    c.gridy = 0;
    add(searchField, c);
    c.gridx = 0;
    c.gridy = 1;
    add(charLabel, c);
    c.gridx = 1;
    c.gridy = 1;
    add(charField, c);  

    }
}