2014-04-06 79 views
1

我有一個JButton,當我調用setText()時,文本不會更新。但只要我將鼠標懸停在按鈕上,它就會更新。我該如何解決這個問題,以便在我調用setText()時更新按鈕?我只會發布美國東部時間所需的方法。它就會按照順序:)Java JButton不更新

public void mouseClicked(MouseEvent e) {//This is a differn't event. This is not executed by clicking on the same button. It's a differn't one. 
      click(new Point(e.getX() + gridScrollPaneViewport.getViewPosition().x, e.getY() + gridScrollPaneViewport.getViewPosition().y)); 
     } 

public void click(Point point) { 
       map.selectPlot((PlotGUI) map.getComponentAt(point)); 
      } 
public void selectPlot(PlotGUI plot) { 
     ChickenTycoon.getInstance().getScreen().getPlayingScreen().sideBar 
       .setPlot(plot); 
     selectedPlot = plot; 
    } 

public void setPlot(PlotGUI plot) { 
     title.setText(plot.getName() + plot.getX() + plot.getY()); 
     for (Component comp : stuffPanel.getComponents()) { 
      stuffPanel.remove(comp); 
     } 
     for (Component comp : plot.getComponentList()) { 
      stuffPanel.add(comp); 
     } 
     update(); 
    } 

public void update() { 
     if (ChickenTycoon.getInstance().getScreen().getPlayingScreen().map 
       .getSelectedPlot() != null) { 
      ChickenTycoon.getInstance().getScreen().getPlayingScreen().map 
        .getSelectedPlot().update(); 
     } 
    } 

public void update() { 
     Log.m("Updating LandGUI"); 
     ((CButton) componentList[0]).setText("Buy for $ "//I know I am casting this to a CButton, it's my custom version of JButton. I tried it with a JButton and the same thing happens. 
       + ((Land) parentPlot).getPurchasePrice()); 
    } 

CButton.java

package gui; 

import game.GameConfiguration; 

import java.awt.BasicStroke; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Font; 
import java.awt.FontMetrics; 
import java.awt.Frame; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.Insets; 
import java.awt.RenderingHints; 
import java.awt.Shape; 
import java.awt.Stroke; 
import java.awt.event.ComponentEvent; 
import java.awt.event.ComponentListener; 
import java.awt.geom.Area; 
import java.awt.geom.Rectangle2D; 
import java.awt.geom.RoundRectangle2D; 

import javax.swing.JButton; 
import javax.swing.JOptionPane; 
import javax.swing.SwingUtilities; 

import chicken.GlobalConfiguration; 

public class CButton extends JButton implements ComponentListener { 
    protected static final int BORDER_WIDTH = 5; 
    private static final Font font = GlobalConfiguration.font; 
    private static final Insets INSETS_MARGIN = new Insets(2, 5, 2, 5); 

    private static final long serialVersionUID = 1L; 
    protected Area m_areaDraw = null; 
    private Area m_areaFill = null; 

    private double m_dHeightDraw = 0d; 
    private double m_dHeightFill = 0d; 
    private double m_dWidthDraw = 0d; 

    private double m_dWidthFill = 0d; 
    private int m_nMinHeight = 0; 
    private int m_nMinWidth = 0; 

    private int m_nStringHeightMax = 0; 
    private int m_nStringWidthMax = 0; 
    private RoundRectangle2D m_rrect2dDraw = null; 
    private RoundRectangle2D m_rrect2dFill = null; 
    private Shape m_shape = null; 

    public CButton(String strLabel) { 
     setContentAreaFilled(false); 
     setMargin(CButton.INSETS_MARGIN); 
     setFocusPainted(false); 
     addComponentListener(this); 
     setFont(CButton.font); 
     setText(strLabel); 
    } 

    @Override 
    public void componentHidden(ComponentEvent e) { 
    } 

    @Override 
    public void componentMoved(ComponentEvent e) { 
    } 

    // Needed if we want this button to resize 
    @Override 
    public void componentResized(ComponentEvent e) { 
     m_shape = new Rectangle2D.Float(0, 0, getBounds().width, 
       getBounds().height); 
     m_dWidthFill = (double) getBounds().width - 1; 
     m_dHeightFill = (double) getBounds().height - 1; 
     m_dWidthDraw = ((double) getBounds().width - 1) 
       - (CButton.BORDER_WIDTH - 1); 
     m_dHeightDraw = ((double) getBounds().height - 1) 
       - (CButton.BORDER_WIDTH - 1); 
     setShape(); 
     repaint(); 
    } 

