2

FlowLayoutパネル内で 2 つのパネルを重ね合わせようとしているアプリケーションがありますBorderLayout。ただし、それを行うと、どちらのパネルも表示されません。

これらのパネルを積み重ねる前のフォームは次のようになります。

積み重ねる前に

しかし、それらを積み重ねると、次のようになります。

積み重ねた後

SSCCE を使用する場合、問題の原因となる行は次のとおりです。

pane.add(textAreaPanel, BorderLayout.CENTER);

したがって、その行をコメントアウトすると、フォームは少なくとも最初のパネルを表示します。

SSCCE

個別指導電卓

package tutoringcalculator;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.*;
import javax.swing.*;

public class TutoringCalculator extends JFrame {
    FlowLayout layout = new FlowLayout(FlowLayout.LEFT);

    public TutoringCalculator(String name) {
        super(name);
    }

    public void addComponentsToPane(final Container pane) {
        final JPanel contentPanel = new JPanel();
        contentPanel.setLayout(layout);
        final JPanel textAreaPanel = new JPanel();
        textAreaPanel.setLayout(layout);
        final JPanel buttons = new JPanel();
        buttons.setLayout(new FlowLayout(FlowLayout.RIGHT));

        contentPanel.add(new JLabel("Session Minutes:"));

        JTextField txt = new JTextField(10);
        txt.setName("sessionMinutes");

        txt.addFocusListener(new DecimalFieldFocusListener(new IValidDecimalValue() {
            @Override
            public boolean callback(String val) {
                return ApplicationState.getInstance().setCurrentSessionMinutes(val);
            }
        }));
        contentPanel.add(txt);

        // setup the default border
        ApplicationState.getInstance().setDefaultTextFieldBorder(txt.getBorder());

        contentPanel.add(new JLabel("Earnings:"));

        txt = new JTextField(10);
        txt.setName("earnings");

        txt.addFocusListener(new DecimalFieldFocusListener(new IValidDecimalValue() {
            @Override
            public boolean callback(String val) {
                return ApplicationState.getInstance().setCurrentEarnings(val);
            }
        }));
        contentPanel.add(txt);

        JButton btn = new JButton("Enter");
        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String result = ApplicationState.getInstance().acceptEarnings();
                if (result != null) {
                    JOptionPane.showMessageDialog(null, result, "Error Entering Earnings", JOptionPane.ERROR_MESSAGE);
                }
                else {
                    JOptionPane.showMessageDialog(null, "The session minutes and earnings were accepted.", "Success!", JOptionPane.INFORMATION_MESSAGE);

                    JTextField sessionMinutes = (JTextField)contentPanel.getComponent(1);
                    sessionMinutes.setText("");

                    JTextField earnings = (JTextField)contentPanel.getComponent(3);
                    earnings.setText("");

                    sessionMinutes.requestFocus();
                }
            }
        });
        contentPanel.add(btn);

        JTextArea textArea = new JTextArea();
        textAreaPanel.add(textArea);

        btn = new JButton("Run Report");
        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
            }
        });
        buttons.add(btn);

        btn = new JButton("Quit");
        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        buttons.add(btn);

        pane.add(contentPanel, BorderLayout.CENTER);
        pane.add(textAreaPanel, BorderLayout.CENTER);
        pane.add(buttons, BorderLayout.SOUTH);
    }

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event dispatch thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        TutoringCalculator frame = new TutoringCalculator("Tutoring Calculator");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        //Set up the content pane.
        frame.addComponentsToPane(frame.getContentPane());
        //Display the window.
        frame.setPreferredSize(new Dimension(600, 400));
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        /* Use an appropriate Look and Feel */
        try {
            //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
        } catch (UnsupportedLookAndFeelException | IllegalAccessException | InstantiationException | ClassNotFoundException ex) {
            ex.printStackTrace();
        }
        /* Turn off metal's use of bold fonts */
        UIManager.put("swing.boldMetal", Boolean.FALSE);
        //Schedule a job for the event dispatchi thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

