Java DrawRect问题
我正在用Java编写一个文件管理器。我需要将矩形选择添加到我的程序中,就像它在windows中工作一样(以便在一个矩形中选择多个文件)。 我的问题是,每当我向DrawRect面板添加布局以放置图标时,我就不能再绘制矩形了! 这是我的DrawRect代码:Java DrawRect问题,java,swing,Java,Swing,我正在用Java编写一个文件管理器。我需要将矩形选择添加到我的程序中,就像它在windows中工作一样(以便在一个矩形中选择多个文件)。 我的问题是,每当我向DrawRect面板添加布局以放置图标时,我就不能再绘制矩形了! 这是我的DrawRect代码: import javax.swing.*; import java.awt.*; import java.awt.event.*; public class DrawRect extends JPanel { int x,
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DrawRect extends JPanel {
int x, y, x2, y2;
// public static void main(String[] args) {
// JFrame f = new JFrame("Draw Box Mouse 2");
// f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// f.setContentPane(new DrawRect());
// f.setSize(300, 300);
// f.setVisible(true);
// }
public DrawRect() {
x = y = x2 = y2 = 0; //
MyMouseListener listener = new MyMouseListener();
addMouseListener(listener);
addMouseMotionListener(listener);
}
public void setStartPoint(int x, int y) {
this.x = x;
this.y = y;
}
public void setEndPoint(int x, int y) {
x2 = (x);
y2 = (y);
}
public void drawPerfectRect(Graphics g, int x, int y, int x2, int y2) {
int px = Math.min(x,x2);
int py = Math.min(y,y2);
int pw=Math.abs(x-x2);
int ph=Math.abs(y-y2);
g.drawRect(px, py, pw, ph);
g.fillRect(px,py,pw,ph);
}
class MyMouseListener extends MouseAdapter {
public void mousePressed(MouseEvent e) {
setStartPoint(e.getX(), e.getY());
}
public void mouseDragged(MouseEvent e) {
setEndPoint(e.getX(), e.getY());
repaint();
}
public void mouseReleased(MouseEvent e) {
setEndPoint(e.getX(), e.getY());
repaint();
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
int alpha = 50; // 50% transparent
Color myColour = new Color(0, 0, 200,50);
g.setColor(myColour);
drawPerfectRect(g, x, y, x2, y2);
}
}
为了进一步解释,在上面的图片中,我有一个拆分窗格,它的右侧是一个滚动窗格,其中一个DrawRect实例被添加到该滚动窗格中,并且图标也被添加到我的DrawRect面板中,每个窗格中有多行网格布局,其中有一个流布局。
我该怎么做才能画出矩形
正如您在下面看到的,如果我不向DrawRect面板添加任何布局,它可以正常工作,但仍然忽略了图标和布局所在的部分:
最后,我有一个问题:在我解决了这个问题之后,我能告诉这个选择矩形中的按钮被选中吗?
非常感谢
===========================================================================
更新:
查看Swing教程中关于如何完成绘制的部分 所发生的情况是,在绘制选择矩形后,将绘制添加到面板的组件
解决方案是使用自定义代码覆盖
绘制(…)
。然后选择矩形将绘制在子组件的顶部。我不明白。我读过你寄给我的东西,但太复杂了,我解决不了。我不知道如何告诉它绘制后来添加到其中的按钮:(根据您最初的问题,我给了您一个简单的建议。建议覆盖“面板”上的paint()
,而不是paintComponent()因此,在按钮绘制之后,选择矩形被绘制。如果没有帮助,那么我不理解这个问题。非常感谢camickr,你救了我!!!它W-O-R-K-S!!!我已经处理了一个星期!!!
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
abstract public class GridIcon extends JButton {
private Color pressedBackgroundColor = Color.blue;
private String shortenedName;
private String path;
private boolean setSelected = false;
public GridIcon(String text, Icon icon, String path) {
this.addActionListener(new ButtonListener());
this.path = path;
if (text.length() > 9) {
shortenedName = text.substring(0, 9);
shortenedName += "...";
} else
shortenedName = text;
this.setIcon(icon);
this.setText(shortenedName);
this.setFocusable(false);
this.setVerticalTextPosition(SwingConstants.BOTTOM);
this.setHorizontalTextPosition(SwingConstants.CENTER);
super.setOpaque(false);
super.setContentAreaFilled(false);
super.setBorderPainted(false);
super.setBorder(null);
this.setBackground(new Color(0, 0, 0, 0));
// super.setPreferredSize(new Dimension(60,60));
}
@Override
protected void paintComponent(Graphics g) {
if (getModel().isPressed() || setSelected) {
g.setColor(pressedBackgroundColor);
} else {
g.setColor(getBackground());
}
g.fillRect(0, 0, getWidth(), getHeight());
super.paintComponent(g);
}
@Override
public void setContentAreaFilled(boolean b) {
}
public String getPath() {
return path;
}
public Color getPressedBackgroundColor() {
return pressedBackgroundColor;
}
public void setPressedBackgroundColor(Color pressedBackgroundColor) {
this.pressedBackgroundColor = pressedBackgroundColor;
}
public void setSetSelected(boolean isSelected) {
setSelected = isSelected;
}
public boolean isSetSelected() {
return setSelected;
}
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (setSelected)
setSelected = false;
else
setSelected = true;
}
}
}