2010-11-27 58 views
1

這可能是一個非常愚蠢的問題,發佈在深夜。我試圖創建兩個放在另一個之上的JButton。但由於某種原因,它沒有得到正確的對齊。底部按鈕b2的左邊緣略微出現在頂部底部b1的左邊緣的左側。JButton對齊問題

下面是代碼:

class thistrial extends JPanel implements ActionListener 
{ 
... 
    public thistrial() 
    { 
    ..... 
    add(new JSeparator(SwingConstants.HORIZONTAL)); 

    //ADD THE START AND STOP BUTTONS 
    Border raisedBorder = BorderFactory.createRaisedBevelBorder(); 


    b1 = new JButton("START"); 
    b1.setVerticalTextPosition(AbstractButton.CENTER); 
    b1.setHorizontalTextPosition(AbstractButton.CENTER); 
    b1.setPreferredSize(new Dimension(220,100)); 
    add(new JSeparator(SwingConstants.HORIZONTAL)); 

    b2 = new JButton("STOP"); 
    b2.setPreferredSize(new Dimension(220,100)); 
    b2.setVerticalTextPosition(AbstractButton.CENTER); 
    b2.setHorizontalTextPosition(AbstractButton.CENTER); 
      add(b1); 
    add(b2); 
      ............. 
} 
    } 
/** MAIN function **/ 
public static void main(String args[]) 

{ 
     //Create and set up the window. 
     JFrame frame = new JFrame(); 
    frame.getContentPane().setBackground(Color.BLACK); 
    frame.setSize(398,480); 
    frame.setLocation(300,200); 
    frame.setResizable(false); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 


    //Create and set up the content pane. 
    thistrial newContentPane = new thistrial(); 
    frame.setContentPane(newContentPane); 

    //Display the window. 
    frame.setVisible(true); 
} 

我能做些什麼呢?

+1

你還沒有給我們正確的代碼段。顯然,顏色信息是無關緊要的。查看代碼將按鈕添加到面板的位置非常重要。另外你有`setLayout`? – 2010-11-27 09:33:21

回答

0

我建議使用正確的佈局管理器。你的代碼沒有顯示。