Results 1 to 9 of 9
Thread: Spacing buttons on a panel
- 10-16-2008, 06:15 PM #1
Member
- Join Date
- Sep 2008
- Posts
- 31
- Rep Power
- 0
Spacing buttons on a panel
how would i make it so that 1, the buttons are spaced a little on the jpanel, and 2 changing the background color of the screen, i tried screen.setBackground(Color.Black); and put it before the screen is added to the jpanel, and before the frame is packed, but it doesnt work :-(
Java Code:import java.lang.Math; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.StringSelection; import java.awt.*; import java.awt.Toolkit; //import random shit public class Main { private JPanel MainPanel = 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 JMenu view; 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 JCheckBoxMenuItem advanced = new JCheckBoxMenuItem("Advanced"); public static JCheckBoxMenuItem simple = new JCheckBoxMenuItem("Simple"); public static JPanel advancedButtons; public static JButton log; public static JButton pi; public static JButton cube; public static JButton square; public static JButton sin; public static JButton cos; public static JButton tan; public static JButton insin; public static JButton incos; public static JButton intan; public static ButtonListener buttonListener; //{"x²", "sin", "cos", "tan"}, //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 { {"CE", "\u221A", "1/x", "inv"}, {"7", "8", "9", "/"}, {"4", "5", "6", "*"}, {"1", "2", "3", "-"}, {"0", ".", "=", "+"}, }; public Main() { int rowLength = buttonStrings.length; int colLength = buttonStrings[0].length; MainPanel.setLayout(new GridLayout(rowLength, colLength)); MainPanel.setSize(200,200); 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 MainPanel.add(button); // and add it to the panel button.setBackground(Color.orange); } } Main.about.addActionListener(new MenuActionListener()); Main.advanced.addActionListener(new MenuActionListener()); Main.simple.addActionListener(new MenuActionListener()); Main.exit.addActionListener(new MenuActionListener()); Main.copy.addActionListener(new MenuActionListener()); } class MenuActionListener implements ActionListener { public void actionPerformed(ActionEvent e) { if ((e.getActionCommand().equals("About")) && (options == true)){ JOptionPane.showMessageDialog(null, "Java calculator created by Isaac Flaum"); options = false; } if ((e.getActionCommand().equals("Advanced"))){ advancedButtons.setVisible(true); advanced.setSelected(true); simple.setSelected(false); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } if ((e.getActionCommand().equals("Simple"))){ advancedButtons.setVisible(false); advanced.setSelected(false); simple.setSelected(true); frame.pack(); } if ((e.getActionCommand().equals("Copy"))){ String s = screen.getText(); StringSelection ss = new StringSelection(s); Clipboard systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); systemClipboard.setContents(ss,ss); } if ((e.getActionCommand().equals("Exit"))){ System.exit(0); } } } public JComponent getComponent() { return MainPanel; } // 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); } //3.1415926535897932384626433832795 = pi if (actionCommand == "sin"){ secondNum = screen.getText(); secondParse = Double.parseDouble(secondNum); oppAnswer = Math.sin(secondParse); writeAnswer = Double.toString(oppAnswer); screen.setText(writeAnswer); } if (actionCommand == "asin"){ secondNum = screen.getText(); secondParse = Double.parseDouble(secondNum); oppAnswer = Math.asin(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 == "acos"){ secondNum = screen.getText(); secondParse = Double.parseDouble(secondNum); oppAnswer = Math.acos(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 == "x³"){ secondNum = screen.getText(); secondParse = Double.parseDouble(secondNum); oppAnswer = secondParse * secondParse * secondParse; writeAnswer = Double.toString(oppAnswer); screen.setText(writeAnswer); } if (actionCommand == "atan"){ secondNum = screen.getText(); secondParse = Double.parseDouble(secondNum); oppAnswer = Math.atan(secondParse); writeAnswer = Double.toString(oppAnswer); screen.setText(writeAnswer); } if (actionCommand == "log"){ secondNum = screen.getText(); secondParse = Double.parseDouble(secondNum); oppAnswer = Math.log10(secondParse); writeAnswer = Double.toString(oppAnswer); screen.setText(writeAnswer); } if (actionCommand == "\u03C0"){ screen.setText("3.1415926535897932384626433832795"); } 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() { Main.frame = new JFrame("Calculator"); frame.setLayout(new BorderLayout()); frame.getContentPane().add(new Main().getComponent()); Main.screenPanel = new JPanel(); //panel to hold screen Main.screenPanel.setLayout(new GridLayout(2, 1)); Main.screen = new JTextField(20); Main.menu = new JMenu("File"); Main.view = new JMenu("View"); Main.menuBar = new JMenuBar(); Main.edit = new JMenu("Edit"); edit.add(copy); screen.setBackground(Color.blue); menu.add(exit); menu.add(about); menuBar.add(menu); menuBar.add(edit); menuBar.add(view); view.add(advanced); view.add(simple); log = new JButton("Log"); Main.screenPanel.add(Main.menuBar); Main.screenPanel.add(screen); frame.add(Main.screenPanel, BorderLayout.NORTH); advancedButtons = new JPanel(new GridLayout(5,2)); //pain in the ass gridlayout manager log = new JButton("log"); sin = new JButton("sin"); insin = new JButton("asin"); cos = new JButton("cos"); incos = new JButton("acos"); tan = new JButton("tan"); intan = new JButton("atan"); cube = new JButton("x³"); pi = new JButton("\u03C0"); square = new JButton("x²"); advancedButtons.add(log); advancedButtons.add(square); advancedButtons.add(sin); advancedButtons.add(insin); advancedButtons.add(cos); advancedButtons.add(incos); advancedButtons.add(tan); advancedButtons.add(intan); advancedButtons.add(cube); advancedButtons.add(pi); sin.addActionListener(Main.buttonListener); cos.addActionListener(Main.buttonListener); tan.addActionListener(Main.buttonListener); insin.addActionListener(Main.buttonListener); incos.addActionListener(Main.buttonListener); intan.addActionListener(Main.buttonListener); log.addActionListener(Main.buttonListener); square.addActionListener(Main.buttonListener); cube.addActionListener(Main.buttonListener); pi.addActionListener(Main.buttonListener); sin.setToolTipText("Calculates the Sine of a number"); cos.setToolTipText("Calculates the Cosine of a number"); tan.setToolTipText("Calculates the Tangent of a number"); insin.setToolTipText("Calculates the Inverse Sine of a number"); incos.setToolTipText("Calculates the Inverse Cosine of a number"); intan.setToolTipText("Calculates the Inverse Tangent of a number"); log.setToolTipText("Calculates the Logarithm of a number"); square.setToolTipText("Squares a number"); cube.setToolTipText("Cubes a number"); frame.add(advancedButtons,BorderLayout.EAST); advanced.setSelected(false); simple.setSelected(true); advancedButtons.setVisible(false); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { try{ UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } 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(); } }); } }
-
1) Please only post compilable code relevant to your question. You are expecting volunteers to go through tons of code here most completely unrelated to the problem at hand. If you have a question about app layout and color, then it would be wise for you to only post code that affects layout and color. In general the less code you post, the greater the chances are that someone will read it. I would recommend though that you post enough code so that the app compiles and runs. It helps for us to "see" the problem when dealing with problems that involve visual representations of things.
2) If you are going to ask for advice here, it would be wise to heed advice given to you. I think you were told before about using "==" when comparing Strings, I am right?
3) You're using GridLayout, correct? Have a look at the GridLayout API and it will answer your main question.
4) As for your other question, you may wish to look at setting the opaque property on components, but I'm not sure as I can't bring myself to look at all that code.
5) The code that I did see though had an awful lot of static modifiers. What's up with that?Last edited by Fubarable; 10-17-2008 at 02:45 AM.
- 10-17-2008, 04:08 AM #3
Member
- Join Date
- Sep 2008
- Posts
- 31
- Rep Power
- 0
the == works fine in my script, i fixed most of the problems i had, but i have to import a .jar file into netbeans with a UI manager theme that i use. whenever i compile it i get that stupid lib folder and the calculator.jar needs this lib folder to work, is there any way i can make it so that i don't need the annoying lib folder with netbeans?
- 10-18-2008, 10:47 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 10-18-2008, 03:14 PM #5
Member
- Join Date
- Sep 2008
- Posts
- 31
- Rep Power
- 0
- 10-18-2008, 09:54 PM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 10-19-2008, 02:51 AM #7
Member
- Join Date
- Sep 2008
- Posts
- 31
- Rep Power
- 0
in order to have the creme look and feel, i need to include an external jar file, and when netbeans compiles it, there is a regular jar file, and the external jar file is located in the /lib folder. is there a way to combine the external jar file into the regular one so that i only have one jar file. if i move the external jar file away from the main jar file, the main one still works but goes back to the default theme, is there any way to combine the external jar into the main jar?
- 10-19-2008, 05:39 AM #8
Member
- Join Date
- Sep 2008
- Posts
- 31
- Rep Power
- 0
- 10-20-2008, 10:44 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
So you have solve the question.
Actually you can make a single jar file if you know exactly how those files are related. I mean what's the main class to execute those two jar files. If so extract all files in the jar bundle and build a new one.
Similar Threads
-
panel positioning
By shwein in forum New To JavaReplies: 4Last Post: 09-09-2008, 05:15 PM -
add image on panel
By samiksha.goel in forum AWT / SwingReplies: 4Last Post: 08-02-2008, 07:38 PM -
Why the panel text changed?
By ottawalyli in forum SWT / JFaceReplies: 0Last Post: 12-16-2007, 04:16 PM -
Help with drag from panel
By fernando in forum AWT / SwingReplies: 2Last Post: 08-07-2007, 10:19 PM -
Help with spacing in java
By barney in forum New To JavaReplies: 1Last Post: 07-31-2007, 08:03 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks