Is there a way to select multiple check boxes in java? - java

I have been programming this small app that the user can select one of the a font colors, blue , red and black, so i used JradioButton for them. also the user can choose a style to the font whether bold or italic or both at the same time, so i used Jcheckbox. the problem is that i tried to let the user choose both italic and bold but nothing changed, is there a way to do that?
private void jRadioBlackActionPerformed(java.awt.event.ActionEvent evt) {
if (jRadioBlack.isSelected()) {
jTextField1.setForeground(Color.black);
}
if (jCheckBoxBold.isSelected() && jCheckBoxItalic.isSelected()) {
jTextField1.setFont(new Font("Times New Roman", Font.BOLD + Font.ITALIC, 14));
}
}
private void jRadioRedActionPerformed(java.awt.event.ActionEvent evt) {
if (jRadioRed.isSelected())
jTextField1.setForeground(Color.red);
}
private void jRadioBlueActionPerformed(java.awt.event.ActionEvent evt) {
if (jRadioBlue.isSelected())
jTextField1.setForeground(Color.blue);
}
private void jCheckBoxBoldItemStateChanged(java.awt.event.ItemEvent evt) {
if (jCheckBoxBold.isSelected()) {
jTextField1.setFont(new Font("Times New Roman", Font.BOLD, 14));
} else {
jTextField1.setFont(new Font("Times New Roman", Font.PLAIN, 14));
}
}
private void jCheckBoxItalicStateChanged(javax.swing.event.ChangeEvent evt) {
}
private void jCheckBoxItalicItemStateChanged(java.awt.event.ItemEvent evt) {
if (jCheckBoxItalic.isSelected()) {
jTextField1.setFont(new Font("Times New Roman", Font.ITALIC, 14));
} else {
jTextField1.setFont(new Font("Times New Roman", Font.PLAIN, 14));
}
}

Example:
import java.awt.Font;
import java.awt.BorderLayout;
import java.awt.event.ItemEvent;
import javax.swing.*;
public class Test {
public void run() {
final JTextField textField = new JTextField(20);
JCheckBox bold = new JCheckBox("Bold");
JCheckBox italic = new JCheckBox("Italic");
JPanel stylePanel = new JPanel();
stylePanel.add(bold);
stylePanel.add(italic);
bold.addItemListener(e -> changeStyle(textField, Font.BOLD, e.getStateChange()));
italic.addItemListener(e -> changeStyle(textField, Font.ITALIC, e.getStateChange()));
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(textField, BorderLayout.NORTH);
frame.add(stylePanel, BorderLayout.SOUTH);
frame.setSize(800, 600);
frame.setVisible(true);
}
private void changeStyle(JTextField textField, int style, int stateChange) {
if (stateChange == ItemEvent.DESELECTED) {
removeStyle(textField, style);
} else {
addStyle(textField, style);
}
}
private void addStyle(JTextField textField, int style) {
Font current = textField.getFont();
int newStyle = current.getStyle() | style;
textField.setFont(current.deriveFont(newStyle));
}
private void removeStyle(JTextField textField, int style) {
Font current = textField.getFont();
int newStyle = current.getStyle() & ~style;
textField.setFont(current.deriveFont(newStyle));
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new Test().run());
}
}

Related

Is it possible to split initialize section into different classes?

