2013-05-05 61 views
0

我在java中創建一個Team應用程序。我想補充一個標誌這是不顯示...還加入未顯示新的球員矩形和圓形的JFrame的,但之後在應用程序的JTextArea新增標誌組合...JTextArea不顯示

這裏是我的代碼。

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.GridLayout; 
import java.awt.Rectangle; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 
import javax.swing.JTextArea; 


public class MainGUI extends JFrame 
{ 
private JLabel teamName; 
private JLabel playerCount; 
private JLabel maxPlayer; 

private JButton createTeam; 
private JButton addOnePlayer; 
private JButton addRemining; 
private JButton exit; 

private Team team; 
private boolean teamCreated; 

private JTextArea text; 
Logo logo; 
public static void main(String[] args) 
{ 

    new MainGUI(); 

} 

public MainGUI() 
{ 
     super.setTitle("Team manager"); 
     super.setSize(500,400); 
     super.setLocation(150,150); 
     super.setLayout(new BorderLayout()); 

     add(addTopPanel(), BorderLayout.NORTH); 
     add(textArea(), BorderLayout.CENTER); 
     add(buttonPanel(), BorderLayout.SOUTH); 
     Logo logo = new Logo(); 
     logo.setBounds(100, 100, 150,150); 
     getContentPane().add(logo); 
     // logo.addSquare(10, 10, 100, 100); 

     super.setVisible(true); 
     super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

} 

private JPanel buttonPanel() 
{ 
    JPanel buttonPanel = new JPanel(); 
    buttonPanel.setLayout(new GridLayout(2,2)); 
    createTeam = new JButton("Create Team"); 
    addOnePlayer = new JButton("Add one player"); 
    addRemining = new JButton("Add remaining players"); 
    exit = new JButton("Exit"); 

    buttonPanel.add(createTeam); 
    buttonPanel.add(addOnePlayer); 
    buttonPanel.add(addRemining); 
    buttonPanel.add(exit); 

    createTeam.addActionListener(new ActionListener() 
    { 
    @Override 
     public void actionPerformed(ActionEvent e) 
     { 
      if(!teamCreated) 
      { 
       teamCreated = true; 
       team = new Team(); 
       teamName.setText("Team name: "+team.getName()); 
       playerCount.setText("Players count: "+team.getCount()); 
       maxPlayer.setText("Max team size: "+team.getSize()); 
      } 
      else 
      { 
       JOptionPane.showMessageDialog(null,"The team has been already created, and no further Team instances are instantiated"); 
      } 

     } 
    }); 

    addOnePlayer.addActionListener(new ActionListener() 
    { 
     @Override 
     public void actionPerformed(ActionEvent e) 
     { 
      if(team != null) 
      { 
       Player pl = Team.createPlayer(); 
       team.addPlayer(pl); 
       playerCount.setText("Players count: "+team.getCount()); 
       text.setText(team.toString()); 
      } 
      else 
      { 
       JOptionPane.showMessageDialog(null,"Create a team first "); 
      } 
     } 
    }); 

    addRemining.addActionListener(new ActionListener() 
    { 
     @Override 
     public void actionPerformed(ActionEvent e) 
     { 
      if(team != null) 
      { 
       team.addPlayers(); 
       playerCount.setText("Players count: "+team.getCount()); 
       text.setText(team.toString()); 
      } 
      else 
      { 
       JOptionPane.showMessageDialog(null,"Create a team first "); 
      } 
     } 
    }); 

    exit.addActionListener(new ActionListener() 
    { 
     @Override 
     public void actionPerformed(ActionEvent e) 
     { 
      System.exit(1); 
     } 
    }); 

    return buttonPanel; 

} 

private JTextArea textArea() 
{ 
    text = new JTextArea(); 

    return text; 
} 

private JPanel addTopPanel() 
{ 
    JPanel top = new JPanel(); 
    top.setLayout(new GridLayout(1,3)); 

    teamName = new JLabel("Team name: "); 
    playerCount = new JLabel("Players count: "); 
    maxPlayer = new JLabel("Max team size: "); 

    top.add(teamName); 
    top.add(playerCount); 
    top.add(maxPlayer); 

    return top; 
} 
class Logo extends JPanel { 
     /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 

    @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 


      g.setColor(Color.orange); 
      g.drawRect(350,80,70,70); 

      g.setColor(Color.blue); 
      g.drawRoundRect(360, 30, 50, 50, 50, 50);; 
     } 
     } 
} 

回答

2

後應用JTextArea中新增標誌未顯示

原因是這一行:

logo.setBounds(100, 100, 150,150); 
    getContentPane().add(logo); 

setBounds與Swing組件的佈局不會工作。所以,當你在的JFramecontainer加入logo它是將它添加到中心,躲藏在中心加入JTextArea

+0

如果我不加logo.setBounds(),則標誌不是框架內顯示。 – Ahmar 2013-05-05 18:12:52

+0

如果你想添加的標誌頂部JFrame'的'左側開出角球,那麼你可以你的'MainGUI'構造函數中使用這樣的:'的JPanel nPanel =新JPanel(); nPanel.setLayout(新的BorderLayout()); nPanel 。新增(標誌,BorderLayout.WEST); nPanel.add(addTopPanel());加(nPanel,BorderLayout.NORTH); ...' – 2013-05-05 18:19:02

+0

我在JPanel中添加的JTextArea解決這個問題,讓大小到JPanel的話,徽標獲得空間。 – Ahmar 2013-05-05 18:29:28