Thank you all - I have got it to work. Note that this assignment is just the GUI aspect, once i get the application working I'll post in submissions. Here is the fix, my main was able to use JFrame, just none of the contents
Main
Java: |
/*
* File name: Calculator.java
* Author: Tallguy
* Professor:-----
* Purpose: The driver class for my Swing GUI, responsible for running everythings. Calls the splash screen
* and creates an intial frame(JFrame) in which to hold the contents (buttons, text field etc)
* of all aspects of the calculator to be created.
* Class list: []
*/
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Toolkit;
import javax.swing.JFrame;
/************************************************
@author Tallguy
@version 1
@see [javax.swing.*, java.awt*]
@since java 1.6
************************************************/
public class Calculator {
public static void main (String[] args ) {
/************************************************
{@value} WIDTH - constant var for the screen width
{@value} HEIGHT - constant var for the screen height
************************************************/
final int WIDTH = 256;
final int HEIGHT = 256;
EventQueue. invokeLater(new Runnable() {
/************************************************
Creates the initial JFrame settings its values, such as height/width and resizable
factors. It thens adds contents to the frame
@param []
@return [ ]
************************************************/
public void run () {
CalculatorView calcview = new CalculatorView ();
JFrame frame = new JFrame("Calculator"); /*creates a new JFrame object*/
frame. setResizable(false);
frame. setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE); /*overrides default close oepration (close window) to kill process*/
int width = WIDTH;
int height = HEIGHT;
Dimension screen = Toolkit. getDefaultToolkit(). getScreenSize();
int x = (screen. width - width ) / 2;
int y = (screen. height - height ) / 2;
frame. setBounds(x, y, width, height ); /*sets screen size*/
frame. add(calcview ); /*adds contents to the JFrame*/
frame. setVisible(true); /*makes the screen visible*/
}
});
}
}
|
CalculatorView
Java: |
/*
* File name: CalculatorView.java
* Author: Tallguy
*/
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.Border;
public class CalculatorView extends JPanel {
private static final long serialVersionUID = 1L;
private JTextField display;
private JLabel error;
private JButton dotButton, backspace, btn;
private JPanel viewPanel;
private JRadioButton radioInt, radioFl;
private ButtonGroup Rbgroup /* = new ButtonGroup()*/;
public CalculatorView () {
this. setLayout(new BorderLayout());
this. setBorder(BorderFactory. createEmptyBorder(0, 2, 5, 2));
Controller handler = new Controller ();
/* sets look and feel to work on both Win and MacOS */
try {
UIManager. setLookAndFeel(UIManager
. getCrossPlatformLookAndFeelClassName());
} catch (Exception e ) {
e. printStackTrace();
}
/* sets look and feel to work on both Win and MacOS - END */
/* radio buttons */
JPanel Rbutton = new JPanel(new FlowLayout());
Border outer = BorderFactory. createEmptyBorder(0, 15, 3, 15);
Border inner = BorderFactory. createLineBorder(Color. BLUE, 4);
Rbutton. setBorder(BorderFactory. createCompoundBorder(outer, inner ));
Rbgroup = new ButtonGroup();;
Rbutton. setFocusable(false);
/* radio buttons - END */
/* north layer parts */
/* backspace button */
backspace = new JButton("<<");
backspace. setPreferredSize(new Dimension(20, 20));
backspace. setBorder(BorderFactory. createEmptyBorder());
backspace. setOpaque(false);
backspace. setToolTipText("Backspace (Alt-B)");
backspace. setContentAreaFilled(false);
backspace. setBorderPainted(false);
backspace. setMnemonic('B');
backspace. setFocusPainted(false);
backspace. addActionListener(handler );
/* backspace button - END */
/* text field */
display = new JTextField("0.0", 15);
display. setHorizontalAlignment(JTextField. RIGHT);
display. setBackground(Color. WHITE);
display. setEditable(false);
/* text field - END */
/* error field */
error = new JLabel(" ");
error. setPreferredSize(new Dimension(20, 20));
error. setBorder(BorderFactory. createEmptyBorder());
error. setOpaque(true);
error. setBackground(Color. GREEN);
error. setToolTipText("Error");
/* error field - END */
/* north layer parts - END */
/* holds all north end parts */
viewPanel = new JPanel(new FlowLayout());
viewPanel. add(error, BorderLayout. WEST);
viewPanel. add(display, BorderLayout. NORTH);
viewPanel. add(backspace, BorderLayout. EAST);
/* holds all north end parts - END */
/* calc buttons */
JPanel buttons = new JPanel(new GridLayout(4, 5, 5, 5));
String[] values = { "7", "8", "9", "/", "P", "4", "5", "6", "*",
"\u221A", "1", "2", "3", "-", "C", "0", ".", "+/-", "+", "=" };
for (String value : values ) {
addButton (buttons, value, handler );
}
String[] radio = { "Integer", "Float" };
for (String value : radio ) {
addButton (Rbutton, value, handler );
}
/* calc buttons - END */
/* adds all to master panel */
this. add(viewPanel, BorderLayout. NORTH);
this. add(Rbutton, BorderLayout. CENTER);
this. add(buttons, BorderLayout. SOUTH);
/* adds all to master panel - END */
}
private void addButton (Container c, String s, ActionListener handler ) {
if (s == "Integer") {
radioInt = new JRadioButton("Integer", false);
radioInt. setFont(new Font("Serif", Font. BOLD, 12));
radioInt. setBackground(Color. yellow);
radioInt. addActionListener(handler );
radioInt. setFocusPainted(false);
Rbgroup. add(radioInt );
c. add(radioInt );
} else if (s == "Float") {
radioFl = new JRadioButton("Float", true);
radioFl. setFont(new Font("Serif", Font. BOLD, 12));
radioFl. setBackground(Color. PINK);
radioFl. addActionListener(handler );
radioFl. setFocusPainted(false);
Rbgroup. add(radioFl );
c. add(radioFl );
} else {
btn = new JButton(s );
btn. setPreferredSize(new Dimension(30, 33));
btn. setForeground(Color. BLUE);
btn. setFont(new Font("Serif", Font. BOLD, 12));
if (s == "+/-") {
btn. setFont(new Font("Serif", Font. BOLD, 10));
}
if (s == ".") {
dotButton = btn;
}
if (s == "C") {
btn. setBackground(Color. RED);
btn. setForeground(Color. black);
}
if (s == "=") {
btn. setBackground(Color. yellow);
btn. setForeground(Color. black);
}
btn. setFocusPainted(false);
btn. addActionListener(handler );
c. add(btn );
}
}
private class Controller implements ActionListener {
public void actionPerformed (ActionEvent e ) {
String digit = e. getActionCommand();
display. setText(digit );
//display.setText(display.getText() + digit);
}
}
}
|
@Tony - You are awesome good sir, I always forget about the for-each loops
Tony wrote:
Doesn't answer your question, but... are you using Java 5+ ? for-each loops are your friend
Insectoid wrote:
I've never been able to get any of Java's gui elements to do what I want. If you were me you'd give up now, but you're Tallguy, not me, so don't give up.
Then again, I am six & a half feet tall, so maybe you are me.
-only 6'4"
EDIT-Added syntax |