Hej, mam szybkie pytanie. Dodaje funkcjonalności do kalkulatora który miał tylko prezentować jak działą GridBagLayout. Kiedy chciałem zmienić czcionke na "ekranie" niestety program nie ruszył. Bez tego działa. Co jest nie tak?Dla wygody ten kawalek kodu jest na początku klasy.
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
import javax.swing.*;
public class Lesson29 extends JFrame{
JButton but1, but2, but3, but4, but5, but6,
but7, but8, but9, but0, butPlus, butEqeals,
clearAll;
JTextArea textResult;
int num1, num2;
String temp;
public static void main(String[] args){
new Lesson29();
}
public Lesson29(){
// Create the frame, position it and handle closing it
this.setSize(300,380);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Calculator");
//TU JEST PROBLEM
textResult.setFont(new Font("Times New Roman",Font.BOLD,2));
JPanel thePanel = new JPanel();
// Create a Grid Layout with as many rows as needed
// and 3 columns. The last 2 parameters create a
// horizontal gap of 2 pixels and a vertical gap of
// 2 pixels.
// thePanel.setLayout(new GridLayout(0,3,2,2));
thePanel.setLayout(new GridBagLayout());
// You create a GridBagContraints object that defines
// defaults for your components
GridBagConstraints gridConstraints = new GridBagConstraints();
// Define the x position of the component
gridConstraints.gridx = 1;
// Define the y position of the component
gridConstraints.gridy = 1;
// Number of columns the component takes up
gridConstraints.gridwidth = 1;
// Number of rows the component takes up
gridConstraints.gridheight = 1;
// Gives the layout manager a hint on how to adjust
// component width (0 equals fixed)
gridConstraints.weightx = 1;
// Gives the layout manager a hint on how to adjust
// component height (0 equals fixed)
gridConstraints.weighty = 1;
// Defines padding top, left, bottom, right
gridConstraints.insets = new Insets(5,5,5,5);
// Defines where to place components if they don't
// fill the space: CENTER, NORTH, SOUTH, EAST, WEST
// NORTHEAST, etc.
gridConstraints.anchor = GridBagConstraints.CENTER;
// How should the component be stretched to fill the
// space: NONE, , VERTICAL, BOTH
gridConstraints.fill = GridBagConstraints.BOTH;
textResult = new JTextArea("",20, 20);
// Defines the font to use in the text field
textResult.setFont(font);
but1 = new JButton("1");
but2 = new JButton("2");
but3 = new JButton("3");
but4 = new JButton("4");
but5 = new JButton("5");
but6 = new JButton("6");
but7 = new JButton("7");
but8 = new JButton("8");
but9 = new JButton("9");
butPlus = new JButton("+");
but0 = new JButton("0");
butEqeals = new JButton("=");
clearAll = new JButton("C");
ListenForButton lForButton = new ListenForButton();
but1.addActionListener(lForButton);
but2.addActionListener(lForButton);
but3.addActionListener(lForButton);
but4.addActionListener(lForButton);
but5.addActionListener(lForButton);
but6.addActionListener(lForButton);
but7.addActionListener(lForButton);
but8.addActionListener(lForButton);
but9.addActionListener(lForButton);
but0.addActionListener(lForButton);
butPlus.addActionListener(lForButton);
butEqeals.addActionListener(lForButton);
clearAll.addActionListener(lForButton);
thePanel.add(clearAll,gridConstraints);
gridConstraints.gridwidth = 20;
gridConstraints.gridx = 5;
thePanel.add(textResult,gridConstraints);
gridConstraints.gridwidth = 1;
gridConstraints.gridx = 1;
gridConstraints.gridy = 2;
thePanel.add(but1,gridConstraints);
gridConstraints.gridx = 5;
thePanel.add(but2,gridConstraints);
gridConstraints.gridx = 9;
thePanel.add(but3,gridConstraints);
gridConstraints.gridx = 1;
gridConstraints.gridy = 3;
thePanel.add(but4,gridConstraints);
gridConstraints.gridx = 5;
thePanel.add(but5,gridConstraints);
gridConstraints.gridx = 9;
thePanel.add(but6,gridConstraints);
gridConstraints.gridx = 1;
gridConstraints.gridy = 4;
thePanel.add(but7,gridConstraints);
gridConstraints.gridx = 5;
thePanel.add(but8,gridConstraints);
gridConstraints.gridx = 9;
thePanel.add(but9,gridConstraints);
gridConstraints.gridx = 1;
gridConstraints.gridy = 5;
thePanel.add(butPlus,gridConstraints);
gridConstraints.gridx = 5;
thePanel.add(but0,gridConstraints);
gridConstraints.gridx = 9;
thePanel.add(butEqeals,gridConstraints);
// Adding buttons using the Grid Layout
/*
thePanel.add(but1);
thePanel.add(but2);
thePanel.add(but3);
thePanel.add(but4);
thePanel.add(but5);
thePanel.add(but6);
thePanel.add(but7);
thePanel.add(but8);
thePanel.add(but9);
thePanel.add(butPlus);
thePanel.add(but0);
thePanel.add(butMinus);
*/
this.add(thePanel);
this.setVisible(true);
} // END OF Lesson29 CONSTRUCTOR
private class ListenForButton implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == but1) {
textResult.append("1");
}
else if(e.getSource() == but2)
{
textResult.append("2");
}
else if(e.getSource() == but3)
{
textResult.append("3");
}
else if(e.getSource() == but4)
{
textResult.append("4");
}
else if(e.getSource() == but5)
{
textResult.append("5");
}
else if(e.getSource() == but6)
{
textResult.append("6");
}
else if(e.getSource() == but7)
{
textResult.append("7");
}
else if(e.getSource() == but8)
{
textResult.append("8");
}
else if(e.getSource() == but9)
{
textResult.append("9");
}
else if(e.getSource() == but0)
{
textResult.append("0");
}
else if(e.getSource() == clearAll) {
textResult.setText("");
}
else if(e.getSource() == butPlus)
{
temp = textResult.getText();
num1 = Integer.parseInt(temp);
temp = "";
textResult.setText("");
}
else if(e.getSource() == butEqeals)
{
temp = textResult.getText();
num2 = Integer.parseInt(temp);
num2 = num2 + num1;
temp = Integer.toString(num2);
textResult.setText(temp);
}
}
}
} // END OF Lesson29 CLASS