我做了使用擺動與BorderLayout
和中心GridLayout
的GUI。我想在我的BorderLayout
東邊的JPanel
另一個班級中添加一個三角形,但無法顯示。問題得到一個三角圖形上顯示我的JPanel東部的BorderLayout
當我設置了bgcolor
說JPanel
我有一個奇怪的收效甚微,如果你喜歡,你可以看看代碼: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);
}
}
編輯: ////////////
請張貼相關碼,優選地是[最小,完整的,並且實施例可驗證程序](http://stackoverflow.com/help/mcve ),這裏有你的問題,而不是鏈接。鏈接可能會死亡,鏈接可能包含大型節目,節目太大而無法請求志願者進行審查。您對此要求的遵守情況將不勝感激,並可能幫助您獲得更好更快的幫助。 – 2014-11-23 22:54:55
我也會建議提出一個問題來幫助人們回答。 – Scott 2014-11-23 22:56:59
我已將您的代碼添加到您的問題中,以使其符合網站的規則。 – 2014-11-23 22:59:16