2015-05-06 226 views
3

我不是很擅長Java GUI,需要尋求幫助。BorderLayout顯示邊框線

我打算在我BorderLayout,中心以西圖像添加到我的內容和按鈕在底部。

我創建了一個空的邊界,使我的南方面板和我的西部和中部面板之間的一些墊襯。現在我只想在南部邊界上添加一條線。

如下面的截圖所示,有西部面板和中央面板之間的直線爲好,我怎麼能刪除該行和整個南面面板的頂部維持線?

附件是我的代碼:

enter image description here

import java.awt.BorderLayout; 
import java.awt.FlowLayout; 
import javax.swing.BorderFactory; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

public class test { 
    public static void main(String[] args) { 

     JPanel panel1 = new JPanel(new BorderLayout()); 
     JPanel panel2 = new JPanel(new FlowLayout()); 
     JPanel panel3 = new JPanel(new FlowLayout()); 
     JPanel panel4 = new JPanel(new FlowLayout()); 

     JFrame frame = new JFrame(); 

     panel2.add(new JLabel("WEST <will be adding image here>")); 
     panel3.add(new JLabel("CENTER <contents>")); 
     panel4.add(new JLabel("SOUTH <will be adding buttons>")); 

     panel1.add(panel2, BorderLayout.WEST); 
     panel1.add(panel3, BorderLayout.CENTER); 
     panel1.add(panel4, BorderLayout.SOUTH); 

     panel2.setBorder(BorderFactory.createRaisedBevelBorder()); 
     panel3.setBorder(BorderFactory.createRaisedBevelBorder());  
     panel4.setBorder(BorderFactory.createEmptyBorder(10,10,10,10)); 

     frame.add(panel1); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     frame.pack(); 
     frame.setSize(510,390); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
     frame.setResizable(false); 
    } 
} 

回答

6

要刪除西部和中心之間的邊界,只是刪除其邊界

panel2.setBorder(BorderFactory.createRaisedBevelBorder()); 
panel3.setBorder(BorderFactory.createRaisedBevelBorder()); 

如果要保留邊框與邊框的邊框,請改爲將邊框添加到panel1

作爲南,如果你想「添加一條線穿過,南邊界之上」,並保持空邊框,使用:

panel4.setBorder(BorderFactory.createCompoundBorder(
     BorderFactory.createEmptyBorder(10, 10, 10, 10), 
     BorderFactory.createMatteBorder(2, 0, 0, 0, Color.BLACK))); 

createRaisedBevelBorder()代替createMatteBorder

請記住,您可以切換邊框的順序及其樣式。有關更多信息,請參閱tutorial

Inner: matte, outer: empty Inner: empty, outer: matte

3

試試這個:

import java.awt.BorderLayout; 
import java.awt.FlowLayout; 
import javax.swing.BorderFactory; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

public class test { 
    public static void main(String[] args) { 

     JPanel panel1 = new JPanel(new BorderLayout()); 
     JPanel panel2 = new JPanel(new BorderLayout()); 
     JPanel panel3 = new JPanel(new FlowLayout()); 
     JPanel panel4 = new JPanel(new FlowLayout()); 
     JPanel panel5 = new JPanel(new FlowLayout()); 

     JFrame frame = new JFrame(); 

     panel4.add(new JLabel("WEST <will be adding image here>")); 
     panel5.add(new JLabel("CENTER <contents>")); 
     panel3.add(new JLabel("SOUTH <will be adding buttons>")); 

     panel1.add(panel2, BorderLayout.CENTER); 
     panel1.add(panel3, BorderLayout.SOUTH); 
     panel2.add(panel4, BorderLayout.WEST); 
     panel2.add(panel5, BorderLayout.CENTER); 

     panel2.setBorder(BorderFactory.createRaisedBevelBorder());  
     panel3.setBorder(BorderFactory.createEmptyBorder(10,10,10,10)); 

     frame.add(panel1); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     frame.pack(); 
     frame.setSize(510,390); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
     frame.setResizable(false); 
    } 
} 
+0

在我看來你的回答是最好的:) –

1

enter image description here
`下面的代碼將組件以一幀的內容面板。 由於內容窗格默認使用BorderLayout類,因此代碼 不需要設置佈局管理器。完整的程序 位於BorderLayoutDemo.java文件中。

...//Container pane = aFrame.getContentPane()... 
JButton button = new JButton("Button 1 (PAGE_START)"); 
pane.add(button, BorderLayout.PAGE_START); 

//Make the center component big, since that's the 
//typical usage of BorderLayout. 
button = new JButton("Button 2 (CENTER)"); 
button.setPreferredSize(new Dimension(200, 100)); 
pane.add(button, BorderLayout.CENTER); 

button = new JButton("Button 3 (LINE_START)"); 
pane.add(button, BorderLayout.LINE_START); 

button = new JButton("Long-Named Button 4 (PAGE_END)"); 
pane.add(button, BorderLayout.PAGE_END); 

button = new JButton("5 (LINE_END)"); 
pane.add(button, BorderLayout.LINE_END); 

Specify the component's location (for example, BorderLayout.LINE_END) as one of the arguments to the add method. If this component is missing from a container controlled by a BorderLayout object, make sure that the component's location was specified and no another component was placed in the same location. 

All tutorial examples that use the BorderLayout class specify the component as the first argument to the add method. For example: 

add(component, BorderLayout.CENTER) //preferred 
However, the code in other programs specifies the component as the second argument. For example, here are alternate ways of writing the preceding code: 

add(BorderLayout.CENTER, component) //valid but old fashioned 
    or 
add("Center", component)    //valid but error prone