當我按下自由按鈕時,我可以在底部窗格中繪制,但是當我按下矩形按鈕時,我可以在面板中繪制一個矩形,但我也可以同時繪制任何東西。我的目標是,當我按下其中一個功能時,我希望另一個功能停止運行。
public class MyFrame extends JFrame implements ActionListener, MouseListener , MouseMotionListener{
JPanel top;
JPanel bottom ;
JButton btn1,btn4;
int freebtn,rectanglebtn;
Graphics g;
public MyFrame(){
this.setTitle("car");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(700,700);
this.setLayout(null);
this.setResizable(false);
top = new JPanel();
bottom = new JPanel();
top.setBounds(0, 0, 700, 100);
top.setBackground(Color.blue);
bottom.setBounds(0, 100, 700, 600);
bottom.setBackground(Color.white);
btn1 = new JButton("free");
btn4 = new JButton("rectangle");
btn1.addActionListener(this);
btn4.addActionListener(this);
top.add(btn1);top.add(btn4);
this.add(top);
this.add(bottom);
bottom.addMouseMotionListener(this);
bottom.addMouseListener(this);
this.setVisible(true);
g=bottom.getGraphics();
}
// action listener-------------------------------
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand() =="free") {
System.out.println("free button");
freebtn=11;
}
else if(e.getActionCommand() =="rectangle") {
System.out.println("rectangle button");
rectanglebtn=12;
}
}
// mouse listener-----------------------------
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println(rectanglebtn);
if(rectanglebtn==12) {
g.setColor(Color.BLUE);
g.drawRect(e.getX(),e.getY(), 100, 50);
}
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
// mouse motion listener---------------------------
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println(freebtn);
if(freebtn==11) {
g.setColor(Color.BLUE);
g.fillOval(e.getX(),e.getY(),20,20);
}
}
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
}
}
當我使用另一個時,我試圖將其中一個設定為空,但它沒有用
uj5u.com熱心網友回復:
讓我們從...
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand() =="free") {
System.out.println("free button");
freebtn=11;
}
else if(e.getActionCommand() =="rectangle") {
System.out.println("rectangle button");
rectanglebtn=12;
}
}
除了您使用e.getActionCommand() =="rectangle"而不是"rectangle".equals(e.getActionCommand())(有關更多詳細資訊,請參閱如何在 Java 中比較字串?)之外,一旦操作freebtn或rectanglebtn操作,它們設定的值永遠不會改變。
因此,例如,如果我按下rectangle按鈕,rectanglebtn則設定為12. 如果我再按下free按鈕,rectanglebtn仍然是12
您應該使用單個“狀態”變數,而不是使用兩個不同(并且有些不相關)的變數。現在,如果您有兩個以上的狀態,我會使用 a enum,例如...
enum State {
FREE, RECTANGLE, CIRCLE
}
然后將動作處理程式更新為...
private State state = null;
// action listener-------------------------------
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if ("free".equals(e.getActionCommand())) {
System.out.println("free button");
state = State.FREE;
} else if ("rectangle".equals(e.getActionCommand())) {
System.out.println("rectangle button");
state = State.RECTANGLE;
}
}
現在,每當操作任一按鈕時,都會將其state更改為單個狀態。
然后你的滑鼠監聽器開始看起來更像......
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println(state);
if (state == State.RECTANGLE) {
g.setColor(Color.BLUE);
g.drawRect(e.getX(), e.getY(), 100, 50);
}
}
// mouse motion listener---------------------------
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println(state);
if (state == State.FREE) {
g.setColor(Color.BLUE);
g.fillOval(e.getX(), e.getY(), 20, 20);
}
}
并且它們不可能交叉連接。
現在,關于“其他”問題......g = bottom.getGraphics();不是自定義繪畫的作業方式。充其量,這將回傳最后繪制的“快照”,而在最壞的情況下,它將回傳null。
您需要查看在 AWT 中的繪畫和 Swing和執行自定義繪畫,以更好地了解 Swing 中的繪畫如何作業以及您應該如何使用它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/516704.html
下一篇:java.awt.IllegalComponentStateException:組件必須在螢屏上顯示以確定其位置
