2016-04-17 24 views
1
私はJFrameの中に含まれる他のサイズ変更が可能なのJPanel( jpanelMidSep)で区切られたまったく同じサイズの2つのサイズ変更可能なのJPanel( jpanelDarkGrayjpanelLightGray)を持ちたい

...制御のJPanelはJFrameのサイズ変更

に応じてサイズjpanelDarkGrayの初期の高さjpanelLightGray146であり、jpanelMidSep8である。

JFrameのサイズを変更すると、jpanelDarkGrayの高さがjpanelLightGrayより1単位大きくなることがあります。 jpanelMidSepの高さを1単位(高さをjpanelDarkGrayに1単位減らし、高さをjpanelMidSepに1単位増やすことで、同じサイズに戻したいと思います)。ここで

すべての私のコード:

package thePack; 

import java.awt.Dimension; 
import java.awt.Color; 
import java.awt.event.ComponentAdapter; 
import java.awt.event.ComponentEvent; 
import java.awt.EventQueue; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.swing.*; 

public class JF_EqualSizeMovingPanels extends JFrame { 

    private JPanel jpanelDarkGray; 
    private JPanel jpanelLightGray; 
    private JPanel jpanelMidSep; 

    public JF_EqualSizeMovingPanels() { 
    initComponents(); 
    } 


    @SuppressWarnings("unchecked") 
    private void initComponents() { 

    jpanelDarkGray = new JPanel(); 
    jpanelMidSep = new JPanel(); 
    jpanelLightGray = new JPanel(); 

    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
    addComponentListener(new ComponentAdapter() { 
     public void componentResized(ComponentEvent evt) { 
     formComponentResized(evt); 
     } 
    }); 

    jpanelDarkGray.setBackground(new Color(96, 96, 96)); 

    GroupLayout jpanelDarkGrayLayout = new GroupLayout(jpanelDarkGray); 
    jpanelDarkGray.setLayout(jpanelDarkGrayLayout); 
    jpanelDarkGrayLayout.setHorizontalGroup(
     jpanelDarkGrayLayout.createParallelGroup(GroupLayout.Alignment.LEADING) 
     .addGap(0, 400, Short.MAX_VALUE) 
    ); 
    jpanelDarkGrayLayout.setVerticalGroup(
     jpanelDarkGrayLayout.createParallelGroup(GroupLayout.Alignment.LEADING) 
     .addGap(0, 146, Short.MAX_VALUE) 
    ); 

    jpanelMidSep.setBackground(new Color(128, 128, 128)); 

    GroupLayout jpanelMidSepLayout = new GroupLayout(jpanelMidSep); 
    jpanelMidSep.setLayout(jpanelMidSepLayout); 
    jpanelMidSepLayout.setHorizontalGroup(
     jpanelMidSepLayout.createParallelGroup(GroupLayout.Alignment.LEADING) 
     .addGap(0, 0, Short.MAX_VALUE) 
    ); 
    jpanelMidSepLayout.setVerticalGroup(
     jpanelMidSepLayout.createParallelGroup(GroupLayout.Alignment.LEADING) 
     .addGap(0, 8, Short.MAX_VALUE) 
    ); 

    jpanelLightGray.setBackground(new Color(160, 160, 160)); 

    GroupLayout jpanelLightGrayLayout = new GroupLayout(jpanelLightGray); 
    jpanelLightGray.setLayout(jpanelLightGrayLayout); 
    jpanelLightGrayLayout.setHorizontalGroup(
     jpanelLightGrayLayout.createParallelGroup(GroupLayout.Alignment.LEADING) 
     .addGap(0, 0, Short.MAX_VALUE) 
    ); 
    jpanelLightGrayLayout.setVerticalGroup(
     jpanelLightGrayLayout.createParallelGroup(GroupLayout.Alignment.LEADING) 
     .addGap(0, 146, Short.MAX_VALUE) 
    ); 

    GroupLayout layout = new GroupLayout(getContentPane()); 
    getContentPane().setLayout(layout); 
    layout.setHorizontalGroup(
     layout.createParallelGroup(GroupLayout.Alignment.LEADING) 
     .addComponent(jpanelDarkGray, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
     .addComponent(jpanelMidSep, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
     .addComponent(jpanelLightGray, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
     .addComponent(jpanelDarkGray, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
     .addGap(0, 0, 0) 
     .addComponent(jpanelMidSep, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) 
     .addGap(0, 0, 0) 
     .addComponent(jpanelLightGray, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
    ); 

    pack(); 
    } 

    private void formComponentResized(java.awt.event.ComponentEvent evt) { 
    System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); 
    System.out.println(this.getName()+ " - Height:" + this.getHeight() 
     + ", Width:" + this.getWidth()); 
    System.out.println(); 
    System.out.println("jpanelDarkGray - Height:" + jpanelDarkGray.getHeight() 
     + ", Width:" + jpanelDarkGray.getWidth()); 
    System.out.println("jpanelMidSep - Height:" + jpanelMidSep.getHeight() 
     + ", Width:" + jpanelMidSep.getWidth()); 
    System.out.println("jpanelLightGray - Height:" + jpanelLightGray.getHeight() 
     + ", Width:" + jpanelLightGray.getWidth()); 
    int adding = (jpanelDarkGray.getHeight() + jpanelMidSep.getHeight() + jpanelLightGray.getHeight()); 
    System.out.println("Adding:" + adding + ", diff:" + (this.getHeight() - adding)); 
    System.out.println("..........................................................."); 
    if (jpanelDarkGray.getHeight() != jpanelLightGray.getHeight()) { 
     int diff; 
     if (jpanelDarkGray.getHeight() > jpanelLightGray.getHeight()) { 
     diff = jpanelDarkGray.getHeight() - jpanelLightGray.getHeight(); 
     jpanelDarkGray.setSize(jpanelDarkGray.getWidth(), jpanelLightGray.getHeight()); 
//  jpanelDarkGray.setPreferredSize(new Dimension(jpanelDarkGray.getWidth(), 
//   jpanelLightGray.getHeight())); 
     jpanelMidSep.setSize(jpanelMidSep.getWidth(), jpanelMidSep.getHeight() + diff); 
//  jpanelMidSep.setPreferredSize(new Dimension(jpanelMidSep.getWidth(), 
//   jpanelMidSep.getHeight() + diff)); 
     } 
     if (jpanelLightGray.getHeight() > jpanelDarkGray.getHeight()) { 
     diff = jpanelLightGray.getHeight() - jpanelDarkGray.getHeight(); 
     jpanelLightGray.setSize(jpanelLightGray.getWidth(), jpanelDarkGray.getHeight()); 
//  jpanelLightGray.setPreferredSize(new Dimension(jpanelLightGray.getWidth(), 
//   jpanelDarkGray.getHeight())); 
     jpanelMidSep.setSize(jpanelMidSep.getWidth(), jpanelMidSep.getHeight() + diff); 
     jpanelMidSep.setPreferredSize(new Dimension(jpanelMidSep.getWidth(), 
      jpanelMidSep.getHeight() + diff)); 
     } 
    } 
    } 

    public static void main(String args[]) { 
    try { 
     for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) { 
     if ("Nimbus".equals(info.getName())) { 
      UIManager.setLookAndFeel(info.getClassName()); 
      break; 
     } 
     } 
    } catch (ClassNotFoundException | InstantiationException | 
      IllegalAccessException | UnsupportedLookAndFeelException ex) { 
     Logger.getLogger(JF_EqualSizeMovingPanels.class.getName()).log(Level.SEVERE, null, ex); 
    } 

    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
     new JF_EqualSizeMovingPanels().setVisible(true); 
     } 
    }); 
    } 
} 

しかし、あなたはJFormのサイズを変更するたびに、jSepMidの高さは(setPreferredSizeメソッドを使用して)、制御不能に成長するか、目標が(制御可能高さを変更する)に達していません(setSizeメソッドを使用)。

のsetSize方法(JFrameのの高さが奇数である場合、jpanelDarkGrayjpanelLightGrayの高さは、私は同じを行うことができなかった)を使用して出力。

enter image description here

run: 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:338, Width:416 

jpanelDarkGray - Height:146, Width:400 
jpanelMidSep - Height:8, Width:400 
jpanelLightGray - Height:146, Width:400 
Adding:300, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:338, Width:416 

jpanelDarkGray - Height:146, Width:400 
jpanelMidSep - Height:8, Width:400 
jpanelLightGray - Height:146, Width:400 
Adding:300, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:354, Width:416 

jpanelDarkGray - Height:154, Width:400 
jpanelMidSep - Height:8, Width:400 
jpanelLightGray - Height:154, Width:400 
Adding:316, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:331, Width:416 

