2015-04-30 53 views
1

好吧,我正在使用java swing編程一個GUI,我一直在使用一些舊代碼來創建一個移動的正方形,但是現在我必須使用切換按鈕來使某些形狀出現並從窗口中消失。我的程序應該有4個不同的切換按鈕(目前有3個),並且每個都有特定的用途:名稱將在屏幕中心顯示我的名字,矩形將在屏幕右下角顯示一個矩形,橢圓形也會在右下角顯示一個橢圓形。我曾嘗試使用一系列布爾值來指示出現哪種形狀,但出於某種原因,每個按鈕只會切換矩形,我不知道爲什麼。有什麼我做錯了,導致所有的按鈕做同樣的事情? 這是我的代碼到目前爲止。第一個是我的paintPanel:如何讓每個切換按鈕執行不同的操作?

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class PaintPanel extends JPanel 
{ 
    private static Color[] colors = 
     { Color.RED, Color.BLACK, Color.PINK, Color.ORANGE }; 
    private int colorNumber = 0; 

    private int shape = 0; 

    private int x = 0; 
    private int y = 0; 

    private int width = 100; 
    private int height = 100; 
    private String display = " "; 
    private String none = " "; 

    private int dx = 2; 
    private int dy = 2; 

    private boolean rectangle = true; 
    private boolean oval= true; 
    private boolean Name = true; 
    private boolean special = true; 

    private String displayString = "hello"; 

    private boolean isStarted = true; 

    /*public void update() 
    { 
     if (isStarted) 
     { 
      x += dx; 
      y += dy; 
     } 
     //dx ++; 
     //dy ++; 

     if (y + height > getHeight()) 
     { 
      dy = -Math.abs(dy); 
      shape++; 
      colorNumber = (colorNumber+1)%colors.length; 
     } 
     else if (y < 0) 
     { 
      dy = Math.abs(dy); 
      shape++; 
      colorNumber = (colorNumber+1)%colors.length; 
     } 

     if (x + width > getWidth()) 
     { 
      dx = -Math.abs(dx); 
      shape++; 
      colorNumber = (colorNumber+1)%colors.length; 
     } 
     else if (x < 0) 
     { 
      dx = Math.abs(dx); 
      shape++; 
      colorNumber = (colorNumber+1)%colors.length; 
     } 

    }*/ 
    public void input() 
    { 
     System.out.print("Button"); 
    } 

    public void changeColor() 
    { 
     colorNumber = (colorNumber+1) % colors.length; 
    } 

    /*public void startStop() 
    { 
     //if (isStarted == true) isStarted = false; 
     //else isStarted = true; 

     isStarted = !isStarted; 
    } 
    */ 

    public void setDisplayText(String dt) 
    { 
     displayString = dt; 
    } 


    public void paintComponent(Graphics g) 
    { 
     super.paintComponent(g); 
     int w = getWidth(); 
     int h = getHeight(); 

     int textx = x+w/2; 
     int texty = y+h/2; 

      FontMetrics fm = g.getFontMetrics(); 
      int texth = fm.getHeight(); 
      int textw = fm.stringWidth(display); 

      textx -= textw/2; 
      texty += texth/2; 
      texty -= 5; 
     if(Name == true) 
     { 
      g.setColor(Color.BLACK); 
      g.drawString(display, textx, texty); 
      Name = false; 
     } 
     else if(Name == false) 
     { 
      g.drawString(none, textx, texty); 
      Name = true; 
     } 
     if (oval == true) 
     { 
      g.setColor(Color.CYAN); 
      g.fillOval((5*w)/8, (5*h)/8, w/4, h/4); 
      oval = false; 
     } 
     else if(oval == false) 
     { 
      g.setColor(Color.WHITE); 
      g.fillOval((5*w)/8, (5*h)/8, w/4, h/4); 
      oval = true; 
     } 
     if(rectangle == true) 
     { 
      g.setColor(Color.PINK); 
      g.fillRect(w/2, h/2, w/2, h/2); 
      rectangle = false; 

     } 
     else if(rectangle == false) 
     { 
      g.setColor(Color.WHITE); 
      g.fillRect(w/2, h/2, w/2, h/2); 
      rectangle = true; 
     } 



     /*int w = getWidth(); 
     int h = getHeight(); 
     g.setColor(colors[colorNumber]); 
     if (shape % 2 == 0) //g.fillOval(x, y, width, height); 
     elseg.fillRect(x,y, width, height); 
     */ 
     /* 
     int textx = x+width/2; 
     int texty = y+height/2; 

     FontMetrics fm = g.getFontMetrics(); 
     int texth = fm.getHeight(); 
     int textw = fm.stringWidth(displayString); 

     textx -= textw/2; 
     texty += texth/2; 
     texty -= 5;  

     g.setColor(colors[(colorNumber+1)%colors.length]); 
     g.drawString(displayString, textx, texty); 
     */ 
    } 

} 

