2

私は数週間前に(初めて)swing を使ってみました。垂直方向の配置に問題があるようです。

状況は次のとおりです。borderlayout の West 部分に boxlayout(Y_AXiS) JPanel があります。JPanel w/boxlayout 内には、他に 2 つの JPanel があり、これらを垂直方向に配置します (フレームのサイズに関係なく、画面の上部に向かって押し出されます)。ただし、レイアウト マネージャーは、2 つの JPanel の間に大きな垂直方向のスペースを置いているようです。これにはグリッドレイアウトの方が適しているのではないかと考えていましたが、よくわかりません。私は何度もグーグルで答えを探し、問題を解決しようとしましたが、うまくいきませんでした。助けてください!

package com.granet;

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;


public class CustomerOrganizer  extends JFrame
{
    public CustomerOrganizer()
    {
        JPanel topPanel = new JPanel();
        topPanel.setBackground(Color.darkGray);

        JLabel customerSlogan = new JLabel("Customer Organizer");

        topPanel.add(customerSlogan);



        JPanel leftPanel = new JPanel();
        leftPanel.setBackground(Color.darkGray);
        leftPanel.setBorder(new EmptyBorder(10,100,00,0));


        leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));

        DrawPanel firstTab = new DrawPanel(Color.white, 350, 50);
        DrawPanel secondTab = new DrawPanel(Color.white, 350, 50);

        JLabel firstTabText = new JLabel("First Tab");
        JLabel secondTabText = new JLabel("Second Tab");

        firstTab.setBorder(new EmptyBorder(0,60,60,0));
        secondTab.setBorder(new EmptyBorder(0,60,60,0));

        firstTab.add(firstTabText);
        secondTab.add(secondTabText);


        leftPanel.add(firstTab);
        leftPanel.add(secondTab);

        firstTab.setAlignmentX(Component.LEFT_ALIGNMENT);
        secondTab.setAlignmentX(Component.LEFT_ALIGNMENT);

/*      DOESN'T WORK, I'm pretty sure this changes the point on the jpanel which used for alignment (top, bottom, left or right) 
        firstTab.setAlignmentY(Component.TOP_ALIGNMENT);
        secondTab.setAlignmentY(Component.TOP_ALIGNMENT);
*/      


        add(topPanel, BorderLayout.NORTH);
        add(leftPanel, BorderLayout.WEST);

        pack();

    }

    public static void main(String[] args)
    {
        CustomerOrganizer frame = new CustomerOrganizer();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(700,400);
        frame.setVisible(true);

    }
}

DrawPanel.java

package com.granet;

import javax.swing.*;
import java.awt.*;

public class DrawPanel extends JPanel
{
    Color color;
    int width;
    int height;

    public DrawPanel(Color color, int width, int height)
    {
        this.color=color;
        this.width=width;
        this.height=height;
    }

    public DrawPanel()
    {
        this.color = Color.darkGray;
        this.width=this.getWidth();
        this.height=this.getHeight();
    }

    public void paintComponent(Graphics g)
    {
        g.setColor(color);
        g.fillRect(0, 0, width, height);
    }
}

これを実行すると、次のように表示されます: http://concertforhope.net/citeforme/javabug.png

下部のタブが上部のタブに対して押し上げられていないことに注意してください。

NVM、理由はわかりました。タブはそれほど小さくはありませんでした。背景を間違った方法で塗りつぶしていました (白いボックスは実際のタブを表していませんでした)。助けてくれてありがとう。

4

2 に答える 2

4

これはバグではありません。ボックスレイアウトは、スペース全体をコンポーネント間で均等に分割することになっています。

おそらく、垂直方向のフローレイアウトまたはそのようなものが必要になるでしょう。

Google WindowBuilder (無料)を入手して、GUI でレイアウトを少し試してみることを強くお勧めします。

特別なケースでは、タブを右側に配置してJTabbedPaneを検討することもできます。

于 2011-06-11T14:27:58.180 に答える
4

非表示コンポーネントをフィラーとして使用して、垂直方向の間隔を制御することも検討してください。たとえば、org.gcs.kinetic.ControlPanelを使用していますBox.createVerticalStrut()が、このでは を使用していますBox.createVerticalGlue()

于 2011-06-11T16:43:02.507 に答える