|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

10-11-2008, 03:05 AM
|
|
Member
|
|
Join Date: Sep 2008
Posts: 31
|
|
|
adding a jpanel in the middle of the script
heres my calculator. it works fine, but for the love of god i can't figure out how to add another JPanel when you clikc the advanced button in the menu. Any way to do this?
import java.lang.Math;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.StringSelection;
//import random shit
public class Calculator
{
private JPanel CalculatorPanel = new JPanel(); // panel holds the button grid
public static JPanel screenPanel;
public static JFrame frame;
public static JTextField screen;
public static JMenu menu;
public static JMenu edit;
public static JMenuBar menuBar;
public static JMenuItem exit = new JMenuItem("Exit");
public static JMenuItem about = new JMenuItem("About");
public static JMenuItem copy = new JMenuItem("Copy");
public static JMenuItem paste = new JMenuItem("Paste");
public static JMenuItem advanced = new JMenuItem("Advanced");
public static JPanel advancedButtons;
//all the menu items, panels, and the frame
//numbers that allow calc to work
int operation = 0;
double secondnum1;
double firstnum1;
double oppAnswer = 0;
String firstNum;
String writeAnswer;
String secondNum;
double secondParse;
double firstParse;
boolean options = true;
String[][] buttonStrings = // use these Strings to create a JButton grid
{
{"x²", "sin", "cos", "tan"},
{"CE", "\u221A", "1/x", "inv"},
{"7", "8", "9", "/"},
{"4", "5", "6", "*"},
{"1", "2", "3", "-"},
{"0", ".", "=", "+"},
};
public Calculator()
{
int rowLength = buttonStrings.length;
int colLength = buttonStrings[0].length;
CalculatorPanel.setLayout(new GridLayout(rowLength, colLength));
CalculatorPanel.setSize(200,200);
ButtonListener buttonListener = new ButtonListener(); // create our ActionListener
for (int row = 0; row < rowLength; row++)
{
for (int col = 0; col < colLength; col++)
{
// for each String in the array, create a button that holds the string
String buttonString = buttonStrings[row][col];
JButton button = new JButton(buttonString); // this button here
button.addActionListener(buttonListener); // add the listener
Calculator.about.addActionListener(new MenuActionListener());
CalculatorPanel.add(button); // and add it to the panel
}
}
}
class MenuActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if ((e.getActionCommand() == "About") && (options == true)){
JOptionPane.showMessageDialog(null, "This is a java calculator created by Isaac Flaum as a good looking efficient multiplatform calculator.");
options = false;
}
if ((e.getActionCommand() == "Copy")){
String calcText = screen.getText();
StringSelection ss = new StringSelection(calcText);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
}
if ((e.getActionCommand() == "Advanced")){
advancedButtons = new JPanel(new GridLayout(1,1)); //pain in the ass gridlayout manager
JButton log = new JButton("Log");
advancedButtons.add(log);
frame.add(advancedButtons,BorderLayout.EAST);
frame.getContentPane().add(new Calculator().getComponent2());
frame.setVisible(true);
//do shit
}
}
}
public JComponent getComponent()
{
return CalculatorPanel;
}
public JComponent getComponent2()
{
return advancedButtons;
}
// here's the button listener class that of course implements action listener
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
options = true;
String screentext = screen.getText();
// we can easily extract the String held by the button pressed
String actionCommand = e.getActionCommand(); // here's the string
if (actionCommand == "0"){
screen.setText(screentext + "0");
}
if (actionCommand == "1"){
screen.setText(screentext + "1");
}
if (actionCommand == "2"){
screen.setText(screentext + "2");
}
if (actionCommand == "3"){
screen.setText(screentext + "3");
}
if (actionCommand == "4"){
screen.setText(screentext + "4");
}
if (actionCommand == "5"){
screen.setText(screentext + "5");
}
if (actionCommand == "6"){
screen.setText(screentext + "6");
}
if (actionCommand == "7"){
screen.setText(screentext + "7");
}
if (actionCommand == "8"){
screen.setText(screentext + "8");
}
if (actionCommand == "9"){
screen.setText(screentext + "9");
}
if (actionCommand == "*"){
firstNum = screen.getText();
operation = 1;
screen.setText("");
}
if (actionCommand == "/"){
firstNum = screen.getText();
operation = 2;
screen.setText("");
}
if (actionCommand == "+"){
firstNum = screen.getText();
operation = 3;
screen.setText("");
}
if (actionCommand == "-"){
firstNum = screen.getText();
operation = 4;
screen.setText("");
}
if (actionCommand == "."){
screen.setText(screentext + ".");
}
if (actionCommand == "CE"){
screen.setText("");
secondNum = "";
firstNum = "";
firstParse = 0;
secondParse = 0;
operation = 0;
writeAnswer = "";
}
if (actionCommand == "1/x"){
secondNum = screen.getText();
secondParse = Double.parseDouble(secondNum);
oppAnswer = 1 / secondParse;
writeAnswer = Double.toString(oppAnswer);
screen.setText(writeAnswer);
}
if (actionCommand == "inv"){
secondNum = screen.getText();
secondParse = Double.parseDouble(secondNum);
oppAnswer = -1 * secondParse;
writeAnswer = Double.toString(oppAnswer);
screen.setText(writeAnswer);
}
if (actionCommand == "\u221A"){
secondNum = screen.getText();
secondParse = Double.parseDouble(secondNum);
if (secondParse < 0){
screen.setText("Error");
}
else{
oppAnswer = Math.sqrt(secondParse);
writeAnswer = Double.toString(oppAnswer);
screen.setText(writeAnswer);
}
}
if (actionCommand == "x²"){
secondNum = screen.getText();
secondParse = Double.parseDouble(secondNum);
oppAnswer = secondParse * secondParse;
writeAnswer = Double.toString(oppAnswer);
screen.setText(writeAnswer);
}
if (actionCommand == "sin"){
secondNum = screen.getText();
secondParse = Double.parseDouble(secondNum);
oppAnswer = Math.sin(secondParse);
writeAnswer = Double.toString(oppAnswer);
screen.setText(writeAnswer);
}
if (actionCommand == "cos"){
secondNum = screen.getText();
secondParse = Double.parseDouble(secondNum);
oppAnswer = Math.cos(secondParse);
writeAnswer = Double.toString(oppAnswer);
screen.setText(writeAnswer);
}
if (actionCommand == "tan"){
secondNum = screen.getText();
secondParse = Double.parseDouble(secondNum);
oppAnswer = Math.tan(secondParse);
writeAnswer = Double.toString(oppAnswer);
screen.setText(writeAnswer);
}
if (actionCommand == "="){
secondNum = screen.getText();
screen.setText("");
firstParse = Double.parseDouble(firstNum);
secondParse = Double.parseDouble(secondNum);
if(operation == 1){
oppAnswer = firstParse * secondParse;
writeAnswer = Double.toString(oppAnswer);
screen.setText(writeAnswer);
}
if(operation == 2){
oppAnswer = firstParse / secondParse;
writeAnswer = Double.toString(oppAnswer);
screen.setText(writeAnswer);
}
if(operation == 4){
oppAnswer = firstParse - secondParse;
writeAnswer = Double.toString(oppAnswer);
screen.setText(writeAnswer);
}
if(operation == 3){
oppAnswer = firstParse + secondParse;
writeAnswer = Double.toString(oppAnswer);
screen.setText(writeAnswer);
}
}
}
}
// this calls our Swing code in a thread-safe manner
private static void createAndShowUI()
{
Calculator.frame = new JFrame("Isaac's Calculator");
frame.setLayout(new BorderLayout());
frame.getContentPane().add(new Calculator().getComponent());
Calculator.screenPanel = new JPanel(); //panel to hold screen
Calculator.screenPanel.setLayout(new GridLayout(2, 1));
Calculator.screen = new JTextField(20);
Calculator.menu = new JMenu("File");
Calculator.menuBar = new JMenuBar();
Calculator.edit = new JMenu("Edit");
edit.add(copy);
edit.add(paste);
edit.add(advanced);
menu.add(exit);
menu.add(about);
menuBar.add(menu);
menuBar.add(edit);
Calculator.screenPanel.add(Calculator.menuBar);
Calculator.screenPanel.add(screen);
frame.add(Calculator.screenPanel, BorderLayout.NORTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args)
{
try{
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
}
catch (UnsupportedLookAndFeelException e) {
// handle exception
}
catch (ClassNotFoundException e) {
// handle exception
}
catch (InstantiationException e) {
// handle exception
}
catch (IllegalAccessException e) {
// handle exception
}
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
|
|

10-11-2008, 03:12 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Posts: 1,289
|
|
|
I haven't looked at your code, but I'd make sure that the main jpanel is using an appropriate layout, one that will easily accept a JPanel on the bottom, say a BorderLayout or a BoxLayout, add the JPanel on demand, and then call pack() on the JFrame.
|
|

10-11-2008, 03:26 AM
|
|
Member
|
|
Join Date: Sep 2008
Posts: 31
|
|
|
yea i tried that, didnt work so well...
advancedButtons = new JPanel(new GridLayout(1,1)); //pain in the ass gridlayout manager
JButton log = new JButton("Log");
advancedButtons.add(log);
frame.add(advancedButtons,BorderLayout.EAST);
frame.getContentPane().add(new Calculator().getComponent2());
frame.pack();
|
|

10-11-2008, 06:43 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Posts: 1,289
|
|
what you need to do is this: add both panels to the main panel but call setVisible(false) on the advanced panel. Then in your menu actions, call setVisible(true/false) depending on the menu chosen, and repeat a JFrame call to "pack()". I'd also enable and disable menu items depending on the advanced panel visibility state.
If this doesn't work, then you need to post an SSCCE.
|
|

10-11-2008, 10:37 AM
|
|
Senior Member
|
|
Join Date: Sep 2008
Posts: 317
|
|
|
And never compare Strings (or any object references) with the == operator, use the .equals(...) method.
db
|
|

10-11-2008, 04:52 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,225
|
|
never compare Strings (or any object references) with the == operator, use the .equals(...)
It all depends what you are doing. Classes are not required to have an equals method that tests/examines/uses the contents of the object. It's a good idea but NOT required.
If you want to see if two pointers (object references) point at/refer to the same object them == is the one to use.
|
|

10-11-2008, 10:02 PM
|
|
Member
|
|
Join Date: Sep 2008
Posts: 31
|
|
so i did that, and now i have this, but it still doesn't work :-(
if ((e.getActionCommand() == "Advanced")){
advancedButtons.setVisible(true);
frame.pack();
|
|

10-12-2008, 12:23 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,225
|
|
Is there an error message? Or what?
Did you read the previous posts about equals() and ==?
If not, you need to.
You need to use equals(). It will test if the contents of the object returned by getActionCommand is the same as the contents of the String object containing "Advanced".
equals tests for the same content in TWO objects. == tests if the two object references(pointers) point to the SAME object.
|
|

10-12-2008, 09:14 AM
|
|
Member
|
|
Join Date: Sep 2008
Posts: 31
|
|
Originally Posted by Norm
Is there an error message? Or what?
Did you read the previous posts about equals() and ==?
If not, you need to.
You need to use equals(). It will test if the contents of the object returned by getActionCommand is the same as the contents of the String object containing "Advanced".
equals tests for the same content in TWO objects. == tests if the two object references(pointers) point to the SAME object.
i changed it to .equals, no luck, no errors either, just when i click advanced the panel doesnt show
|
|

10-12-2008, 03:24 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Posts: 1,289
|
|
Here's a simple example of what I'm suggesting:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class AdvancedFrame
{
private static final Dimension CENTER_SIZE = new Dimension(250, 200);
private static final Dimension EAST_SIZE = new Dimension(150, 200);
private JFrame frame = new JFrame();
private JPanel mainPanel = new JPanel();
private Action advancedAction = new AbstractAction("Advanced")
{
public void actionPerformed(ActionEvent e)
{
advancedAxnPerformed();
}
};
private Action simpleAction = new AbstractAction("Simple")
{
public void actionPerformed(ActionEvent e)
{
simpleAxnPerformed();
}
};
private JMenuItem advancedMenuItem = new JMenuItem(advancedAction);
private JMenuItem simpleMenuItem = new JMenuItem(simpleAction);
private JPanel advancedPanel = new JPanel();
private JPanel mainCalcPanel = new JPanel();
public AdvancedFrame()
{
setupMainPanel(mainCalcPanel, advancedPanel);
frame = new JFrame("Advanced Frame");
frame.getContentPane().add(mainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
advancedPanel.setVisible(false);
JMenu menu = new JMenu("Calculator Type");
menu.add(simpleMenuItem);
menu.add(advancedMenuItem);
simpleMenuItem.setEnabled(false);
JMenuBar menubar = new JMenuBar();
menubar.add(menu);
frame.setJMenuBar(menubar);
frame.pack();
frame.setLocationRelativeTo(null);
}
private void setupMainPanel(JPanel centerPanel, JPanel eastPanel)
{
mainCalcPanel.setBorder(BorderFactory.createTitledBorder("Main Calculator Panel"));
advancedPanel.setBorder(BorderFactory.createTitledBorder("Advanced Panel"));
mainCalcPanel.setPreferredSize(CENTER_SIZE);
advancedPanel.setPreferredSize(EAST_SIZE);
mainPanel.setLayout(new BorderLayout());
mainPanel.add(centerPanel, BorderLayout.CENTER);
mainPanel.add(eastPanel, BorderLayout.EAST);
eastPanel.setVisible(false);
}
private void advancedAxnPerformed()
{
advancedPanel.setVisible(true);
advancedMenuItem.setEnabled(false);
simpleMenuItem.setEnabled(true);
frame.pack();
}
private void simpleAxnPerformed()
{
advancedPanel.setVisible(false);
advancedMenuItem.setEnabled(true);
simpleMenuItem.setEnabled(false);
frame.pack();
}
public JFrame getFrame()
{
return frame;
}
private static void createAndShowUI()
{
AdvancedFrame aFrame = new AdvancedFrame();
aFrame.getFrame().setVisible(true);
}
public static void main(String[] args)
{
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
|
|

10-12-2008, 04:45 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,225
|
|
|
Have you tried debugging your code to see if it is executing the listener? Add some println()s to it to show if it is being executed and with what values.
Your long list of if() tests in the listener assumes that one of them will match. What if none of them match? Your code will NOT show that.
A suggestion: use if{}else if{} ... else{System.out.println("No match");}
with the ending else to catch the case where none of the above if tests matched. Sort of like the default in a switch statement.
|
|

10-12-2008, 07:50 PM
|
|
Member
|
|
Join Date: Sep 2008
Posts: 31
|
|
Originally Posted by Norm
Have you tried debugging your code to see if it is executing the listener? Add some println()s to it to show if it is being executed and with what values.
Your long list of if() tests in the listener assumes that one of them will match. What if none of them match? Your code will NOT show that.
A suggestion: use if{}else if{} ... else{System.out.println("No match");}
with the ending else to catch the case where none of the above if tests matched. Sort of like the default in a switch statement.
yea wow, i forgot to add an action listener to the advanced button :P wow im an idiot! thanks!
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|