jpanelDarkGray - Height:143, Width:400 
jpanelMidSep - Height:8, Width:400 
jpanelLightGray - Height:142, Width:400 
Adding:293, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:331, Width:438 

jpanelDarkGray - Height:143, Width:422 
jpanelMidSep - Height:8, Width:422 
jpanelLightGray - Height:142, Width:422 
Adding:293, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:331, Width:457 

jpanelDarkGray - Height:143, Width:441 
jpanelMidSep - Height:8, Width:441 
jpanelLightGray - Height:142, Width:441 
Adding:293, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:322, Width:457 

jpanelDarkGray - Height:138, Width:441 
jpanelMidSep - Height:8, Width:441 
jpanelLightGray - Height:138, Width:441 
Adding:284, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:326, Width:457 

jpanelDarkGray - Height:140, Width:441 
jpanelMidSep - Height:8, Width:441 
jpanelLightGray - Height:140, Width:441 
Adding:288, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:311, Width:457 

jpanelDarkGray - Height:133, Width:441 
jpanelMidSep - Height:8, Width:441 
jpanelLightGray - Height:132, Width:441 
Adding:273, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:311, Width:439 

jpanelDarkGray - Height:133, Width:423 
jpanelMidSep - Height:8, Width:423 
jpanelLightGray - Height:132, Width:423 
Adding:273, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:311, Width:408 

jpanelDarkGray - Height:133, Width:392 
jpanelMidSep - Height:8, Width:392 
jpanelLightGray - Height:132, Width:392 
Adding:273, diff:38 
........................................................... 

setPreferredSize方法を使用して出力。

enter image description here

run: 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:338, Width:416 

jpanelDarkGray - Height:146, Width:400 
jpanelMidSep - Height:8, Width:400 
jpanelLightGray - Height:146, Width:400 
Adding:300, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:338, Width:416 

jpanelDarkGray - Height:146, Width:400 
jpanelMidSep - Height:8, Width:400 
jpanelLightGray - Height:146, Width:400 
Adding:300, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:342, Width:416 

jpanelDarkGray - Height:148, Width:400 
jpanelMidSep - Height:8, Width:400 
jpanelLightGray - Height:148, Width:400 
Adding:304, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:337, Width:416 

jpanelDarkGray - Height:146, Width:400 
jpanelMidSep - Height:8, Width:400 
jpanelLightGray - Height:145, Width:400 
Adding:299, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:337, Width:420 

jpanelDarkGray - Height:145, Width:404 
jpanelMidSep - Height:9, Width:404 
jpanelLightGray - Height:145, Width:404 
Adding:299, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:337, Width:442 

jpanelDarkGray - Height:145, Width:426 
jpanelMidSep - Height:9, Width:426 
jpanelLightGray - Height:145, Width:426 
Adding:299, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:337, Width:430 

jpanelDarkGray - Height:145, Width:414 
jpanelMidSep - Height:9, Width:414 
jpanelLightGray - Height:145, Width:414 
Adding:299, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:313, Width:430 

jpanelDarkGray - Height:133, Width:414 
jpanelMidSep - Height:9, Width:414 
jpanelLightGray - Height:133, Width:414 
Adding:275, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:317, Width:430 

jpanelDarkGray - Height:135, Width:414 
jpanelMidSep - Height:9, Width:414 
jpanelLightGray - Height:135, Width:414 
Adding:279, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:315, Width:430 

jpanelDarkGray - Height:134, Width:414 
jpanelMidSep - Height:9, Width:414 
jpanelLightGray - Height:134, Width:414 
Adding:277, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:317, Width:430 

jpanelDarkGray - Height:135, Width:414 
jpanelMidSep - Height:9, Width:414 
jpanelLightGray - Height:135, Width:414 
Adding:279, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:318, Width:430 

jpanelDarkGray - Height:135, Width:414 
jpanelMidSep - Height:9, Width:414 
jpanelLightGray - Height:136, Width:414 
Adding:280, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:318, Width:447 

jpanelDarkGray - Height:140, Width:431 
jpanelMidSep - Height:10, Width:431 
jpanelLightGray - Height:130, Width:431 
Adding:280, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:320, Width:447 

jpanelDarkGray - Height:129, Width:431 
jpanelMidSep - Height:20, Width:431 
jpanelLightGray - Height:133, Width:431 
Adding:282, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:322, Width:447 

