2015-04-12 54 views
0

我使用jbutton編寫了該代碼並編譯。但是當我運行這段代碼時,只有「buttoneone」出現,而「quibutton」沒有出現。這很奇怪,我不明白爲什麼會發生這種情況。請幫忙。如何使用Jbutton添加多個按鈕

import java.awt.EventQueue; 
import java.awt.Container; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.GroupLayout; 
import javax.swing.*; 

public class SimpleEx extends JFrame { 

    /** 
    * Default constructor 
    * Will be invoked when we create an instance of this class 
    */ 
    public SimpleEx() { 

     initializeUserInterface(); 
    } 

    /** 
    * Set up the window/frame just the way we want it 
    */ 
    private void initializeUserInterface() { 

     // set up the frame 
     setTitle("Frame Title"); 
     setSize(400, 300); 
     setLocationRelativeTo(null); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 

     // create the buttons 
     createButtons(); 

    } 

    /** 
    * In this method we'll create ALL the buttons we'll use in the application 
    */ 
    private void createButtons(){ 
     // Create a button with the label "Quit" and 
     JButton quitButton = new JButton("Quit"); 
     // set up the action listener 
     quitButton.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent event) { 
       System.exit(0); 
      } 
     }); 
      JButton buttonOne = new JButton("Button 1"); 
     // set up the action listener 
     buttonOne.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent event) { 

      } 
     }); 
     // place the button onto the window 
     Container pane = getContentPane(); 
     GroupLayout gl = new GroupLayout(pane); 
     GroupLayout g2 = new GroupLayout(pane); 
     pane.setLayout(gl); 
     pane.setLayout(g2); 

     gl.setAutoCreateContainerGaps(true); 
     g2.setAutoCreateContainerGaps(true); 

     gl.setHorizontalGroup(gl.createSequentialGroup() 
       .addComponent(quitButton) 
     ); 

     gl.setVerticalGroup(gl.createSequentialGroup() 
       .addComponent(quitButton) 
     ); 
     g2.setHorizontalGroup(g2.createSequentialGroup() 
       .addComponent(buttonOne) 
     ); 

     g2.setVerticalGroup(g2.createSequentialGroup() 
       .addComponent(buttonOne) 
     ); 
    } 

    /** 
    * Main method called when the application starts up 
    */ 
    public static void main(String[] args) { 

     // Set up the GUI event queue 
     EventQueue.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       // create the main frame/window and make it visible 
       SimpleEx ex = new SimpleEx(); 
       ex.setVisible(true); 
      } 
     }); 
    } 
} 
+0

就個人而言,我會避免GroupLayout的,它不是真正的意思爲手動操作:P – MadProgrammer

回答

2

本聲明

pane.setLayout(g2); 

替代佈局gl其中包含退出按鈕,因爲容器只能有一個佈局管理器。你可以刪除這一點,並做

gl.setHorizontalGroup(gl.createSequentialGroup() 
    .addComponent(quitButton).addComponent(buttonOne)); 
gl.setVerticalGroup(gl.createParallelGroup() 
    .addComponent(quitButton).addComponent(buttonOne)); 
+0

非常感謝你這似乎工作。 – user4289755

+0

yw,而此作品'GroupLayout'旨在用於GUI生成器工具。更好地使用一些更簡單的佈局管理器進行手工編碼,例如「FlowLayout」.... – Reimeus