我有一个GUI,我使用drawString来标记Ellipse2D对象的行。问题是两者都没有像我想要的那样同时显示在同一个tabbedPane上。
问题:为什么会这样?
DrawEllipse.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.util.*;
@SuppressWarnings("serial")
class DrawEllipses extends JPanel {
private static final int OVAL_WIDTH = 30;
private static final Color INACTIVE_COLOR = Color.RED;
private static final Color ACTIVE_COLOR = Color.green;
private java.util.List<Point> points;
private java.util.List<Ellipse2D> ellipses = new ArrayList<>();
private Map<Ellipse2D, Color> ellipseColorMap = new HashMap<>();
/*
* This method is used to populate ellipses with initialized ellipse2D
*/
public DrawEllipses(java.util.List<Point> points) {
JLabel outBound = new JLabel("<html><font size=6>External Port</font></html>");
this.points = points;
for (Point p : points) {
int x = p.x - OVAL_WIDTH / 2;
int y = p.y - OVAL_WIDTH / 2;
int w = OVAL_WIDTH;
int h = OVAL_WIDTH;
Ellipse2D ellipse = new Ellipse2D.Double(x, y, w, h);
ellipses.add(ellipse);
ellipseColorMap.put(ellipse, INACTIVE_COLOR);
}
MyMouseAdapter mListener = new MyMouseAdapter();
addMouseListener(mListener);
addMouseMotionListener(mListener);
add(outBound);
}
/*
* paintComponent is used to paint the ellipses
*/
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
for (Ellipse2D ellipse : ellipses) {
g2.setColor(ellipseColorMap.get(ellipse));
g2.fill(ellipse);
}
}
/*
* MouseAdapter is extended for mousePressed Event that detects if the x, y coordinates
* of a drawn ellipse are clicked. If the color is INACTIVE it is changed to ACTIVE and
* vice versa.
*/
private class MyMouseAdapter extends MouseAdapter {
@Override
/*
* When mousePressed event occurs, the color is toggled between ACTIVE and INACTIVE
*/
public void mousePressed(MouseEvent e) {
Color c = null;
for (Ellipse2D ellipse : ellipses) {
if (ellipse.contains(e.getPoint())) {
c = (ellipseColorMap.get(ellipse) == INACTIVE_COLOR) ? ACTIVE_COLOR : INACTIVE_COLOR;
ellipseColorMap.put(ellipse, c);
}
}
repaint();
}
}
/*
*Used for button click action to change all ellipses to ACTIVE_COLOR
*/
public void activateAll(){
for (Ellipse2D ellipse : ellipses){
ellipseColorMap.put(ellipse, ACTIVE_COLOR);
}
repaint();
}
/*
*Used for button click action to change all ellipses to INACTIVE_COLOR
*/
public void deactivateAll(){
for (Ellipse2D ellipse : ellipses){
ellipseColorMap.put(ellipse, INACTIVE_COLOR);
}
repaint();
}
}
DrawString.java
import javax.swing.*;
import java.awt.*;
class DrawString extends JPanel {
private static final Color STRING_COLOR = Color.BLACK;
public DrawString() {}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(STRING_COLOR);
g.drawString("1", 40, 80);
g.drawString("2", 40, 160);
}
}
答案 0 :(得分:3)
问题可能是您的布局管理器。布局管理器负责设置面板上每个组件的大小/位置。
此外,当您进行自定义绘制时,您需要覆盖每个组件的getPreferredSize()
方法,以便布局管理器可以使用该信息来设置每个组件的大小/位置。如果大小为(0,0)则没有任何东西可以绘画。
阅读Custom Painting上Swing教程中的部分,了解更多信息和工作示例。
如果您需要更多帮助,请发布展示问题的正确SSCCE。您在上一个问题中发布的代码不是SSCCE。我们不需要50分来证明一个概念。您不需要所有操作,因为它们与组件的对齐无关。 SSCCE的目的是简化代码。