2017-04-20 102 views
1

我试图让行匹配高度(yAxis)到复合边框中心的标题。这里是一个图像,以帮助解释什么我正在寻找(仅适用于不至于箭头实际上指向)...增加厚度createCompoundBorder

enter image description here

了一下,这里是代码...

package Main; 

import org.apache.commons.lang3.ArrayUtils; 

import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.*; 
import javax.swing.border.Border; 
import javax.swing.border.TitledBorder; 

/** 
* Created by Vako on 4/16/2017. 
*/ 
public class TestGui extends JFrame { 
    private final String[] guiCharSelDefault = {"--- Select Character ---"}; 
    private final String[] characters = {"charOne", "charTwo", "charThree", "charFour"}; 
    private final String[] GuiCharSel = (String[]) ArrayUtils.addAll(guiCharSelDefault, characters); 
    private final String[] weapon = {"Weapon"}; 
    private final String[][] allWeapons = { 
      { 
        "weakWeaponOne", "strongWeaponOne", "shortWeaponOne", "longWeaponOne" 
      }, 
      { 
        "weakWeaponTwo", "strongWeaponTwo", "shortWeaponTwo", "longWeaponTwo" 
      }, 
      { 
        "weakWeaponThree", "strongWeaponThree", "shortWeaponThree", "longWeaponThree" 
      }, 
      { 
        "weakWeaponFour", "strongWeaponFour", "shortWeaponFour", "longWeaponFour" 
      } 
    }; 
    private JComboBox charCombo = new JComboBox(GuiCharSel); 
    private JComboBox weaponsCombo = new JComboBox(weapon); 
    private JPanel centerFrame = createCenterFrame(); 

    //************************************************************************************** 

    private GridBagConstraints setGbc(int gridx, int gridy, int ipadx, int ipady, String anchorLocation, double weightx, double weighty, Insets insets){ 
     GridBagConstraints gbc = new GridBagConstraints(); 

     if (anchorLocation.toUpperCase().equals("NORTHWEST")){ 
      gbc.anchor = GridBagConstraints.NORTHWEST; 
     } else if (anchorLocation.toUpperCase().equals("NORTH")){ 
      gbc.anchor = GridBagConstraints.NORTH; 
     } else if (anchorLocation.toUpperCase().equals("NORTHEAST")){ 
      gbc.anchor = GridBagConstraints.NORTHEAST; 
     } else if (anchorLocation.toUpperCase().equals("WEST")){ 
      gbc.anchor = GridBagConstraints.WEST; 
     } else if (anchorLocation.toUpperCase().equals("EAST")){ 
      gbc.anchor = GridBagConstraints.EAST; 
     } else if (anchorLocation.toUpperCase().equals("SOUTHWEST")){ 
      gbc.anchor = GridBagConstraints.SOUTHWEST; 
     } else if (anchorLocation.toUpperCase().equals("SOUTH")){ 
      gbc.anchor = GridBagConstraints.SOUTH; 
     } else if (anchorLocation.toUpperCase().equals("SOUTHEAST")){ 
      gbc.anchor = GridBagConstraints.SOUTHEAST; 
     } else { 
      gbc.anchor = GridBagConstraints.CENTER; 
     } 

     gbc.gridx = gridx; 
     gbc.gridy = gridy; 
     gbc.ipadx = ipadx; 
     gbc.ipady = ipady; 
     gbc.weightx = weightx; 
     gbc.weighty = weighty; 
     gbc.insets = insets; 

     return gbc; 
    } 

    private Insets setInsets(int top, int left, int bottom, int right){ 
     Insets insets = new Insets(top,left,bottom,right); 
     return insets; 
    } 

    //************************************************************************************** 

    private JPanel createCenterFrame(){ 
     JPanel pnl = new JPanel(); 
     Border raisedBevel = BorderFactory.createRaisedBevelBorder(); 
     Border loweredBevel = BorderFactory.createLoweredBevelBorder(); 
     Border compound = BorderFactory.createCompoundBorder(raisedBevel, loweredBevel); 
     TitledBorder topFrameTitle = BorderFactory.createTitledBorder(compound, "Character"); 
     topFrameTitle.setTitleJustification(TitledBorder.CENTER); 

     pnl.setBorder(topFrameTitle); 

     charCombo.addActionListener(
       new ActionListener() { 
        public void actionPerformed(ActionEvent e) { 
         String charName = ((JComboBox)(e.getSource())).getSelectedItem().toString(); 
         if (charName.equals("charOne")){ 
          weaponsCombo.removeAllItems(); 
          weaponsCombo.setModel(new DefaultComboBoxModel(allWeapons[1])); 
         } 
        } 
       } 
     ); 
     pnl.add(charCombo, setGbc(0,0, 0, 0, "NORTHWEST", 0, 0, setInsets(0, 10, 0, 0))); 
     pnl.add(weaponsCombo, setGbc(0,0, 0, 0, "NORTHWEST", 0, 0, setInsets(0, 10, 0, 0))); 

     return pnl; 
    } 

    TestGui(){ 
     add(centerFrame, BorderLayout.CENTER); 

     setSize(400,175); 
     setVisible(true); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 

    //************************************************************************************** 

    public static void main(String[] args) { 
     new TestGui(); 
    } 
} 

如果有人有任何想法如何做到这一点,我会非常感谢帮助。

回答

3

下面是使用MatteBorder

screenshot

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

public class TestGui2 extends JFrame { 
    private static JPanel createCenterFrame() { 
    Border raisedBevel = BorderFactory.createRaisedBevelBorder(); 
    Border lineBorder = BorderFactory.createMatteBorder(10, 0, 0, 0, Color.GRAY.brighter()); 
    Border loweredBevel = BorderFactory.createLoweredBevelBorder(); 
    Border compound1 = BorderFactory.createCompoundBorder(raisedBevel, lineBorder); 
    Border compound2 = BorderFactory.createCompoundBorder(compound1, loweredBevel); 
    TitledBorder topFrameTitle = BorderFactory.createTitledBorder(compound2, "Character"); 
    topFrameTitle.setTitleJustification(TitledBorder.CENTER); 

    JPanel pnl = new JPanel(); 
    pnl.setBorder(topFrameTitle); 
    return pnl; 
    } 
    TestGui2() { 
    add(createCenterFrame(), BorderLayout.CENTER); 
    setSize(400, 175); 
    setVisible(true); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 
    public static void main(String[] args) { 
    EventQueue.invokeLater(() -> new TestGui2()); 
    } 
} 
+0

真棒,感谢一个可能的实现。我不知道你可以保持堆叠在一起。 –