2014-11-23 25 views
1

我做了使用擺動與BorderLayout和中心GridLayout的GUI。我想在我的BorderLayout東邊的JPanel另一個班級中添加一個三角形,但無法顯示。問題得到一個三角圖形上顯示我的JPanel東部的BorderLayout

當我設置了bgcolorJPanel我有一個奇怪的收效甚微,如果你喜歡,你可以看看代碼:gistlink

我有一種感覺,問題是在TriGoButton構造函數,但我我不知道如何進一步測試。我嘗試了paint()的不同變化,但從未能夠看到綠色三角形。


import javax.swing.*; 

import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

@SuppressWarnings("serial") 
public class TestGUI extends JFrame implements ActionListener { 

    private JPanel content; 
    private JTextField placeTxtField; 

    public static void main(String[] args) { 
     TestGUI frame = new TestGUI(); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    @SuppressWarnings("rawtypes") 
    public TestGUI() { 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setLocationRelativeTo(null); 
     content = new JPanel(); 
     content.setLayout(new BorderLayout()); 
     setContentPane(content); 

       // issue 
     JPanel rightPanel = new JPanel(); 
     content.add(rightPanel, BorderLayout.EAST); 
     rightPanel.add(new TriGoButton()); 
       // issue? 

     JPanel leftPanel = new JPanel(); 
     content.add(leftPanel, BorderLayout.WEST); 

     JPanel centerPanel = new JPanel(); 
     content.add(centerPanel, BorderLayout.CENTER); 
     centerPanel.setLayout(new GridLayout(3, 3, 0, 20)); 

     JLabel countyLbl = new JLabel("County"); 
     centerPanel.add(countyLbl); 

     JComboBox countyDropDown = new JComboBox(); 
     centerPanel.add(countyDropDown); 

     JLabel muniLbl = new JLabel("Munipalicity"); 
     centerPanel.add(muniLbl); 

     JComboBox muniDropDown = new JComboBox(); 
     centerPanel.add(muniDropDown); 

     JLabel placeLbl = new JLabel("City or place"); 
     placeLbl.setToolTipText("search"); 
     centerPanel.add(placeLbl); 

     placeTxtField = new JTextField(); 
     centerPanel.add(placeTxtField); 
     placeTxtField.setColumns(15); 
     placeTxtField.setToolTipText("enter w/e"); 

     JPanel bottomPanel = new JPanel(); 
     content.add(bottomPanel, BorderLayout.SOUTH); 

     JButton goBtn = new JButton("Clicky"); 
     bottomPanel.add(goBtn); 
     goBtn.setToolTipText("Please click."); 
     goBtn.addActionListener(this); 

     JPanel topPanel = new JPanel(); 
     content.add(topPanel, BorderLayout.NORTH); 

     JLabel headlineLbl = new JLabel("headline"); 
     topPanel.add(headlineLbl); 

    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 


    } 

} 

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Graphics; 

import javax.swing.JPanel; 

@SuppressWarnings("serial") 
public class TriGoButton extends JPanel { 

    public TriGoButton() { 
     add(new JPanel(), BorderLayout.EAST); 
     setBackground(new Color(100,100,250)); //blue //wtf 
    } 
     public void paint(Graphics g) { 
      super.paint(g); 

      int[]x={90,90,300}; 

      int[]y={150,0,90}; 

      g.setColor(new Color(23,201,10)); //green 
      g.fillPolygon(x,y,3); 
     } 

} 

編輯: ////////////

+1

請張貼相關碼,優選地是[最小,完整的,並且實施例可驗證程序](http://stackoverflow.com/help/mcve ),這裏有你的問題,而不是鏈接。鏈接可能會死亡,鏈接可能包含大型節目,節目太大而無法請求志願者進行審查。您對此要求的遵守情況將不勝感激,並可能幫助您獲得更好更快的幫助。 – 2014-11-23 22:54:55

+0

我也會建議提出一個問題來幫助人們回答。 – Scott 2014-11-23 22:56:59

+0

我已將您的代碼添加到您的問題中,以使其符合網站的規則。 – 2014-11-23 22:59:16

回答

2

我加你的代碼。我認爲你的問題是你的TriGoPanel不覆蓋getPreferredSize,所以它可能會縮小本身。考慮添加到類是這樣的:

@Override 
    public Dimension getPreferredSize() { 
     if (isPreferredSizeSet()) { 
     return super.getPreferredSize(); 
     } 
     return new Dimension(PREF_W, PREF_H); 
    } 

,你有一個整型常量,PREF_W,PREF_H,爲您的寬度和高度尺寸。

__________________________________________________

編輯:和MadProgrammer建議我強烈第二一切!

3
  1. 我不知道你爲什麼要給TriGoButton類增加一個JPanel,但這會導致你的問題。這不是建議你重寫paint
  2. ,這可能會導致沒有問題的結束,因爲父容器並不總是包含在更新時,它的兒童畫。有關更多詳細信息,請參見Painting in AWT and SwingPerforming Custom Painting
  3. BorderLayout將使用組件的preferredSize tomake決定如何它應該大小。您的TriGoButton類應覆蓋getPreferredSize方法並返回適當的默認大小..
+0

1.哦,是的,刪除。 2.我是否壓倒油漆? 3.謝謝! – cashmeer 2014-11-23 23:16:24

+1

@cashmeer:'公共void paint(圖形g){' - 看起來對我來說! – 2014-11-23 23:17:29