    @Override 
    public void componentShown(ComponentEvent e) { 
    } 

    @Override 
    public boolean contains(int nX, int nY) { 
     if ((null == m_shape) || m_shape.getBounds().equals(getBounds())) { 
      m_shape = new Rectangle2D.Float(0, 0, this.getBounds().width, 
        this.getBounds().height); 
     } 
     return m_shape.contains(nX, nY); 
    } 

    @Override 
    public void paintBorder(Graphics g) { 
     Graphics2D g2 = (Graphics2D) g; 
     RenderingHints hints = new RenderingHints(
       RenderingHints.KEY_ANTIALIASING, 
       RenderingHints.VALUE_ANTIALIAS_ON); 
     g2.setRenderingHints(hints); 
     g2.setColor(Color.black); 
     Stroke strokeOld = g2.getStroke(); 
     g2.setStroke(new BasicStroke(CButton.BORDER_WIDTH, 
       BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); 
     g2.draw(m_areaDraw); 

     if (getModel().isRollover()) { 
      g2.setColor(Color.GRAY); 
      g2.draw(m_areaDraw); 
     } 
     g2.setStroke(strokeOld); 
    }; 

    @Override 
    public void paintComponent(Graphics g) { 
     Graphics2D g2 = (Graphics2D) g; 
     RenderingHints hints = new RenderingHints(
       RenderingHints.KEY_ANTIALIASING, 
       RenderingHints.VALUE_ANTIALIAS_ON); 
     g2.setRenderingHints(hints); 
     if (getModel().isArmed()) { 
      g2.setColor(Color.CYAN.darker()); 
     } else { 
      g2.setColor(Color.CYAN); 
     } 
     g2.fill(m_areaFill); 
     super.paintComponent(g2); 
    } 

    private void setShape() { 
     // Area 
     double dArcLengthFill = Math.min(m_dWidthFill, m_dHeightFill); 
     m_rrect2dFill = new RoundRectangle2D.Double(0d, 0d, m_dWidthFill, 
       m_dHeightFill, dArcLengthFill, dArcLengthFill); 
     // WARNING: arclength and archeight are divided by 2 
     // when they get into the roundedrectangle shape 
     m_areaFill = new Area(m_rrect2dFill); 
     // Border 
     double dArcLengthDraw = Math.min(m_dWidthDraw, m_dHeightDraw); 
     m_rrect2dDraw = new RoundRectangle2D.Double(
       (CButton.BORDER_WIDTH - 1)/2, (CButton.BORDER_WIDTH - 1)/2, 
       m_dWidthDraw, m_dHeightDraw, dArcLengthDraw, dArcLengthDraw); 
     m_areaDraw = new Area(m_rrect2dDraw); 
    } 

    @Override 
    public void setText(final String strText) { 
     CButton.super.setText(strText); 
     Frame frame = JOptionPane.getRootFrame(); 
     FontMetrics fm = frame.getFontMetrics(font); 
     m_nStringWidthMax = fm.stringWidth(getText()); 
     m_nStringWidthMax = Math.max(m_nStringWidthMax, 
       fm.stringWidth(getText())); 
     // WARNING: use getMargin. it refers to dist btwn text and border. 
     // Also use getInsets. it refers to the width of the border 
     int nWidth = Math.max(m_nMinWidth, m_nStringWidthMax + getMargin().left 
       + getInsets().left + getMargin().right + getInsets().right); 
     m_nStringHeightMax = fm.getHeight(); 
     // WARNING: use getMargin. it refers to dist btwn text and border. 
     // Also use getInsets. it refers to the width of the border 
     int nHeight = Math.max(m_nMinHeight, m_nStringHeightMax 
       + getMargin().left + getInsets().left + getMargin().right 
       + getInsets().right); 
     setPreferredSize(new Dimension(
       nWidth + ((2 * getFont().getSize())/5), nHeight 
         + ((2 * getFont().getSize())/5))); 
     // Set the initial draw and fill dimensions 
     setShape(); 
    } 
} 
+0

PLZ顯示你如何編碼。 – tana

+1

要修復它,您必須修復代碼中的錯誤。爲了我們的幫助,我們必須***看到這些錯誤。我們看到的代碼沒有顯示,理解沒有解釋的概念的能力被大大高估了。 –

+0

讓我看看我能否解開所有正在發生的事情,它是從我知道的EDT運行。 –

回答

0

我固定它。我需要爲該按鈕的容器添加重新繪製。就那麼簡單!

button.setText("Some Text"); 
buttonContainer.repaint();