2012-10-17 64 views
0

我正在嘗試爲我在網上找到的示例評分GUI添加一個「綠色團隊」。由於某些原因,代碼編譯,但它只與原來的兩個團隊運行。我嘗試過在尺寸/位置上稍微笨拙地玩弄,並且由於這些修改沒有發生變化(ALL沒有變化),我承認我必須缺少一些必要的屬性或其他東西。任何幫助?下面的代碼:Java初學者評分程序按鈕開發

import javax.swing.*; 

import java.awt.Color; 

import java.awt.event.ActionListener; 

import java.awt.event.ActionEvent; 


public class ButtonDemo_Extended3 implements ActionListener{ 



    // Definition of global values and items that are part of the GUI. 

    int redScoreAmount = 0; 

    int blueScoreAmount = 0; 
    int greenScoreAmount = 0; 



    JPanel titlePanel, scorePanel, buttonPanel; 

    JLabel redLabel, blueLabel,greenLabel, redScore, blueScore, greenScore; 

    JButton redButton, blueButton, greenButton,resetButton; 



    public JPanel createContentPane(){ 



     // We create a bottom JPanel to place everything on. 

     JPanel totalGUI = new JPanel(); 

     totalGUI.setLayout(null); 



     // Creation of a Panel to contain the title labels 

     titlePanel = new JPanel(); 

     titlePanel.setLayout(null); 

     titlePanel.setLocation(0, 0); 

     titlePanel.setSize(500, 500); 

     totalGUI.add(titlePanel); 



     redLabel = new JLabel("Red Team"); 

     redLabel.setLocation(300, 0); 

     redLabel.setSize(100, 30); 

     redLabel.setHorizontalAlignment(0); 

     redLabel.setForeground(Color.red); 

     titlePanel.add(redLabel); 



     blueLabel = new JLabel("Blue Team"); 

     blueLabel.setLocation(900, 0); 

     blueLabel.setSize(100, 30); 

     blueLabel.setHorizontalAlignment(0); 

     blueLabel.setForeground(Color.blue); 

     titlePanel.add(blueLabel); 

    greenLabel = new JLabel("Green Team"); 

     greenLabel.setLocation(600, 0); 

     greenLabel.setSize(100, 30); 

     greenLabel.setHorizontalAlignment(0); 

     greenLabel.setForeground(Color.green); 

     titlePanel.add(greenLabel); 



     // Creation of a Panel to contain the score labels. 

     scorePanel = new JPanel(); 

     scorePanel.setLayout(null); 

     scorePanel.setLocation(10, 40); 

     scorePanel.setSize(500, 30); 

     totalGUI.add(scorePanel); 



     redScore = new JLabel(""+redScoreAmount); 

     redScore.setLocation(0, 0); 

     redScore.setSize(40, 30); 

     redScore.setHorizontalAlignment(0); 

     scorePanel.add(redScore); 

    greenScore = new JLabel(""+greenScoreAmount); 

     greenScore.setLocation(60, 0); 

     greenScore.setSize(40, 30); 

     greenScore.setHorizontalAlignment(0); 

     scorePanel.add(greenScore); 



     blueScore = new JLabel(""+blueScoreAmount); 

     blueScore.setLocation(130, 0); 

     blueScore.setSize(40, 30); 

     blueScore.setHorizontalAlignment(0); 

     scorePanel.add(blueScore); 



     // Creation of a Panel to contain all the JButtons. 

     buttonPanel = new JPanel(); 

     buttonPanel.setLayout(null); 

     buttonPanel.setLocation(10, 80); 

     buttonPanel.setSize(2600, 70); 

     totalGUI.add(buttonPanel); 



     // We create a button and manipulate it using the syntax we have 

     // used before. Now each button has an ActionListener which posts 

     // its action out when the button is pressed. 

     redButton = new JButton("Red Score!"); 

     redButton.setLocation(0, 0); 

     redButton.setSize(30, 30); 

     redButton.addActionListener(this); 

     buttonPanel.add(redButton); 



     blueButton = new JButton("Blue Score!"); 

     blueButton.setLocation(150, 0); 

     blueButton.setSize(30, 30); 

     blueButton.addActionListener(this); 

     buttonPanel.add(blueButton); 

    greenButton = new JButton("Green Score!"); 

     greenButton.setLocation(250, 0); 

     greenButton.setSize(30, 30); 

     greenButton.addActionListener(this); 

     buttonPanel.add(greenButton); 



     resetButton = new JButton("Reset Score"); 

     resetButton.setLocation(0, 100); 

     resetButton.setSize(50, 30); 

     resetButton.addActionListener(this); 

     buttonPanel.add(resetButton); 



     totalGUI.setOpaque(true); 

     return totalGUI; 

    } 