無論什麼被註釋掉是因爲它稍後會被實現爲一個特殊的按鈕。 這裏是我的主類:

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class GuiTest extends JFrame 
    implements ActionListener 
{ 
    private Timer frameTimer; 
    private JToggleButton Name; 
    private JToggleButton oval; 
    private JToggleButton rectangle; 
    //private JTextField theText; 
    private PaintPanel paintPanel; 

    public GuiTest() 
    { 
     setTitle("Servando Hernandez"); 
     setSize(500,500); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     JPanel topPanel = new JPanel(); 
     topPanel.setLayout(new GridLayout(1,4)); 

     Name = new JToggleButton("Name"); 
     Name.addActionListener(this); 

     oval = new JToggleButton("Oval"); 
     oval.addActionListener(this); 

     rectangle = new JToggleButton("Rectangle"); 
     rectangle.addActionListener(this); 

     //theText = new JTextField("HI"); 
     //theText.addActionListener(this); 


     topPanel.add(Name); 
     topPanel.add(oval); 
     topPanel.add(rectangle); 
     //topPanel.add(theText); 

     Container contentPane = getContentPane(); 
     contentPane.setLayout(new BorderLayout()); 
     contentPane.add(topPanel, BorderLayout.SOUTH); 

     paintPanel = new PaintPanel(); 
     contentPane.add(paintPanel, BorderLayout.CENTER); 

     //frameTimer = new Timer(50, this); 
     //frameTimer.start(); 
    } 

    public void actionPerformed(ActionEvent e) 
    { 
     //System.out.println("Action performed"); 
     if (e.getSource() == oval)  
     { 
      paintPanel.input(); 
     } 
     /*else if (e.getSource() == frameTimer) 
     { 
      paintPanel.update(); 
     }*/ 
     if (e.getSource() == rectangle) 
     { 
      paintPanel.input(); 
     } 
     if (e.getSource() == oval) 
     { 
      //System.out.println("start/stop"); 
      paintPanel.input(); 
     } 
     if (e.getSource() == Name) 
     { 
      paintPanel.input(); 
      String text = e.getActionCommand(); 
      paintPanel.setDisplayText("Servando Hernandez"); 
      //System.out.println(text); 
     } 

     repaint(); 
    } 

    public static void main(String[] args) 
    { 
     GuiTest gui = new GuiTest(); 
     gui.setVisible(true); 

    } 
} 

回答

3

無論被註釋掉是因爲它後面將一個特殊的按鈕來實現。

那麼,不要在問題中包含代碼。代碼與問題無關,我們不想浪費時間閱讀它或猜測它爲什麼在那裏?

private boolean Name = true; 

爲什麼變量名大寫?其他變量都不以大寫字母開頭(這是正確的)。始終如一!!!

if(Name == true) 
    { 
     g.setColor(Color.BLACK); 
     g.drawString(display, textx, texty); 
     Name = false; 
    } 
    else if(Name == false) 
    { 
     g.drawString(none, textx, texty); 
     Name = true; 
    } 

您正在使if語句複雜化。如果一個布爾變量不是真的,那麼它是假的。只是使用:

if(name == true) 
    //if(name) // even simpler, you don't need the "== true" 
    { 
     g.setColor(Color.BLACK); 
     g.drawString(display, textx, texty); 
    } 
    else 
    { 
     g.drawString(none, textx, texty); 
    } 

我不明白你的ActionListener代碼。無論您點擊哪個按鈕,始終執行paintPanel.input()

請參閱下面的編輯代碼應該是什麼樣的例子。

使用這些建議清理代碼。如果您仍然需要更多幫助,請發佈適當的SSCCE來證明問題。

編輯:

您需要添加屬性到您的PaintPanel類。例如:

public void setOval(Boolean oval) 
{ 
    this.oval = oval; 
    repaint(); 
} 

現在在你ActionListener的代碼如下:

if (e.getSource() == oval) 
{ 
    paintPanel.setOval(oval.isSelected(); 
} 
+0

謝謝你,我一直很魯莽我的代碼,因爲這是我第一次搞GUI。但我很困惑,在修正中,你說明了橢圓==真,但那會做什麼?我的意思是切換按鈕不能用作布爾值。 – Servanh

+0

@Servanh,這是一個錯字,我的意思是說'橢圓=真'。但是,再看一次,即使這是錯誤的。看看我的答案編輯。 – camickr

+0

我現在明白了。這對我很有幫助。非常感謝您的編輯。 – Servanh

2

有大量的在你的代碼混亂的事情。對於初學者來說,每次運行paintComponent()時,都會打開/關閉所有形狀的狀態。您應該從您的actionPerformed()方法處理狀態。例如:

if (e.getSource() == rectangle) { 
    // paintPanel.input(); // what's this doing anyways? 
    paintPanel.setShowRectangle(rectangle.isSelected()); 
} 

在任何情況下,你的其他形狀進行展示,各種原因...

display是一個空字符串。試試看看:

g.drawString("Test", textx, texty); 

您的位置正在將其從面板上拉下來。我改變了它(5:1)只是爲了讓它出現,但你可能會想別的地方:

g.fillOval((1 * w)/8, (1 * h)/8, w/4, h/4); 
+0

1+,太多的問題,以保持對它們的跟蹤... – camickr

相關問題