I have been working on some side project involving MySQL, with will use tree different screens: 'menu, ' add breed', 'browse breed'.
At this point section initialize is getting quite large and I would like to split it into 3 different classes.
Is it possible to initialize for example JPanel outside Window class?
public class Window {
public static void setBreed()
{
for(int i=0; i<16;i++) {
breedLabels[i].setText(breedInfo[i]);
breedLabels[i].setBounds(600,100+i*30,300, 100);
breedLabels[i].setFont(new Font("Verdana", Font.PLAIN, 20));
viewBreed.add(breedLabels[i]);
}
}
public static void setText()
{
for(int i=0; i<16;i++) {
textLabels[i].setText(text[i]);
textLabels[i].setBounds(300,100+i*30,300, 100);
textLabels[i].setFont(new Font("Verdana", Font.PLAIN, 20));
viewBreed.add(textLabels[i]);
}
}
public static String URL = "jdbc:mysql://localhost:3306/chooseyourpuppy";
public static String user = "root";
public static String password = "";
public static String query = "select * from breeds";
static String [] breedInfo = new String[16];
static String [] text = new String[16];
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Window window = new Window();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
View.connect(URL, user, password, query);
}
public Window() {
initialize();
}
private JFrame frame;
public JPanel addBreed;
public static JPanel viewBreed;
public JPanel menu;
public static JLabel[] textLabels;
public static JLabel[] breedLabels;
private void initialize() {
final int WIDTH = 1280, HEIGHT = 720;
frame = new JFrame();
frame.getContentPane().setBackground(Color.WHITE);
frame.getContentPane().setLayout(null);
frame.setBounds(100, 100, WIDTH, HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("MyBREEDS Viewer");
frame.setResizable(false);
frame.setVisible(true);
//header
JPanel red = new JPanel();
red.setBounds(400, 0, 888, 80);
frame.getContentPane().add(red);
red.setBackground(new Color(204, 0, 0));
red.setLayout(null);
JPanel darkGrey = new JPanel();
darkGrey.setBounds(0, 0, 387, 80);
frame.getContentPane().add(darkGrey);
darkGrey.setBackground(new Color(51, 51, 51));
darkGrey.setLayout(null);
JLabel txtpnChoose = new JLabel();
txtpnChoose.setForeground(new Color(240, 240, 240));
txtpnChoose.setBounds(56, 11, 367, 63);
txtpnChoose.setFont(new Font("Verdana", Font.BOLD, 46));
txtpnChoose.setText("Choose your");
txtpnChoose.setBackground(null);
darkGrey.add(txtpnChoose);
JLabel txtpnPuppy = new JLabel();
txtpnPuppy.setBounds(5, 11, 166, 63);
txtpnPuppy.setForeground(new Color(240, 240, 240));
txtpnPuppy.setFont(new Font("Nunito-Bold", Font.BOLD, 46));
txtpnPuppy.setText("puppy");
txtpnPuppy.setBackground(null);
red.add(txtpnPuppy);
JLayeredPane layeredPane = new JLayeredPane();
layeredPane.setBounds(0, 0, WIDTH, HEIGHT);
frame.getContentPane().add(layeredPane);
layeredPane.setLayout(new CardLayout(0, 0));
JButton btnMenu = new JButton("Back to menu");
btnMenu.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
layeredPane.removeAll();
layeredPane.add(menu);
layeredPane.repaint();
layeredPane.revalidate();
}
});
btnMenu.setForeground(Color.WHITE);
btnMenu.setBackground(new Color(51, 51, 51));
btnMenu.setFont(new Font("Verdana", Font.BOLD, 18));
btnMenu.setBounds(660, 20, 180, 40);
btnMenu.setBorderPainted(false);
btnMenu.setFocusPainted(false);
red.add(btnMenu);
//menu
menu = new JPanel();
menu.setBackground(Color.WHITE);
layeredPane.add(menu, "name_410359960271086");
menu.setLayout(null);
JButton btnBrowse = new JButton("Browse breeds");
btnBrowse.setBounds(100, 300, 400, 200);
btnBrowse.setFont(new Font("Verdana", Font.PLAIN, 40));
btnBrowse.setBorder(new LineBorder(Color.DARK_GRAY));
btnBrowse.setBorder(BorderFactory.createStrokeBorder(new BasicStroke(5.0f)));
btnBrowse.setBackground(Color.WHITE);
btnBrowse.setRequestFocusEnabled(false);
btnBrowse.setVisible(true);
btnBrowse.setFocusPainted(false);
btnBrowse.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
layeredPane.removeAll();
layeredPane.add(viewBreed);
layeredPane.repaint();
layeredPane.revalidate();
setText();
setBreed();
}
});
btnBrowse.addMouseListener(new MouseAdapter() {
#Override
public void mouseEntered(MouseEvent e) {
btnBrowse.setBackground(new Color(237, 237, 237));
}
#Override
public void mouseExited(MouseEvent e) {
btnBrowse.setBackground(Color.WHITE);
}
});
menu.add(btnBrowse);
addBreed = new JPanel();
layeredPane.add(addBreed, "name_410359942089403");
addBreed.setVisible(false);
addBreed.setBackground(Color.WHITE);
addBreed.setLayout(null);
//view breed window
viewBreed = new JPanel();
layeredPane.add(viewBreed, "name_410359924014670");
viewBreed.setLayout(null);
viewBreed.setVisible(false);
viewBreed.setBackground(Color.WHITE);
ImageIcon previous = new ImageIcon("src/images/previous.png");
ImageIcon previousHover = new ImageIcon("src/images/previousHover.png");
JButton prevBreed = new JButton(previous);
prevBreed.addMouseListener(new MouseAdapter() {
#Override
public void mouseEntered(MouseEvent e) {
prevBreed.setIcon(previousHover);
}
#Override
public void mouseExited(MouseEvent e) {
prevBreed.setIcon(previous);
}
});
prevBreed.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
View.changeBreed(false);
}
});
prevBreed.setBounds(30, 300, previous.getIconHeight(), previous.getIconWidth());
viewBreed.add(prevBreed);
prevBreed.setRequestFocusEnabled(false);
prevBreed.setOpaque(false);
prevBreed.setContentAreaFilled(false);
prevBreed.setBorderPainted(false);
prevBreed.setFocusPainted(false);
ImageIcon next = new ImageIcon("src/images/next.png");
ImageIcon nextHover = new ImageIcon("src/images/nextHover.png");
JButton nextBreed = new JButton(next);
nextBreed.addMouseListener(new MouseAdapter() {
#Override
public void mouseEntered(MouseEvent e) {
nextBreed.setIcon(nextHover);
}
#Override
public void mouseExited(MouseEvent e) {
nextBreed.setIcon(next);
}
});
nextBreed.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
View.changeBreed(true);
}
});
nextBreed.setBounds(1140, 300, previous.getIconHeight(), previous.getIconWidth());
viewBreed.add(nextBreed);
nextBreed.setRequestFocusEnabled(false);
nextBreed.setVisible(true);
nextBreed.setOpaque(false);
nextBreed.setContentAreaFilled(false);
nextBreed.setBorderPainted(false);
nextBreed.setFocusPainted(false);
//add breed window
JButton btnAdd = new JButton("Add new breed");
btnAdd.setBounds(780, 300, 400, 200);
btnAdd.setFont(new Font("Verdana", Font.PLAIN, 40));
btnAdd.setBorder(new LineBorder(Color.DARK_GRAY));
btnAdd.setBorder(BorderFactory.createStrokeBorder(new BasicStroke(5.0f)));
btnAdd.setBackground(Color.WHITE);
btnAdd.setRequestFocusEnabled(false);
btnAdd.setFocusPainted(false);
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
layeredPane.removeAll();
layeredPane.add(addBreed);
layeredPane.repaint();
layeredPane.revalidate();
}
});
btnAdd.addMouseListener(new MouseAdapter() {
#Override
public void mouseEntered(MouseEvent e) {
btnAdd.setBackground(new Color(237, 237, 237));
}
#Override
public void mouseExited(MouseEvent e) {
btnAdd.setBackground(Color.WHITE);
}
});
menu.add(btnAdd);
breedLabels = new JLabel[breedInfo.length];
for(int i=0; i<breedInfo.length; i++) {
breedLabels[i] = new JLabel(breedInfo[i]);
}
textLabels = new JLabel[breedInfo.length];
for(int i=0; i<breedInfo.length; i++) {
textLabels[i] = new JLabel(breedInfo[i]);
}
}
}
Is it possible to initialize for example JPanel outside Window class?"
Yes. A different class might contain a method that creates & returns a JPanel.
Other tips:
Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or combinations of them along with layout padding and borders for white space.
Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An embedded-resource must be accessed by URL rather than file. See the info. page for embedded resource for how to form the URL.
new Font("Verdana", Font.PLAIN, 20) Use defaults or logical fonts (E.G. Font.SERIF) unless the font is supplied with your app. as an embedded resource.
The loop and array of labels that are added to viewBreed suggest it should be a JList rather than a JPanel
layeredPane.removeAll(); .. Ugh.. Use a CardLayout as shown in this answer.
What is the purpose of the JLayeredPane? I expect it's unnecessary purely on the basis that there is so little use for them.

