0

I have to create an interface which allows the user to increase/decrease the size of a piece of text and to show the current font size value of that text.

I have two buttons, increase and decrease. I have two labels. One label has the text "X" which needs to change size every time a button is pressed. The other label has to display the current font size value of "X".

I have managed to implement the increase/decrease method for the text, however I cannot get the value of the text to increase after clicking. The value of the text when increased only allows the user to increase it once. I want the program to be able to increase it by 5 every time the button is activated.

I believe I have to somehow store the new value of the font size and use the new value to allow me to increase/decrease even more.

If anyone could tell me how to do this, or show a solution, it would be greatly appreciated.

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));
 }
 }
 }
}
asked Mar 19, 2015 at 2:28

2 Answers 2

1

Simple fix really: on like 64 of the original code, you accidentaly are trying to count the variable temp2 as the size of the font of it, not the actual text. I've attached a slightly refactored, as well as corrected, version of the code.

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));
 }
 }
}

Hope this helped, and best of luck to you.

answered Mar 19, 2015 at 2:45
Sign up to request clarification or add additional context in comments.

Comments

0

int temp2 = sizeValue.getFont().getSize(); isn't the size of the font you're changing, but is the size of the font which is been used to render the label.

Try using something more like instead...

 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));
 }

You may also need to call revalidate(); and repaint(); at the end of the actionPerformed method to force a refresh, but it work okay without for me

Equally, you could just something like...

 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()));

Which allows you to maintain the font that the label was originally using, but increases/decreases it's size, but also relies on the actual Font size to update the display, rather the relying on calculated values...

answered Mar 19, 2015 at 2:36

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.