jpanelDarkGray - Height:130, Width:431 
jpanelMidSep - Height:24, Width:431 
jpanelLightGray - Height:130, Width:431 
Adding:284, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:328, Width:447 

jpanelDarkGray - Height:133, Width:431 
jpanelMidSep - Height:24, Width:431 
jpanelLightGray - Height:133, Width:431 
Adding:290, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:328, Width:457 

jpanelDarkGray - Height:133, Width:441 
jpanelMidSep - Height:24, Width:441 
jpanelLightGray - Height:133, Width:441 
Adding:290, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:328, Width:401 

jpanelDarkGray - Height:133, Width:385 
jpanelMidSep - Height:24, Width:385 
jpanelLightGray - Height:133, Width:385 
Adding:290, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:328, Width:401 

jpanelDarkGray - Height:133, Width:385 
jpanelMidSep - Height:24, Width:385 
jpanelLightGray - Height:133, Width:385 
Adding:290, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:327, Width:401 

jpanelDarkGray - Height:133, Width:385 
jpanelMidSep - Height:24, Width:385 
jpanelLightGray - Height:132, Width:385 
Adding:289, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:327, Width:401 

jpanelDarkGray - Height:133, Width:385 
jpanelMidSep - Height:24, Width:385 
jpanelLightGray - Height:132, Width:385 
Adding:289, diff:38 
........................................................... 

いくつかの手がかり?

EDIT @chepe_luchoのソリューションをテストする1

は...私のために正常に動作します!

run: 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:338, Width:416 

jpanelDarkGray - Height:146, Width:400 
jpanelMidSep - Height:8, Width:400 
jpanelLightGray - Height:146, Width:400 
Adding:300, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:343, Width:416 

jpanelDarkGray - Height:148, Width:400 
jpanelMidSep - Height:8, Width:400 
jpanelLightGray - Height:149, Width:400 
Adding:305, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:343, Width:422 

jpanelDarkGray - Height:148, Width:406 
jpanelMidSep - Height:9, Width:406 
jpanelLightGray - Height:148, Width:406 
Adding:305, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:292, Width:422 

jpanelDarkGray - Height:123, Width:406 
jpanelMidSep - Height:9, Width:406 
jpanelLightGray - Height:122, Width:406 
Adding:254, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:292, Width:460 

jpanelDarkGray - Height:123, Width:444 
jpanelMidSep - Height:8, Width:444 
jpanelLightGray - Height:123, Width:444 
Adding:254, diff:38 
........................................................... 
+0

をユーザーがパネルサイズやコードを変更することができスイングスプリットペインが変化しないように、独自のスプリットペインを構築しようとしていますか? – DevilsHnd

+0

常に同じサイズ(高さと幅)の2つのパネルが必要です。コンテナ(この場合はJFrame)は自由にサイズ変更可能ですが、jpanelDarkGrayとjpanelLightGrayのサイズは自由ではありませんが、制御されています。 –

+0

'GridBagLayout'には丸めの問題があります。時には、利用可能なスペースにすべてのコンポーネントを表示する方法を決定する必要がある場合があります。 – MadProgrammer

答えて

1

あなたはこのコードをテストする必要があります。

if (jpanelDarkGray.getHeight() != jpanelLightGray.getHeight()) { 
    if (jpanelMidSep.getHeight() == 8) { 
    jpanelMidSep.setPreferredSize(new Dimension(jpanelMidSep.getWidth(), 9)); 
    } 
    if (jpanelMidSep.getHeight() == 9) { 
    jpanelMidSep.setPreferredSize(new Dimension(jpanelMidSep.getWidth(), 8)); 
    } 
} 
0

コンテナJFrameのGridLayoutを使用できます。行と列を設定します。あなたの場合、2行1列になります。 グリッドレイアウトは、すべてのコンポーネント

jframe.setLayout(new GridLayout(2,1)); 

に等しい面積を共有するには、フレームとフレームのサイズを変更しようとするグリッドレイアウトを設定してみてください。これは、同じ面積でパネルのサイズを変更します。 setPrefferedSize()をパネルに使用しないでください。

+1

OPが望んでいるという事実3つのパラメータは、2つが等しいサイズで、3つ目は(私は可変であると仮定します)、 'GridLayout'は助けになりません – MadProgrammer

関連する問題