2016-11-16 6 views
0

Ich habe ein Formular mit 5 Tasten mit einem GridBagLayout erstellt dieses Formular zu erhalten: This is the form I have createdWelches Layout sollte ich verwenden?

Was ich will, ist für die Tasten größer und gleichmäßig wie diese angeordnet zu sein: Desired Form

Hier ist mein Code :

package com.GUI; 
import java.awt.Color; 
import javax.swing.*; 
import com.seaglasslookandfeel.*; 


public class JFramePlus extends JFrame{ 
    public JFramePlus(){ 
     super("OmegaBrain"); 
     setSize(1000,800); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     setVisible(true); 
     getContentPane().setBackground(Color.black); 
     setResizable(false); 

    } 

} 

Dies ist die Oberklasse der betreffenden Klasse.

package com.GUI; 
import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.*; 
import java.util.Stack; 



class GamePlay extends JFramePlus implements ActionListener{ 

    //Create Stack 
    Stack sequence = new Stack(); 

    //Declare Variables 
    String greekSequence; 
    int stackCount; 
    int timeLeft; 
    static int optionNo; 

    //Create Constraints 
    GridBagConstraints c = new GridBagConstraints(); 

    //Defining new objects 
    JLabel timeDisplay, sequenceViewer; 
    JButton mainMenu, input1, input2, input3, input4, input5; 
    JPanel timerPane, centerPane, exitPane; 
    Timer t; 


    GamePlay(){ 


     //Create Labels 
     timeDisplay = new JLabel(); 
     sequenceViewer = new JLabel(); 

     //Create Panels 
     timerPane = new JPanel(); 
     centerPane = new JPanel();  
     exitPane = new JPanel(); 

     //Change layout of centerPane 
     centerPane.setLayout(new GridBagLayout()); 

     //Creates JButtons 
     mainMenu = new JButton("Main Menu"); 

     input1 = new JButton("Ξ"); 
      c.gridx = 0; 
      c.gridy = 1; 
     centerPane.add(input1, c);  

     input2 = new JButton("Ω"); 
      c.gridx = 2; 
      c.gridy = 1; 
     centerPane.add(input2, c); 

     input3 = new JButton("Ψ"); 
      c.gridx = 4; 
      c.gridy = 1; 
     centerPane.add(input3, c); 

     input4 = new JButton("Φ"); 
      c.gridx = 1; 
      c.gridy = 2; 
     centerPane.add(input4, c); 

     input5 = new JButton("Γ"); 
      c.gridx = 3; 
      c.gridy = 2; 
     centerPane.add(input5, c); 

     //Create Timer 
     t = new Timer(1000, this); 


     //Changes the size of the font 
     timeDisplay.setFont(timeDisplay.getFont().deriveFont(64.0f)); 

     //Generate Sequence 
     sequenceGenerator(); 


     //Add components to panels 
     timerPane.add(timeDisplay); 

     centerPane.add(sequenceViewer, c); 

     exitPane.add(mainMenu); 



     //add panels to frame 
     add(timerPane, BorderLayout.LINE_END); 
     add(centerPane, BorderLayout.CENTER); 
     add(exitPane, BorderLayout.SOUTH); 


     //Change colors to fit theme 
     timeDisplay.setForeground(Color.WHITE); 
     sequenceViewer.setForeground(Color.WHITE); 
     timerPane.setBackground(Color.BLACK); 
     centerPane.setBackground(Color.BLACK); 
     exitPane.setBackground(Color.BLACK); 


     //Add ActionListeners to buttons 
     mainMenu.addActionListener(this); 
     input1.addActionListener(this); 
     input2.addActionListener(this); 
     input3.addActionListener(this); 
     input4.addActionListener(this); 
     input5.addActionListener(this); 
    } 

    public void sequenceGenerator(){ 
     sequence.push(1 + (int)(Math.random() * optionNo)); 
     stackCount++; 

     greekSequence = ""; 
     for(int i = 0; i < stackCount; i++){ 
      if (sequence.get(i) == 1){ 
       greekSequence = greekSequence + 'Ξ'; 
      } 
     } 
     sequenceViewer.setText(greekSequence); 
    } 




    void startTimer() { 
     t.start(); 

    } 



    public void actionPerformed(ActionEvent evt) { 
     Object source = evt.getSource(); 
     if(source == t){ 
      timeDisplay.setText(String.valueOf(timeLeft)); 
      timeLeft--; 

      if(timeLeft == -1){ 
       t.stop(); 
      } 
     } 

     else if(source == mainMenu){ 
      int yesNo = JOptionPane.showConfirmDialog( 
       null, 
       "Are you sure you want to exit? Your current score will be saved as it is." , 
       "Exit Game?", 
       JOptionPane.YES_NO_OPTION); 

      if(yesNo == JOptionPane.YES_OPTION){ 
       dispose(); 
       mainMenu menu = new mainMenu(); 
      } 

      else{ 

      } 
     } 
    } 
} 

Antwort

1

Dies ist eine geladene Frage. Ich werde damit anfangen zu sagen, GridBagLayout ist ganz in Ordnung für das, was Sie erreichen wollen. Ich denke, Sie sollten etwas Zeit in die Untersuchung investieren: How to Use GridBagLayout.

Sie sollten auch schauen Sie in „Einschübe“ für Abstandsoptionen und gridwidth Verwendung gridheight, und vielleicht sogar ipadx und ipady, wenn sie mit GridBagConstraints beschäftigen.

+1

Ich stimme der Verwendung von GBL zu. Einige Tipps für 'größere Schaltflächen' wären, die verwendete Schriftgröße zu erhöhen oder [AbstractButton.setMargin (Installs) '] aufzurufen (http://docs.oracle.com/javase/8/docs/api/javax/ swing/AbstractButton.html # setMargin-java.awt.Insets-). –

0

Sie können auch ein BoxLayout verwenden. (How to use BoxLayout).

Sie können Ihre Komponenten wie gewünscht positionieren, und wenn Sie zwischen den Komponenten Platz hinzufügen möchten, können Sie Ihren Komponenten einen leeren Rahmen hinzufügen oder unsichtbare Komponenten dazwischen platzieren.

Sie können this discussion für weitere Informationen sehen.

Verwandte Themen