2015-03-19 16 views
0

我必須創建一個接口,它允許用戶增加/減少一段文本的大小並顯示該文本的當前字體大小值。增加JButton問題的文本字體值

我有兩個按鈕,增加和減少。 我有兩個標籤。一個標籤具有文字「X」,每次按下按鈕時都需要改變尺寸。另一個標籤必須顯示當前字體大小值「X」。

我設法實現了文本的增加/減少方法,但是我無法在單擊後獲得增加文本的值。增加時文本的值只允許用戶增加一次。我希望程序能夠在每次啓動按鈕時將其增加5。

我相信我必須以某種方式存儲字體大小的新值,並使用新的值來讓我增加/減少更多。

如果任何人可以告訴我如何做到這一點,或顯示解決方案,將不勝感激。

package lab3; 
import java.awt.*; 
import java.awt.event.*; 

import javax.swing.*; 

public class FontSize extends JFrame{ 


JButton increase, decrease; 
JLabel sizeX, sizeValue; 


public static void main (String[]args){ 

    FontSize changeFont = new FontSize(); 
    changeFont.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    changeFont.setTitle("Increase/Decrease Font Size"); 
    changeFont.setSize(900,700); 
    changeFont.setVisible(true); 
    changeFont.setLayout(new GridLayout(2,2)); 

} 


public FontSize(){ 

    increase = new JButton("increase"); 
    increase.setBackground(Color.white); 
    increase.setFont(increase.getFont().deriveFont(30.0f)); 
    add(increase); 

    decrease = new JButton("decrease"); 
    decrease.setBackground(Color.white); 
    decrease.setFont(decrease.getFont().deriveFont(30.0f)); 
    add(decrease); 

    sizeX = new JLabel("X", SwingConstants.CENTER); 
    sizeX.setBackground(Color.yellow); 
    sizeX.setFont(sizeX.getFont().deriveFont(30.0f)); 
    add(sizeX); 

    int temp = sizeX.getFont().getSize(); 
    sizeValue = new JLabel("",SwingConstants.CENTER); 
    sizeValue.setText(String.valueOf(temp)); 
    sizeValue.setBackground(Color.yellow); 
    sizeValue.setFont(sizeValue.getFont().deriveFont(30.0f)); 
    add(sizeValue); 

    event e = new event(); 
    increase.addActionListener(e); 
    decrease.addActionListener(e); 


} 
public class event implements ActionListener { 

    public void actionPerformed(ActionEvent e){ 


     String operation = e.getActionCommand(); 
     int temp = sizeX.getFont().getSize(); 
     int temp2 = sizeValue.getFont().getSize(); 


     if(operation.equals("increase")) 
      {      
       temp = temp + 5; 
       sizeX.setFont(new Font("Arial", Font.PLAIN, temp)); 

       temp2 = temp2 + 5; 
       sizeValue.setText(String.valueOf(temp2)); 

      } 
     else if(operation.equals("decrease")) 
      { 
       temp = temp - 5; 
       sizeX.setFont(new Font("Arial", Font.PLAIN, temp)); 

       temp2 = temp2 - 5; 
       sizeValue.setText(String.valueOf(temp2)); 

      } 

     } 
    } 

} 

回答

1

簡單修復真的:在像64代碼,你意外地試圖計數變量temp2作爲它的字體大小,而不是實際的文本。我附加了一個稍微重構的,以及更正的代碼版本。

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

public class FontSize extends JFrame implements ActionListener { 
    private JButton increase, decrease; 
    private JLabel sizeX, sizeValue; 

    public static void main (String[]args) { 
     FontSize changeFont = new FontSize(); 
     changeFont.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     changeFont.setTitle("Increase/Decrease Font Size"); 
     changeFont.setSize(900,700); 
     changeFont.setVisible(true); 
     changeFont.setLayout(new GridLayout(2,2)); 
    } 

    public FontSize(){ 
     increase = new JButton("increase"); 
     increase.setBackground(Color.white); 
     increase.setFont(increase.getFont().deriveFont(30.0f)); 
     add(increase); 

     decrease = new JButton("decrease"); 
     decrease.setBackground(Color.white); 
     decrease.setFont(decrease.getFont().deriveFont(30.0f)); 
     add(decrease); 

     sizeX = new JLabel("X", SwingConstants.CENTER); 
     sizeX.setBackground(Color.yellow); 
     sizeX.setFont(sizeX.getFont().deriveFont(30.0f)); 
     add(sizeX); 

     int temp = sizeX.getFont().getSize(); 
     sizeValue = new JLabel("",SwingConstants.CENTER); 
     sizeValue.setText(String.valueOf(temp)); 
     sizeValue.setBackground(Color.yellow); 
     sizeValue.setFont(sizeValue.getFont().deriveFont(30.0f)); 
     add(sizeValue); 

     increase.addActionListener(this); 
     decrease.addActionListener(this); 
    } 
    public void actionPerformed(ActionEvent e) { 
     String operation = e.getActionCommand(); 
     int temp = sizeX.getFont().getSize(); 
     int temp2 = Integer.parseInt(sizeValue.getText()); 

     if(operation.equals("increase")) {      
      temp += 5; 
      sizeX.setFont(new Font("Arial", Font.PLAIN, temp)); 

      temp2 += 5; 
      sizeValue.setText(String.valueOf(temp2)); 

     } else if(operation.equals("decrease")) { 
      temp -= 5; 
      sizeX.setFont(new Font("Arial", Font.PLAIN, temp)); 

      temp2 -= 5; 
      sizeValue.setText(String.valueOf(temp2)); 
     } 
    } 
} 

希望這對你有所幫助,祝你好運。

0

int temp2 = sizeValue.getFont().getSize();是不是你改變字體的大小,而是它被用來渲染標籤的字體大小。

嘗試使用更多的東西一樣,而不是...

 String operation = e.getActionCommand(); 
     int temp = sizeX.getFont().getSize(); 

     if (operation.equals("increase")) { 
      temp = temp + 5; 
      sizeX.setFont(new Font("Arial", Font.PLAIN, temp)); 

      sizeValue.setText(String.valueOf(temp)); 

     } else if (operation.equals("decrease")) { 
      temp = temp - 5; 
      sizeX.setFont(new Font("Arial", Font.PLAIN, temp)); 

      sizeValue.setText(String.valueOf(temp)); 

     } 

您可能還需要調用revalidate();repaint();actionPerformed方法強制刷新結束,但它沒有工作,沒有問題啊

同樣,你可以只像...

 Font font = sizeX.getFont(); 

     if (operation.equals("increase")) { 
      font = font.deriveFont(font.getSize() + 5f); 
     } else if (operation.equals("decrease")) { 
      font = font.deriveFont(font.getSize() - 5f); 
     } 
     sizeX.setFont(font); 
     sizeValue.setText(NumberFormat.getNumberInstance().format(font.getSize())); 

,讓您保持字體ŧ標籤最初使用的帽子,但增加/減少它的大小,但也依靠實際的Font大小來更新顯示器,而不是依靠計算值...