    // This is the new ActionPerformed Method. 

    // It catches any events with an ActionListener attached. 

    // Using an if statement, we can determine which button was pressed 

    // and change the appropriate values in our GUI. 

    public void actionPerformed(ActionEvent e) { 

     if(e.getSource() == redButton) 

     { 

      redScoreAmount = redScoreAmount + 1; 

      redScore.setText(""+redScoreAmount); 

     } 

     else if(e.getSource() == blueButton) 

     { 

      blueScoreAmount = blueScoreAmount + 1; 

      blueScore.setText(""+blueScoreAmount); 

     } 
    else if(e.getSource() == greenButton) 

     { 

      greenScoreAmount = greenScoreAmount + 1; 

      greenScore.setText(""+greenScoreAmount); 

     } 

     else if(e.getSource() == resetButton) 

     { 

      redScoreAmount = 0; 

      blueScoreAmount = 0; 
     greenScoreAmount = 0; 

      redScore.setText(""+redScoreAmount); 

      blueScore.setText(""+blueScoreAmount); 
     greenScore.setText(""+greenScoreAmount); 

     } 

    } 



    private static void createAndShowGUI() { 



     JFrame.setDefaultLookAndFeelDecorated(true); 

     JFrame frame = new JFrame("[=] JButton Scores! [=]"); 



     //Create and set up the content pane. 

     ButtonDemo_Extended demo = new ButtonDemo_Extended(); 

     frame.setContentPane(demo.createContentPane()); 



     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     frame.setSize(1024, 768); 

     frame.setVisible(true); 

    } 



    public static void main(String[] args) { 

     //Schedule a job for the event-dispatching thread: 

     //creating and showing this application's GUI. 

     SwingUtilities.invokeLater(new Runnable() { 

      public void run() { 

       createAndShowGUI(); 

      } 

     }); 

    } 

} 
+0

空佈局可能是問題的一部分,至少... – DNA

回答

0

如果我運行你的代碼修改,我只看到了「紅隊」的標籤,以及一些按鈕這是太小,無法閱讀文字(其中有些只有當鼠標滑過出現) 。

如果你註釋掉的所有空佈局:

//buttonPanel.setLayout(null); 

那麼所有的三個按鈕和標籤顯示正常。

沒有佈局管理器並使用絕對定位是可能的(請參閱Java Swing tutorial頁面上的確切主題),但通常不推薦。有關Swing教程的Laying Out Components Within a Container課程中使用佈局管理器的大量信息。

1
import javax.swing.*; 

import java.awt.Color; 
import java.awt.FlowLayout; 

import java.awt.event.ActionListener; 

import java.awt.event.ActionEvent; 