How to switch JPanel from different classes with mouseClicked?

I'm learning javax.swing right now and I'm just trying some things out. But now I'm facing a problem I just can't solve. When I click the JLabel for changing the page (jpanel), nothing happens. It won't remove and show the other jpanel.
I also added a
public final HomeGUI getMainFrame() {
return this;
}
and at the mouseClicked(MouseEvent e)
gui.getMainFrame().removeAll();
and I tried it also with
gui.getMainFrame().mainPanel.removeAll();
My GUI:
public class HomeGUI extends JFrame {
private static final long serialVersionUID = 1L;
JPanel mainPanel = new JPanel(new CardLayout());
final CardLayout cl = new CardLayout();
final JPanel mainPanel = new JPanel(cl);
Panel panel;
Page2 page2;
public HomeGUI() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(1200, 650);
this.setTitle("Terminal");
this.setLocationRelativeTo(null);
this.setLayout(new BorderLayout(1,1));
panel = new Panel();
panel.Inhalt();
page2 = new Page2();
page2.Inhalt();
this.add(mainPanel);
mainPanel.add(panel, "Seite1");
mainPanel.add(page2, "Seite2");
cl.show(mainPanel, "panel");
}
public static void main(String[] args) {
HomeGUI terminal = new HomeGUI();
terminal.setResizable(false);
terminal.setVisible(true);
}
}
the Panel:
class Panel extends JPanel {
private HomeGUI gui;
Page2 page2;
private static final long serialVersionUID = 1L;
JPanel Panel;
JLabel title = new JLabel("Willkommen");
JLabel bgc = new JLabel("");
JLabel menuStrich = new JLabel();
JLabel menuTitle = new JLabel("Menu");
JLabel menuHome = new JLabel("Home");
JLabel menuSeite2 = new JLabel("Seite2");
public void Inhalt() {
this.setBackground(new Color(230, 230, 230));
this.add(title);
this.add(bgc);
this.setLayout(null);
//Seite1
title.setSize(300, 50);
title.setLocation(300, 20);
title.setFont(new Font("Alba Matter", Font.PLAIN, 48));
//Menu
bgc.setLayout(null);
bgc.setOpaque(true);
bgc.setBackground(new Color(66, 78, 245));
bgc.setSize(280, 650);
bgc.setLocation(0, 0);
bgc.add(menuTitle);
bgc.add(menuStrich);
bgc.add(menuHome);
bgc.add(menuSeite2);
menuTitle.setLocation(90, 10);
menuTitle.setSize(100, 50);
menuTitle.setFont(new Font("Bahnschrift", Font.PLAIN, 38));
menuTitle.setForeground(Color.white);
menuStrich.setLocation(10, 55);
menuStrich.setSize(260, 5);
menuStrich.setBackground(new Color(240, 240, 240));
menuStrich.setOpaque(true);
menuHome.setLocation(30, 70);
menuHome.setSize(200, 50);
menuHome.setFont(new Font("Concert One", Font.PLAIN, 36));
menuHome.setForeground(Color.white);
menuSeite2.setLocation(30, 130);
menuSeite2.setSize(200, 50);
menuSeite2.setForeground(Color.LIGHT_GRAY);
menuSeite2.setFont(new Font("Concert One", Font.PLAIN, 32));
menuSeite2.addMouseListener(new menuSeite2Event());
}
private class menuSeite2Event extends MouseAdapter {
#Override
public void mouseClicked(MouseEvent e) {
//show page2
gui.cl.show(gui.mainPanel, "page2");
}
#Override
public void mouseEntered(MouseEvent e) {
menuSeite2.setForeground(Color.CYAN);
}
#Override
public void mouseExited(MouseEvent e) {
menuSeite2.setForeground(Color.LIGHT_GRAY);
}
}
}
class Page2 extends JPanel {
private static final long serialVersionUID = 1L;
JPanel Panel;
JLabel title = new JLabel("Willkommen");
JLabel bgc = new JLabel("");
JLabel menuStrich = new JLabel();
JLabel menuTitle = new JLabel("Menu");
JLabel menuHome = new JLabel("Home");
JLabel menuSeite2 = new JLabel("Seite2");
public void Inhalt() {
this.setBackground(new Color(230, 230, 230));
this.add(title);
this.add(bgc);
this.setLayout(null);
//Seite1
title.setSize(300, 50);
title.setLocation(300, 20);
title.setFont(new Font("Alba Matter", Font.PLAIN, 48));
//Menu
bgc.setLayout(null);
bgc.setOpaque(true);
bgc.setBackground(new Color(66, 78, 245));
bgc.setSize(280, 650);
bgc.setLocation(0, 0);
bgc.add(menuTitle);
bgc.add(menuStrich);
bgc.add(menuHome);
bgc.add(menuSeite2);
menuTitle.setLocation(90, 10);
menuTitle.setSize(100, 50);
menuTitle.setFont(new Font("Bahnschrift", Font.PLAIN, 38));
menuTitle.setForeground(Color.white);
menuStrich.setLocation(10, 55);
menuStrich.setSize(260, 5);
menuStrich.setBackground(new Color(240, 240, 240));
menuStrich.setOpaque(true);
menuHome.setLocation(30, 70);
menuHome.setSize(200, 50);
menuHome.setFont(new Font("Concert One", Font.PLAIN, 36));
menuHome.setForeground(Color.white);
menuHome.addMouseListener(new menuHomeEvent());
menuSeite2.setLocation(30, 130);
menuSeite2.setSize(200, 50);
menuSeite2.setForeground(Color.white);
menuSeite2.setFont(new Font("Concert One", Font.PLAIN, 32));
}
private class menuHomeEvent extends MouseAdapter {
#Override
public void mouseClicked(MouseEvent e) {
//show Home
}
#Override
public void mouseEntered(MouseEvent e) {
menuHome.setForeground(Color.CYAN);
}
#Override
public void mouseExited(MouseEvent e) {
menuHome.setForeground(Color.LIGHT_GRAY);
}
}
}
INFO: all classes are in 1 file.