アプリケーション状態

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tutoringcalculator;

import javax.swing.border.Border;

/**
 *
 * @author Owner
 */
final public class ApplicationState {
    private static ApplicationState instance = new ApplicationState();
    public static ApplicationState getInstance() {
        return instance;
    }

    private ApplicationState() {

    }

    private Border defaultTextFieldBorder;
    public Border getDefaultTextFieldBorder() {
        return defaultTextFieldBorder;
    }
    public void setDefaultTextFieldBorder(Border b) {
        defaultTextFieldBorder = b;
    }

    private double currentSessionMinutes;
    private boolean isValidSessionMinutes;
    public boolean setCurrentSessionMinutes(String text) {
        isValidSessionMinutes = false;
        try {
            currentSessionMinutes = Double.parseDouble(text);
            isValidSessionMinutes = true;
        }
        catch (NumberFormatException nfe) {
            return false;
        }

        return true;
    }

    private double currentEarnings;
    private boolean isValidCurrentEarnings;
    public boolean setCurrentEarnings(String text) {
        isValidCurrentEarnings = false;
        try {
            currentEarnings = Double.parseDouble(text);
            isValidCurrentEarnings = true;
        }
        catch (NumberFormatException nfe) {
            return false;
        }

        return true;
    }

    public String acceptEarnings() {
        if (!(isValidSessionMinutes && isValidCurrentEarnings)) {
            return "Please enter valid session minutes and current earnings.";
        }

        addEarnings(currentSessionMinutes, currentEarnings);

        return null;
    }

    private double[][] earningsArray = new double[100][2];
    private int newRowIndex = 0;
    private void addEarnings(double sessionMinutes, double earnings) {
        earningsArray[newRowIndex][0] = sessionMinutes;
        earningsArray[newRowIndex][1] = earnings;
        newRowIndex++;
    }
}

DecimalFieldFocusListener

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tutoringcalculator;

import java.awt.Color;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.BorderFactory;
import javax.swing.JTextField;

/**
 *
 * @author Owner
 */
public class DecimalFieldFocusListener implements FocusListener {
    private IValidDecimalValue _callback;

    public DecimalFieldFocusListener(IValidDecimalValue callback) {
        _callback = callback;
    }

    public void focusGained(FocusEvent e) {
    }

    public void focusLost(FocusEvent e) {
        JTextField txt = (JTextField)e.getSource();
        if (!_callback.callback(txt.getText())) {
            txt.setBorder(BorderFactory.createMatteBorder(2, 2, 2, 2, Color.red));
        }
        else {
            txt.setBorder(ApplicationState.getInstance().getDefaultTextFieldBorder());
            txt.setBackground(Color.white);
        }
    }
}

IValidDecimalValue

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tutoringcalculator;

/**
 *
 * @author Owner
 */
public interface IValidDecimalValue {
    boolean callback(String val);
}
4

2 に答える 2

4

その場所に表示できるコンポーネントは 1 つだけBorderLayout.CENTERです。したがって、いつ

pane.add(contentPanel, BorderLayout.CENTER);
pane.add(textAreaPanel, BorderLayout.CENTER);

が呼び出され、textAreaPanel実質的に を置き換えcontentPanelます。代わりに、次contentPanelの場所に配置できNORTHます。

pane.add(contentPanel, BorderLayout.NORTH);
于 2013-05-16T23:27:28.920 に答える
2

各場所に配置できるコンポーネントは 1 つだけです。2 つのパネルをBorderLayout.CENTER配置すると、一方を他方の上に配置するようなものになります。コンポーネントを互いに積み重ねたい場合は、BorderLayout Manager を使用できます。コンポーネントをコンテナーの X 軸または Y 軸に沿って積み重ねることができます。次の方法で実装できます。

pane.setLayout(new BoxLayout(pane,BoxLayout.Y_AXIS));
        pane.add(contentPanel);
        pane.add(textAreaPanel);
        pane.add(buttons);
于 2013-05-17T00:55:51.507 に答える