gpt4 book ai didi

java - 在 BorderLayout 的 JPanel east 上显示图形三角形的问题

转载 作者:行者123 更新时间:2023-11-29 07:44:31 25 4
gpt4 key购买 nike

我使用 swing 制作了一个 GUI,中间有一个 BorderLayoutGridLayout。我想将我在另一个类中制作的三角形添加到我的 BorderLayout 中的 JPanel 东边,但无法显示它。

当我为 JPanel 设置一个 bgcolor 时,我得到了一个奇怪的小结果,如果你愿意,可以看看代码: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. 我不知道您为什么要将 JPanel 添加到您的 TriGoButton 类中,但这会给您带来问题。
  2. 不建议您覆盖 paint,这可能会导致无休止的问题,因为在绘制子容器时父容器并不总是包含在更新中。参见 Painting in AWT and SwingPerforming Custom Painting了解更多详情。
  3. BorderLayout 将使用组件的 preferredSize 来决定它应该如何调整大小。您的 TriGoButton 类应该覆盖 getPreferredSize 方法并返回适当的默认大小..

关于java - 在 BorderLayout 的 JPanel east 上显示图形三角形的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27095312/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com