Simple quiz not showing next answer

I'm trying to make a "Simple" quiz in Java using a JFrame. Basically long story short... When the user clicks the "NEXT" button after question 2, it doesn't show the next question...
How can I get the code to proceed to question 3?
CODE
import javax.swing.DefaultListModel;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class SimpleQuiz implements KeyListener, ActionListener
{
static final int WIDTH = 900, HEIGHT = 600;
static final Font FONT = new Font("Arial", Font.BOLD, 20);
static final Color DARKGREEN = new Color(0, 140, 0);
int correct = 0;
boolean start = false , Q1 = false , Q2 = false, Q3 = false;
JFrame window;
JMenuBar Menu;
JMenu startMenu;
JMenuItem GoAction;
JRadioButton radButton1;
JRadioButton radButton2;
JRadioButton radButton3;
JRadioButton radButton4;
JLabel question1;
JLabel question2;
JLabel question3;
JLabel score;
JButton next1;
JButton next2;
JButton finish;
JCheckBox checkBox1;
JCheckBox checkBox2;
JCheckBox checkBox3;
JCheckBox checkBox4;
JList listBox;
public static void main(String[] args)
{
new SimpleQuiz();
}
public SimpleQuiz()
{
window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(null);
window.setSize(WIDTH, HEIGHT);
window.setTitle("Quiz");
window.getContentPane().setBackground(Color.WHITE);
window.setLocationRelativeTo(null);
window.setResizable(false);
//
//
//Question 1
//
//
radButton1 = new JRadioButton();
radButton1.setFont(new Font("Arial", Font.BOLD, 14));
radButton1.setForeground(Color.BLACK);
radButton1.setSize(160, 30);
radButton1.setText("Los Angeles");
radButton1.setLocation(300, 180);
radButton1.setFocusable(false);
radButton1.setVisible(false);
window.add(radButton1);
radButton2 = new JRadioButton();
radButton2.setFont(new Font("Arial", Font.BOLD, 14));
radButton2.setForeground(Color.BLACK);
radButton2.setSize(160, 30);
radButton2.setText("Detroit");
radButton2.setLocation(300, 210);
radButton2.setFocusable(false);
radButton2.setVisible(false);
window.add(radButton2);
radButton3 = new JRadioButton();
radButton3.setFont(new Font("Arial", Font.BOLD, 14));
radButton3.setForeground(Color.BLACK);
radButton3.setSize(160, 30);
radButton3.setText("Shanghai");
radButton3.setLocation(300, 240);
radButton3.setFocusable(false);
radButton3.setVisible(false);
window.add(radButton3);
radButton4 = new JRadioButton();
radButton4.setFont(new Font("Arial", Font.BOLD, 14));
radButton4.setForeground(Color.BLACK);
radButton4.setSize(160, 30);
radButton4.setText("New York City");
radButton4.setLocation(300, 270);
radButton4.setFocusable(false);
radButton4.setVisible(false);
window.add(radButton4);
question1 = new JLabel();
question1.setSize(550, 30);
question1.setLocation(160, 120);
question1.setFont(new Font("Arial", Font.BOLD, 16));
question1.setText("Which one of these cities is not located in the United states of America:");
question1.setOpaque(true);
question1.setBackground(Color.WHITE);
question1.setForeground(Color.BLACK);
question1.setVisible(false);
window.add(question1);
next1 = new JButton();
next1.setFont(new Font("Arial", Font.BOLD, 28));
next1.setForeground(Color.BLACK);
next1.setSize(160, 30);
next1.setText("NEXT");
next1.setActionCommand("q2");
next1.setLocation(700, 500);
next1.setFocusable(false);
next1.setVisible(false);
next1.addActionListener(this);
window.add(next1);
//
//
//Question 2
//
//
checkBox1 = new JCheckBox();
checkBox1.setFont(new Font("Arial", Font.BOLD, 14));
checkBox1.setForeground(Color.BLACK);
checkBox1.setSize(160, 30);
checkBox1.setText("13");
checkBox1.setLocation(300, 180);
checkBox1.setFocusable(false);
checkBox1.setVisible(false);
window.add(checkBox1);
checkBox2 = new JCheckBox();
checkBox2.setFont(new Font("Arial", Font.BOLD, 14));
checkBox2.setForeground(Color.BLACK);
checkBox2.setSize(160, 30);
checkBox2.setText("79");
checkBox2.setLocation(300, 210);
checkBox2.setFocusable(false);
checkBox2.setVisible(false);
window.add(checkBox2);
checkBox3 = new JCheckBox();
checkBox3.setFont(new Font("Arial", Font.BOLD, 14));
checkBox3.setForeground(Color.BLACK);
checkBox3.setSize(160, 30);
checkBox3.setText("14");
checkBox3.setLocation(300, 240);
checkBox3.setFocusable(false);
checkBox3.setVisible(false);
window.add(checkBox3);
checkBox4 = new JCheckBox();
checkBox4.setFont(new Font("Arial", Font.BOLD, 14));
checkBox4.setForeground(Color.BLACK);
checkBox4.setSize(160, 30);
checkBox4.setText("87");
checkBox4.setLocation(300, 270);
checkBox4.setFocusable(false);
checkBox4.setVisible(false);
window.add(checkBox4);
question2 = new JLabel();
question2.setSize(550, 30);
question2.setLocation(160, 120);
question2.setFont(new Font("Arial", Font.BOLD, 16));
question2.setText("Select the prime number(s):");
question2.setOpaque(true);
question2.setBackground(Color.WHITE);
question2.setForeground(Color.BLACK);
question2.setVisible(false);
window.add(question2);
next2 = new JButton();
next2.setFont(new Font("Arial", Font.BOLD, 28));
next2.setForeground(Color.BLACK);
next2.setSize(160, 30);
next2.setText("EXT");
next2.setActionCommand("q3");
next2.setLocation(700, 500);
next2.setFocusable(false);
next2.setVisible(false);
next2.addActionListener(this);
window.add(next2);
//
//
//Question 3
//
//
listBox = new JList();
listBox.setFont(new Font("Arial", Font.BOLD, 14));
listBox.setForeground(Color.BLACK);
listBox.setSize(160, 30);
listBox.setLocation(300, 210);
listBox.setFocusable(false);
listBox.setVisible(false);
window.add(listBox);
question3 = new JLabel();
question3.setSize(550, 30);
question3.setLocation(160, 120);
question3.setFont(new Font("Arial", Font.BOLD, 16));
question3.setText("Of the people listed, who was not a US President:");
question3.setOpaque(true);
question3.setBackground(Color.WHITE);
question3.setForeground(Color.BLACK);
question3.setVisible(false);
window.add(question3);
finish = new JButton();
finish.setFont(new Font("Arial", Font.BOLD, 28));
finish.setForeground(Color.BLACK);
finish.setSize(160, 30);
finish.setText("FINISH");
finish.setActionCommand("end");
finish.setLocation(700, 500);
finish.setFocusable(false);
finish.setVisible(false);
finish.addActionListener(this);
window.add(finish);
//
//
//End
//
//
score = new JLabel();
score.setSize(550, 30);
score.setLocation(160, 120);
score.setFont(new Font("Arial", Font.BOLD, 16));
score.setText("your score is: " + correct + "/3");
score.setOpaque(true);
score.setBackground(Color.WHITE);
score.setForeground(Color.BLACK);
score.setVisible(false);
window.add(score);
//
//
//Extra
//
//
Menu = new JMenuBar();
startMenu = new JMenu("Start");
Menu.add(startMenu);
GoAction = new JMenuItem("Go");
GoAction.setActionCommand("q1");
GoAction.addActionListener(this);
startMenu.add(GoAction);
//exitMenuItem.addActionListener(this);
window.setVisible(true);
window.setJMenuBar(Menu);
//if (getActionCommand() == "BeginQuiz")
//System.out.println(Q1);
}
public void keyPressed(KeyEvent e)
{
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
}
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals("q1"))
{
start = true;
radButton1.setVisible(true);
radButton2.setVisible(true);
radButton3.setVisible(true);
radButton4.setVisible(true);
question1.setVisible(true);
next1.setVisible(true);
System.out.println("Q1");
}
if (e.getActionCommand().equals("q2"))
{
{
radButton1.setVisible(false);
radButton2.setVisible(false);
radButton3.setVisible(false);
radButton4.setVisible(false);
question1.setVisible(false);
next1.setVisible(false);
System.out.println("Q2");
checkBox1.setVisible(true);
checkBox2.setVisible(true);
checkBox3.setVisible(true);
checkBox4.setVisible(true);
question2.setVisible(true);
next2.setVisible(true);
}
if (e.getActionCommand().equals("q3"))
{
{
next2.setVisible(false);
checkBox1.setVisible(false);
checkBox2.setVisible(false);
checkBox3.setVisible(false);
checkBox4.setVisible(false);
question2.setVisible(false);
System.out.println("Q3");
question3.setVisible(true);
finish.setVisible(true);
}
if (e.getActionCommand().equals("end"))
{
{
question3.setVisible(false);
finish.setVisible(false);
score.setVisible(true);
finish.setVisible(true);
}
}
}
}
}
}
As always, thanks for helping me out!
You've got your if (action command equals q3) if block buried within the previous if block and so it will never be reached when it is in a true state.
e.g. you have something like:
if (e.getActionCommand().equals("q2"))
{
{ // this block is unnecessary
// bunch of stuff in here
}
// this block is buried within the if block above, and so will never be true
if (e.getActionCommand().equals("q3"))
{
{ // again this block is unnesseary
// more bunch of code
}
// again this block is buried within the previous two!
if (e.getActionCommand().equals("end"))
{
To solve the immediate problem, each if block should be at the same block code level and not nested in the prior block.
For example, a simple fix would be to change this:
if (e.getActionCommand().equals("q2")) {
{
radButton1.setVisible(false);
radButton2.setVisible(false);
radButton3.setVisible(false);
radButton4.setVisible(false);
question1.setVisible(false);
next1.setVisible(false);
System.out.println("Q2");
checkBox1.setVisible(true);
checkBox2.setVisible(true);
checkBox3.setVisible(true);
checkBox4.setVisible(true);
question2.setVisible(true);
next2.setVisible(true);
}
if (e.getActionCommand().equals("q3")) {
{
next2.setVisible(false);
checkBox1.setVisible(false);
checkBox2.setVisible(false);
checkBox3.setVisible(false);
checkBox4.setVisible(false);
question2.setVisible(false);
System.out.println("Q3");
question3.setVisible(true);
finish.setVisible(true);
}
if (e.getActionCommand().equals("end")) {
{
question3.setVisible(false);
finish.setVisible(false);
score.setVisible(true);
finish.setVisible(true);
}
}
}
}
to this:
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("q1")) {
start = true;
radButton1.setVisible(true);
radButton2.setVisible(true);
radButton3.setVisible(true);
radButton4.setVisible(true);
question1.setVisible(true);
next1.setVisible(true);
System.out.println("Q1");
}
if (e.getActionCommand().equals("q2")) {
radButton1.setVisible(false);
radButton2.setVisible(false);
radButton3.setVisible(false);
radButton4.setVisible(false);
question1.setVisible(false);
next1.setVisible(false);
System.out.println("Q2");
checkBox1.setVisible(true);
checkBox2.setVisible(true);
checkBox3.setVisible(true);
checkBox4.setVisible(true);
question2.setVisible(true);
next2.setVisible(true);
}
if (e.getActionCommand().equals("q3")) {
next2.setVisible(false);
checkBox1.setVisible(false);
checkBox2.setVisible(false);
checkBox3.setVisible(false);
checkBox4.setVisible(false);
question2.setVisible(false);
System.out.println("Q3");
question3.setVisible(true);
finish.setVisible(true);
}
if (e.getActionCommand().equals("end")) {
question3.setVisible(false);
finish.setVisible(false);
score.setVisible(true);
finish.setVisible(true);
}
}
But more importantly your code is very repetitive and mixes data with code in an unhealthy way. I would first concentrate on creating an OOP-compliant Question class, and only after doing that and testing it, building a GUI around this class. Also rather than swap components, consider swapping the data that the components display. This will make extending and debugging your code much easier.
For example, I'd start with this:
public class Question {
private String question;
private String correctAnswer;
private List<String> wrongAnswers = new ArrayList<>();
public Question(String question, String correctAnswer) {
this.question = question;
this.correctAnswer = correctAnswer;
}
public void addWrongAnswer(String wrongAnswer) {
wrongAnswers.add(wrongAnswer);
}
public boolean testAnswer(String possibleAnswer) {
return correctAnswer.equalsIgnoreCase(possibleAnswer);
}
public List<String> getAllRandomAnswers() {
List<String> allAnswers = new ArrayList<>(wrongAnswers);
allAnswers.add(correctAnswer);
Collections.shuffle(allAnswers);
return allAnswers;
}
public String getQuestion() {
return question;
}
public String getCorrectAnswer() {
return correctAnswer;
}
// toString, equals and hashCode need to be done too
}
Then I'd
Create a class to hold an ArrayList<Question>, that could give questions as needed, that could tally responses, correct vs. incorrect.
Create file I/O routines to store questions in a file, perhaps as a simple text file or better as an XML file or best as a database.
Then create a class that creates a JPanel that can display any question and that can get user input.
Then a GUI to hold the above JPanel that can get the Question collection from file, that can test the user.

How to change the background color of JLabel?

Here's my program:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FinalsLabExam extends JFrame implements ActionListener,
MouseListener {
JLabel labelMc = new JLabel("McElvin R. Liceralde", SwingConstants.CENTER);
String[] fNames = { "arial", "calibri", "tahoma", "verdana", "broadway" };
JComboBox cfNames = new JComboBox(fNames);
JCheckBox bold, italic;
Font f = new Font("Times New Roman", Font.BOLD, 35);
Font font1 = new Font("Arial", Font.BOLD, 35);
Font font2 = new Font("Calibri", Font.BOLD, 35);
Font font3 = new Font("Tahoma", Font.BOLD, 35);
Font font4 = new Font("Verdana", Font.BOLD, 35);
Font font5 = new Font("Broadway", Font.BOLD, 35);
JMenuBar menubar = new JMenuBar();
JMenu mFormat, mFont, mFont2, mFontName, mFontSize, mColor, mColor2, mText,
mBColor;
JMenuItem miArial, miCalibri, miTahoma, miVerdana, miBroadway, miBlue,
miGreen, miRed, miYellow, miBlack, miOrange;
JPopupMenu pumenu = new JPopupMenu();
JToolBar tbar = new JToolBar();
public FinalsLabExam() {
mFormat = new JMenu("Format");
mFont = new JMenu("Font");
mFont2 = new JMenu("Font");
mFontName = new JMenu("Font Name");
mFontSize = new JMenu("Font Size");
mColor = new JMenu("Color");
mColor2 = new JMenu("Color");
mText = new JMenu("Text");
mBColor = new JMenu("Background");
mFormat.setMnemonic('f');
mFont.setMnemonic('f');
mFont2.setMnemonic('f');
mFontName.setMnemonic('n');
mFontSize.setMnemonic('s');
mColor.setMnemonic('c');
mColor2.setMnemonic('c');
mText.setMnemonic('t');
mBColor.setMnemonic('b');
miArial = new JMenuItem("Arial", 'a');
miCalibri = new JMenuItem("Calibri", 'c');
miTahoma = new JMenuItem("Tahoma", 't');
miVerdana = new JMenuItem("Verdana", 'v');
miBroadway = new JMenuItem("Broadway", 'w');
// miBlue = new JMenuItem("Blue", 'l');
miBlue = new JMenuItem((new ImageIcon("blue.jpg")) + "Blue", 'b');
miGreen = new JMenuItem("Green", 'g');
miRed = new JMenuItem("Red", 'r');
miYellow = new JMenuItem("Yellow", 'y');
miBlack = new JMenuItem("Black", 'k');
miOrange = new JMenuItem("Orange", 'o');
setJMenuBar(menubar);
menubar.add(mFormat);
mFormat.add(mFont);
mFormat.add(mFont2);
mFont.add(mFontName);
mFont2.add(mFontName);
mFontName.add(miArial);
mFontName.add(miCalibri);
mFontName.add(miTahoma);
mFontName.add(miVerdana);
mFontName.add(miBroadway);
mFont.add(mFontSize);
mFont2.add(mFontSize);
mFormat.add(mColor);
mFormat.add(mColor2);
mColor.add(mText);
mColor2.add(mText);
mText.add(miBlue);
mText.add(miGreen);
mText.add(miRed);
mColor.add(mBColor);
mColor2.add(mBColor);
mBColor.add(miYellow);
mBColor.add(miBlack);
mBColor.add(miOrange);
miArial.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, 2));
miCalibri.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, 2));
miTahoma.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T, 2));
miVerdana.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, 2));
miBroadway.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, 2));
miBlue.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B, 2));
miGreen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G, 2));
miRed.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, 2));
miYellow.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Y, 2));
miBlack.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_K, 2));
miOrange.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, 2));
addMouseListener(this);
miArial.addActionListener(this);
miCalibri.addActionListener(this);
miTahoma.addActionListener(this);
miVerdana.addActionListener(this);
miBroadway.addActionListener(this);
miBlue.addActionListener(this);
miGreen.addActionListener(this);
miRed.addActionListener(this);
miYellow.addActionListener(this);
miBlack.addActionListener(this);
miOrange.addActionListener(this);
Container pane = getContentPane();
pane.setLayout(new BorderLayout());
pane.setOpaque(true);
labelMc.setBounds(350, 400, 600, 100);
pane.add(labelMc);
labelMc.setFont(f);
pane.add(tbar, BorderLayout.NORTH);
tbar.add(mFont);
tbar.add(mColor);
pumenu.add(mFont);
pumenu.add(mColor);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setVisible(true);
setTitle("Finals Laboratory Exam");
setSize(500, 200);
setLocation(500, 300);
setResizable(false);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == miArial)
labelMc.setFont(font1);
else if (e.getSource() == miCalibri)
labelMc.setFont(font2);
else if (e.getSource() == miTahoma)
labelMc.setFont(font3);
else if (e.getSource() == miVerdana)
labelMc.setFont(font4);
else if (e.getSource() == miBroadway)
labelMc.setFont(font5);
else if (e.getSource() == miBlue)
labelMc.setForeground(Color.BLUE);
else if (e.getSource() == miGreen)
labelMc.setForeground(Color.GREEN);
else if (e.getSource() == miRed)
labelMc.setForeground(Color.RED);
else if (e.getSource() == miYellow) {
pane.setBackground(Color.YELLOW);
} else if (e.getSource() == miBlack) {
setBackground(Color.BLACK);
} else if (e.getSource() == miOrange) {
setBackground(Color.ORANGE);
}
}
public void mouseExited(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger())
pumenu.show(e.getComponent(), e.getX(), e.getY());
}
public void mousePressed(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
public static void main(String[] args) {
new FinalsLabExam();
}
}
but it doesn't change the background color :(
and when I add pane.setBackground(Color.YELLOW);
It says...CANNOT FIND SYMBOL :(
help me please :)
Thank you for all who will answer and help me. It's my 1st time posting a question here :)
I agree to #Hovercarft you are doing only setBackground(Color.YELLOW); not for any Container .So declate it outside the constructor so that it can be accesible in other methods and Do pane.setBackground(Color.YELLOW);.
Here is full code in case you find any trouble
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FinalsLabExam extends JFrame implements ActionListener, MouseListener
{
JLabel labelMc = new JLabel ("McElvin R. Liceralde", SwingConstants.CENTER);
String [] fNames = {"arial", "calibri", "tahoma", "verdana", "broadway"};
JComboBox cfNames = new JComboBox(fNames);
JCheckBox bold, italic;
Font f = new Font("Times New Roman", Font.BOLD, 35);
Font font1 = new Font("Arial", Font.BOLD, 35);
Font font2 = new Font("Calibri", Font.BOLD, 35);
Font font3 = new Font("Tahoma", Font.BOLD, 35);
Font font4 = new Font("Verdana", Font.BOLD, 35);
Font font5 = new Font("Broadway", Font.BOLD, 35);
JMenuBar menubar = new JMenuBar();
JMenu mFormat, mFont, mFont2, mFontName, mFontSize, mColor, mColor2, mText, mBColor;
JMenuItem miArial, miCalibri, miTahoma, miVerdana, miBroadway, miBlue, miGreen, miRed, miYellow, miBlack, miOrange;
JPopupMenu pumenu = new JPopupMenu();
JToolBar tbar = new JToolBar();
Container pane;// = getContentPane();
public FinalsLabExam()
{
mFormat = new JMenu("Format");
mFont = new JMenu("Font");
mFont2 = new JMenu("Font");
mFontName = new JMenu("Font Name");
mFontSize = new JMenu("Font Size");
mColor = new JMenu("Color");
mColor2 = new JMenu("Color");
mText = new JMenu("Text");
mBColor = new JMenu("Background");
mFormat.setMnemonic('f');
mFont.setMnemonic('f');
mFont2.setMnemonic('f');
mFontName.setMnemonic('n');
mFontSize.setMnemonic('s');
mColor.setMnemonic('c');
mColor2.setMnemonic('c');
mText.setMnemonic('t');
mBColor.setMnemonic('b');
miArial = new JMenuItem("Arial", 'a');
miCalibri = new JMenuItem("Calibri", 'c');
miTahoma = new JMenuItem("Tahoma", 't');
miVerdana = new JMenuItem("Verdana", 'v');
miBroadway = new JMenuItem("Broadway", 'w');
//miBlue = new JMenuItem("Blue", 'l');
miBlue = new JMenuItem((new ImageIcon("blue.jpg")) + "Blue", 'b');
miGreen = new JMenuItem("Green", 'g');
miRed = new JMenuItem("Red", 'r');
miYellow = new JMenuItem("Yellow", 'y');
miBlack = new JMenuItem("Black", 'k');
miOrange = new JMenuItem("Orange", 'o');
setJMenuBar(menubar);
menubar.add(mFormat);
mFormat.add(mFont);
mFormat.add(mFont2);
mFont.add(mFontName);
mFont2.add(mFontName);
mFontName.add(miArial);
mFontName.add(miCalibri);
mFontName.add(miTahoma);
mFontName.add(miVerdana);
mFontName.add(miBroadway);
mFont.add(mFontSize);
mFont2.add(mFontSize);
mFormat.add(mColor);
mFormat.add(mColor2);
mColor.add(mText);
mColor2.add(mText);
mText.add(miBlue);
mText.add(miGreen);
mText.add(miRed);
mColor.add(mBColor);
mColor2.add(mBColor);
mBColor.add(miYellow);
mBColor.add(miBlack);
mBColor.add(miOrange);
miArial.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, 2));
miCalibri.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, 2));
miTahoma.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T, 2));
miVerdana.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, 2));
miBroadway.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, 2));
miBlue.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B, 2));
miGreen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G, 2));
miRed.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, 2));
miYellow.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Y, 2));
miBlack.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_K, 2));
miOrange.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, 2));
addMouseListener(this);
miArial.addActionListener(this);
miCalibri.addActionListener(this);
miTahoma.addActionListener(this);
miVerdana.addActionListener(this);
miBroadway.addActionListener(this);
miBlue.addActionListener(this);
miGreen.addActionListener(this);
miRed.addActionListener(this);
miYellow.addActionListener(this);
miBlack.addActionListener(this);
miOrange.addActionListener(this);
pane = getContentPane();
pane.setLayout(new BorderLayout());
//pane.setOpaque(true);
labelMc.setBounds(350,400,600,100);
pane.add(labelMc);
labelMc.setFont(f);
pane.add(tbar, BorderLayout.NORTH);
tbar.add(mFont);
tbar.add(mColor);
pumenu.add(mFont);
pumenu.add(mColor);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setVisible(true);
setTitle("Finals Laboratory Exam");
setSize(500,200);
setLocation(500,300);
setResizable(false);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == miArial)
labelMc.setFont(font1);
else if (e.getSource() == miCalibri)
labelMc.setFont(font2);
else if (e.getSource() == miTahoma)
labelMc.setFont(font3);
else if (e.getSource() == miVerdana)
labelMc.setFont(font4);
else if (e.getSource() == miBroadway)
labelMc.setFont(font5);
else if (e.getSource() == miBlue)
labelMc.setForeground(Color.BLUE);
else if (e.getSource() == miGreen)
labelMc.setForeground(Color.GREEN);
else if (e.getSource() == miRed)
labelMc.setForeground(Color.RED);
else if (e.getSource() == miYellow)
{pane.setBackground(Color.YELLOW);}
else if (e.getSource() == miBlack)
{pane.setBackground(Color.BLACK);}
else if (e.getSource() == miOrange)
{pane.setBackground(Color.ORANGE);}
}
public void mouseExited(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseReleased(MouseEvent e)
{
if (e.isPopupTrigger())
pumenu.show(e.getComponent(), e.getX(), e.getY());
}
public void mousePressed(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public static void main(String[] args)
{
new FinalsLabExam();
}
}
After selecting yellow you get
You've got a variable scope issue. The pane variable is declared inside of a constructor, and is thus only visible inside its scope of declaration, the constructor, and remains completely invisible to the rest of the class. To solve this, declare the variable as a field, in the class, not in the method or constructor.
For example, not:
public class FinalsLabExam {
public FinalsLabExam() {
// ...
Container pane = getContentPane():
//....
}
but rather:
public class FinalsLabExam {
// declare this as a field
// declare as a JPanel, since that's what it really is
private JPanel pane;
public FinalsLabExam() {
// ...
pane = (JPanel) getContentPane(); // cast to JPanel
//....
}
Note that pane is not a JLabel but rather is a Container, and this makes your question a bit confusing -- what are you trying to set the color of? A JLabel or a Container?

Change JTextArea color based on selected JComboBox item

Along with a basic main thread, this will display a window with a sentence, and change the font to bold as soon as something from a drop down menu is selected.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Gui extends JFrame {
private JComboBox box;
private JTextField tf;
private static String [] filename = {"button.png", "x.png"};
public Gui(){
super("The title is");
setLayout(new FlowLayout());
box = new JComboBox(filename);
tf = new JTextField("This is a sentence", 14);
box.addItemListener( new ItemListener() {
public void itemStateChanged(ItemEvent event) {
if(event.getStateChange()==ItemEvent.SELECTED) {
tf.setFont(new Font("Serif", Font.BOLD, 14));
}
}
});
add(box);
add(tf);
}
}
Is there any way of getting it to change back, if i choose the other member from the drop down menu?
Try:
tf.setFont(new Font("Serif", Font.PLAIN, 14));
In order to check with item was selected, use box.getSelectedItem() or box.getSelectedIndex().
For example:
public void itemStateChanged(ItemEvent event){
if(event.getStateChange()==ItemEvent.SELECTED){
if (box.getSelectedIndex()==0)
tf.setFont(new Font("Serif", Font.BOLD, 14)); //first item selected
else
tf.setFont(new Font("Serif", Font.PLAIN, 14)); //second item selected
}
}
How about using getFont to see what you have, test it, and based upon your test change what you want?
box.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
tf.setFont(new Font("Serif", Font.BOLD, 14));
}
});
Try this!

Categories