質問

を持っています JPanel デフォルトで使用するのは FlowLayout マネージャー。ドキュメントスタイルの利点が気に入っています FlowLayout ここでは、自動折り返しを使用してコンポーネントを一度に 1 つずつ追加しますが、コンポーネントに新しい行を強制的に選択させたいと考えています。

を使用していれば読みます BoxLayout のようなものを挿入できます component return key そしてコンポーネントを強制的に新しい行で開始します。自分の決定と、どちらがより良いアプローチであるかについてのアドバイスが必要です。

私は持っています JLabel そして JTextField を 1 行に配置したいのですが、 JTextArea の中に包まれた JScrollPane 下に。

役に立ちましたか?

解決

  • を組み合わせて使用​​します FlowLayout そして BorderLayout. 。目的の結果を得るには、レイアウトをネストすることをお勧めします。
  • JLabel そしてその JTextField 1つに入るだろう JPanelFlowLayout
  • それからもう一つ JPanelBorderLayout 前のパネルを保持します。 NORTH 位置と、 JTextAreaJScrollPaneCENTER 位置。

    JPanel topPanel = new JPanel();
    JLabel label = new JLabel("Text Field Label");
    JTextField jtf = new JTextField(20);
    topPanel.add(label);
    topPanel.add(jtf);
    
    JPanel bothPanel = new JPanel(new BorderLayout());
    JTextArea jta = new JTextArea(20, 40);
    bothPanel.add(topPanel, BorderLayout.NORTH);
    bothPanel.add(new JScrollPane(jta));
    

見て コンテナ内でのコンポーネントのレイアウト

enter image description here

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class FlowBorderDemo {

    public FlowBorderDemo() {
        JPanel topPanel = new JPanel();
        JLabel label = new JLabel("Text Field Label");
        label.setForeground(Color.white);
        JTextField jtf = new JTextField(20);
        topPanel.add(label);
        topPanel.add(jtf);
        topPanel.setBackground(Color.black);


        JPanel bothPanel = new JPanel(new BorderLayout());
        JTextArea jta = new JTextArea(20, 40);
        JScrollPane scrollPane = new JScrollPane(jta);
        scrollPane.setBorder(BorderFactory.createMatteBorder(3, 0, 0, 0, Color.GRAY));
        bothPanel.add(topPanel, BorderLayout.NORTH);
        bothPanel.add(scrollPane);
        bothPanel.setBorder(BorderFactory.createMatteBorder(3, 8, 3, 8, Color.GRAY));

        JLabel copyLabel = new JLabel("<html>&copy;2014 peeskillet</html>");
        copyLabel.setBackground(Color.LIGHT_GRAY);
        copyLabel.setHorizontalAlignment(JLabel.CENTER);
        bothPanel.add(copyLabel, BorderLayout.PAGE_END);


        JFrame frame = new JFrame();
        frame.add(bothPanel);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager
                            .getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException
                        | IllegalAccessException
                        | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                new FlowBorderDemo();
            }
        });
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top