public class ButtonDemo_Extended3 implements ActionListener{ 



// Definition of global values and items that are part of the GUI. 

int redScoreAmount = 0; 

int blueScoreAmount = 0; 
int greenScoreAmount = 0; 



JPanel titlePanel, scorePanel, buttonPanel; 

JLabel redLabel, blueLabel,greenLabel, redScore, blueScore, greenScore; 

JButton redButton, blueButton, greenButton,resetButton; 



public JPanel createContentPane(){ 



    // We create a bottom JPanel to place everything on. 

    JPanel totalGUI = new JPanel(); 

    totalGUI.setLayout(null); 



    // Creation of a Panel to contain the title labels 

    titlePanel = new JPanel(); 

    titlePanel.setLayout(new FlowLayout()); 

    titlePanel.setLocation(0, 0); 

    titlePanel.setSize(500, 500); 





    redLabel = new JLabel("Red Team"); 

    redLabel.setLocation(300, 0); 

    redLabel.setSize(100, 30); 

    redLabel.setHorizontalAlignment(0); 

    redLabel.setForeground(Color.red); 

    titlePanel.add(redLabel, 0); 



    blueLabel = new JLabel("Blue Team"); 

    blueLabel.setLocation(900, 0); 

    blueLabel.setSize(100, 30); 

    blueLabel.setHorizontalAlignment(0); 

    blueLabel.setForeground(Color.blue); 

    titlePanel.add(blueLabel, 1); 

greenLabel = new JLabel("Green Team"); 

    greenLabel.setLocation(600, 0); 

    greenLabel.setSize(100, 30); 

    greenLabel.setHorizontalAlignment(0); 

    greenLabel.setForeground(Color.green); 

    titlePanel.add(greenLabel); 



    // Creation of a Panel to contain the score labels. 

    scorePanel = new JPanel(); 

    scorePanel.setLayout(null); 

    scorePanel.setLocation(10, 40); 

    scorePanel.setSize(500, 30); 





    redScore = new JLabel(""+redScoreAmount); 

    redScore.setLocation(0, 0); 

    redScore.setSize(40, 30); 

    redScore.setHorizontalAlignment(0); 

    scorePanel.add(redScore); 

greenScore = new JLabel(""+greenScoreAmount); 

    greenScore.setLocation(60, 0); 

    greenScore.setSize(40, 30); 

    greenScore.setHorizontalAlignment(0); 

    scorePanel.add(greenScore); 



    blueScore = new JLabel(""+blueScoreAmount); 

    blueScore.setLocation(130, 0); 

    blueScore.setSize(40, 30); 

    blueScore.setHorizontalAlignment(0); 

    scorePanel.add(blueScore); 



    // Creation of a Panel to contain all the JButtons. 

    buttonPanel = new JPanel(); 

    buttonPanel.setLayout(null); 

    buttonPanel.setLocation(10, 80); 

    buttonPanel.setSize(2600, 70); 





    // We create a button and manipulate it using the syntax we have 

    // used before. Now each button has an ActionListener which posts 

    // its action out when the button is pressed. 

    redButton = new JButton("Red Score!"); 

    redButton.setLocation(0, 0); 

    redButton.setSize(30, 30); 

    redButton.addActionListener(this); 

    buttonPanel.add(redButton); 



    blueButton = new JButton("Blue Score!"); 

    blueButton.setLocation(150, 0); 

    blueButton.setSize(30, 30); 

    blueButton.addActionListener(this); 

    buttonPanel.add(blueButton); 

greenButton = new JButton("Green Score!"); 

    greenButton.setLocation(250, 0); 

    greenButton.setSize(30, 30); 

    greenButton.addActionListener(this); 

    buttonPanel.add(greenButton); 



    resetButton = new JButton("Reset Score"); 

    resetButton.setLocation(0, 100); 

    resetButton.setSize(50, 30); 

    resetButton.addActionListener(this); 

    buttonPanel.add(resetButton); 



    totalGUI.setOpaque(true); 

    totalGUI.add(buttonPanel); 
    totalGUI.add(scorePanel); 
    totalGUI.add(titlePanel); 

    return totalGUI; 

} 



// This is the new ActionPerformed Method. 

// It catches any events with an ActionListener attached. 

// Using an if statement, we can determine which button was pressed 

// and change the appropriate values in our GUI. 

public void actionPerformed(ActionEvent e) { 

    if(e.getSource() == redButton) 

    { 

     redScoreAmount = redScoreAmount + 1; 

     redScore.setText(""+redScoreAmount); 

    } 

    else if(e.getSource() == blueButton) 

    { 

     blueScoreAmount = blueScoreAmount + 1; 

     blueScore.setText(""+blueScoreAmount); 

    } 
else if(e.getSource() == greenButton) 

    { 

     greenScoreAmount = greenScoreAmount + 1; 

     greenScore.setText(""+greenScoreAmount); 

    } 

    else if(e.getSource() == resetButton) 

    { 

     redScoreAmount = 0; 

     blueScoreAmount = 0; 
    greenScoreAmount = 0; 

     redScore.setText(""+redScoreAmount); 

     blueScore.setText(""+blueScoreAmount); 
    greenScore.setText(""+greenScoreAmount); 

    } 

} 



private static void createAndShowGUI() { 



    JFrame.setDefaultLookAndFeelDecorated(true); 

    JFrame frame = new JFrame("[=] JButton Scores! [=]"); 



    //Create and set up the content pane. 

    ButtonDemo_Extended3 demo = new ButtonDemo_Extended3(); 

    frame.setContentPane(demo.createContentPane()); 



    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    frame.setSize(1024, 768); 

    frame.setVisible(true); 

} 



public static void main(String[] args) { 

    //Schedule a job for the event-dispatching thread: 

    //creating and showing this application's GUI. 

    SwingUtilities.invokeLater(new Runnable() { 

     public void run() { 

      createAndShowGUI(); 

     } 

    }); 

} 

} 

您必須使用佈局管理器才能顯示您的小部件。在這種情況下,我使用了FlowLayout()。另外,請確保先在面板中添加元素,然後將面板添加到其父面板。

現在,代碼正常工作,但您應該再次使用特定的佈局來排列框架內的面板。