我正在使用Java中的簡單繪圖板工作,並且我有幾個用戶可以使用的工具:鋼筆和橡皮擦。但用於選擇工具的JComboBox不會出現,即使已添加。我聽說定製的油漆會掩蓋任何JComponents。如果這是真的,我怎樣才能使JComboBox始終位於頂端?爲什麼我的JComboBox沒有畫在我的圖畫上?
圖片窗口:
這裏是我的代碼:
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import javax.swing.JComboBox;
import javax.swing.JFrame;
public class DrawFrame extends JFrame implements MouseMotionListener, Runnable {
private static final long serialVersionUID = 1L;
private Image dbi;
private Graphics dbg;
ArrayList<Integer> px = new ArrayList<Integer>();
ArrayList<Integer> py = new ArrayList<Integer>();
int mx;
int my;
@SuppressWarnings("rawtypes")
JComboBox tool = new JComboBox();
@SuppressWarnings("unchecked")
public DrawFrame() {
setSize(480, 480);
setTitle("ScratchPad _14");
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
setBackground(Color.WHITE);
setOpacity(1);
tool.addItem("Pen");
tool.addItem("Eraser");
tool.setEnabled(true);
add(tool);
setVisible(true);
addMouseMotionListener(this);
}
/*@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}*/
public static void main(String[] args) {
// TODO Auto-generated method stub
DrawFrame df = new DrawFrame();
Thread t = new Thread(df);
t.start();
}
public void paint(Graphics g) {
dbi = createImage(getWidth(), getHeight());
dbg = dbi.getGraphics();
draw(dbg);
g.drawImage(dbi, 0, 0, null);
}
private void draw(Graphics g) {
// TODO Auto-generated method stub
super.paint(g);
g.setColor(getBackground());
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.BLACK);
for (int i = 0; i < px.size(); i++) {
g.fillOval(px.get(i), py.get(i), 3, 3);
}
g.setColor(Color.BLUE);
g.drawString("(" + mx + ", " + my + ")", mx + 10, my - 5);
repaint();
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true) {
try {
Thread.sleep(5);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void mouseDragged(MouseEvent m) {
// TODO Auto-generated method stub
int x = m.getX();
int y = m.getY();
mx = x;
my = y;
px.add(x);
py.add(y);
}
@Override
public void mouseMoved(MouseEvent m) {
// TODO Auto-generated method stub
int x = m.getX();
int y = m.getY();
mx = x;
my = y;
}
}
提前感謝!
刪除'super.paint(g)'不會顯示任何內容。而框架是雙重緩衝的。 –
永遠不要停止調用super.paint,這不是問題。問題是,'super.paint'繪製了子組件,但是然後你畫了它們,是的,你已經對框架進行了雙重緩衝,但是猜測是什麼,JComponent是雙重緩衝的默認值,讓你不必自己做這件事。一個JFrame由一個JRootPane,JLayeredPane,內容窗格和玻璃窗格組成,並且您剛剛繪製過很多內容。如果你想解決問題,分開你的自定義繪畫和組件 – MadProgrammer
在eclipse中,不是手動雙緩衝會導致繪圖閃爍,屏幕上的項目太多。 –