1
點擊JComboBox後,JComboBox會用灰色矩形形狀的東西來掩蓋繪畫的某些部分,代碼中是否有錯誤,以及如何修復它?謝謝!JComboBox涵蓋其他東西?
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.Box;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class MouseButtonTester extends JFrame implements MouseMotionListener{
int x,y,r;
JComboBox colorChooser;
Color color;
JTextField red = new JTextField();
JTextField green = new JTextField();
JTextField blue = new JTextField();
JPanel topPanel = new JPanel();
JComboBox pen;
int fillKind;
Object[] chooseRGB = {
"Red: ", red,
"Green: ", green,
"Blue: ", blue
};
public MouseButtonTester(){
super();
this.addMouseMotionListener(this);
setResizable(true);
setLayout(new BorderLayout());
add(topPanel,BorderLayout.NORTH);
topPanel.setLayout(new GridLayout(1,2));
colorChooser = new JComboBox();
pen = new JComboBox();
topPanel.add(pen);
topPanel.add(colorChooser);
colorChooser.setBackground(Color.WHITE);
pen.setBackground(Color.WHITE);
pen.addItem("Pen");
pen.addItem("Marker");
pen.addItem("Highlighter");
pen.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(pen.getSelectedItem().toString().equals("Pen")){
fillKind = 0;
r = 8;
}else if(pen.getSelectedItem().toString().equals("Marker")){
fillKind = 0;
r = 15;
}else if(pen.getSelectedItem().toString().equals("Highlighter")){
fillKind = 1;
}
}
});
colorChooser.setFont(new Font("Serif",Font.PLAIN,14));
colorChooser.addItem("Red");
colorChooser.addItem("Orange");
colorChooser.addItem("Yellow");
colorChooser.addItem("Green");
colorChooser.addItem("Blue");
colorChooser.addItem("Violet");
colorChooser.addItem("Purple");
colorChooser.addItem("Choose RGB");
colorChooser.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if("Red" == colorChooser.getSelectedItem().toString()){
color = Color.RED;
}else if("Orange" == colorChooser.getSelectedItem().toString()){
color = Color.ORANGE;
}else if("Yellow" == colorChooser.getSelectedItem().toString()){
color = Color.YELLOW;
}
else if("Green" == colorChooser.getSelectedItem().toString()){
color = Color.GREEN;
}
else if("Blue" == colorChooser.getSelectedItem().toString()){
color = Color.BLUE;
}
else if("Violet" == colorChooser.getSelectedItem().toString()){
color = new Color(180,0,200);
}
else if("Purple" == colorChooser.getSelectedItem().toString()){
color = new Color(150,0,200);
}
else if("Purple" == colorChooser.getSelectedItem().toString()){
}
else if("Choose RGB" == colorChooser.getSelectedItem().toString()){
int option = JOptionPane.showConfirmDialog(null, chooseRGB, "Choose RGB", JOptionPane.OK_CANCEL_OPTION);
if(option == JOptionPane.OK_OPTION){
int redValue = Integer.parseInt(red.getText());
int greenValue = Integer.parseInt(green.getText());
int blueValue = Integer.parseInt(blue.getText());
color = new Color(redValue,greenValue,blueValue);
}
}
}
});
}
Graphics graphics;
public void paint(Graphics g){
graphics = g.create();
}
public void mouseDragged(MouseEvent e) {
x = e.getX();
y = e.getY();
graphics.setColor(color);
if(fillKind == 0){
graphics.fillOval(x, y, r, r);
}else if(fillKind == 1){
graphics.fillRect(x, y, 10, 25);
}
repaint();
}
public void mouseMoved(MouseEvent arg0) {
//No actions
}
}
猜測:如果在將該框添加到其擁有的面板之前完全配置組合框,它是否有所作爲? – GhostCat
這不完整;沒有'main()',當我添加一個'main()'並運行它時,它會產生一個摺疊的窗口,不像你在圖像中給出的窗口。所以無論產生那個圖像,它都不是這個代碼。 – arcy
再看一遍,我看不到任何代碼來處理組合框下拉框遮住部分面板並重新繪製的情況。你將需要這樣做的代碼。有一個事件(我忘記了哪一個)通知你的面板有什麼東西遮蔽了它,矩形座標已經被遮住了,並且由你的面板重新繪製該部分。 – arcy