hi there i am trying to make a calculator with coding sizes,layouts etc. by myself (trying to not use NetBeans and it is not a homework). but i am facing with a problem about empty spaces. i have a TextArea and Buttons but as you can see below i cant handle this space problem. here is my code,
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
public class calculator extends JFrame {
public calculator(){
initComponents();
}
private void initComponents(){
JPanel panelScreen = new JPanel(new GridLayout(0,1));
JTextArea screen = new JTextArea();
panelScreen.add(screen);
JFrame frame = new JFrame("CALCULATOR");
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panelButtons = new JPanel(new GridLayout(3,3));
JButton oneButton = new JButton("1");
panelButtons.add(oneButton);
JButton twoButton = new JButton("2");
panelButtons.add(twoButton);
JButton threeButton = new JButton("3");
panelButtons.add(threeButton);
JButton fourButton = new JButton("4");
panelButtons.add(fourButton);
JButton fiveButton = new JButton("5");
panelButtons.add(fiveButton);
JButton sixButton = new JButton("6");
panelButtons.add(sixButton);
JButton sevenButton = new JButton("7");
panelButtons.add(sevenButton);
JButton eightButton = new JButton("8");
panelButtons.add(eightButton);
JButton nineButton = new JButton("9");
panelButtons.add(nineButton);
frame.getContentPane().add(panelScreen, BorderLayout.NORTH);
//frame.getContentPane().add(new JSeparator(), BorderLayout.CENTER);
frame.getContentPane().add(panelButtons, BorderLayout.SOUTH);
frame.setBounds(50, 50, 500, 500);
frame.setResizable(false);
//frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
new calculator();
}
}
and this the picture of programme;
i appreciate if you can help me. anyway thanks :)
A few suggestions:
Don't set the JFrame's size, and in fact don't set any sizes.
Call pack to all the components to set their own sizes.
If you want the buttons bigger, consider changing the size of their fonts.
e.g.,
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.*;
public class Calc2 {
public static final String[][] BUTTON_TEXTS = {
{"7", "8", "9", "+"},
{"4", "5", "6", "-"},
{"1", "2", "3", "*"},
{"0", ".", "/", "="}
};
public static final Font BTN_FONT = new Font(Font.SANS_SERIF, Font.BOLD, 24);
private static void createAndShowGui() {
JTextField field = new JTextField(10);
field.setFont(BTN_FONT.deriveFont(Font.PLAIN));
JPanel btnPanel = new JPanel(new GridLayout(BUTTON_TEXTS.length,
BUTTON_TEXTS[0].length));
for (int i = 0; i < BUTTON_TEXTS.length; i++) {
for (int j = 0; j < BUTTON_TEXTS[i].length; j++) {
JButton btn = new JButton(BUTTON_TEXTS[i][j]);
btn.setFont(BTN_FONT);
btnPanel.add(btn);
}
}
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(field, BorderLayout.PAGE_START);
mainPanel.add(btnPanel, BorderLayout.CENTER);
JFrame frame = new JFrame("Calc2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
// no need to extend frame!
//public class Calculator extends JFrame {
public class Calculator {
public Calculator(){
initComponents();
}
private void initComponents(){
// I find it easier to create a panel and SET it as the content pane
JPanel gui = new JPanel(new BorderLayout(5,5));
// add some padding to the main GUI
gui.setBorder(new EmptyBorder(4,4,4,4));
// not needed if only a single compoinent is to be added!
//JPanel panelScreen = new JPanel(new GridLayout(0,1));
// add some constraints to make the output field bigger.
// if it is intended to be single line, a JTextField should be used.
JTextArea screen = new JTextArea(2,25);
gui.add(screen, BorderLayout.NORTH);
//panelScreen.add(screen);
JFrame frame = new JFrame("CALCULATOR");
frame.setContentPane(gui);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// add padding around the buttons
JPanel panelButtons = new JPanel(new GridLayout(3,3,4,4));
JButton oneButton = new JButton("1");
panelButtons.add(oneButton);
JButton twoButton = new JButton("2");
panelButtons.add(twoButton);
JButton threeButton = new JButton("3");
panelButtons.add(threeButton);
JButton fourButton = new JButton("4");
panelButtons.add(fourButton);
JButton fiveButton = new JButton("5");
panelButtons.add(fiveButton);
JButton sixButton = new JButton("6");
panelButtons.add(sixButton);
JButton sevenButton = new JButton("7");
panelButtons.add(sevenButton);
JButton eightButton = new JButton("8");
panelButtons.add(eightButton);
JButton nineButton = new JButton("9");
panelButtons.add(nineButton);
//frame.getContentPane().add(new JSeparator(), BorderLayout.CENTER);
// Add the buttons to the CENTER and they will
// fill whatever space they are provided.
gui.add(panelButtons, BorderLayout.CENTER);
//frame.setBounds(50, 50, 500, 500);
//frame.setResizable(false);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new Calculator();
}
});
}
}
You might like to study this example that follows the suggestions of #HFOE and #mre. Note that "size" appears nowhere in the code.
Read Laying Out Components Within a Container
Implement appropriate layout(s)
EDIT -
Quick solution - replace the JFrame layout manager with BoxLayout (i.e. setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS))).
Related
A frame which includes panels where buttons are located in a column on the left side of the frame:
I tried to make this picture in real Java code but failed.
I can't find the right solution for this issue...
BorderLayout borderLayout = new BorderLayout();
GridBagLayout gridBagLayout = new GridBagLayout();
GridLayout gridLayout = new GridLayout();
CardLayout cardLayout = new CardLayout();
JPanel panelMain = new JPanel();
JPanel panelLeft = new JPanel();
JPanel panelInnerLeft = new JPanel();
JPanel panelRight = new JPanel();
panelMain.setLayout(borderLayout);
panelLeft.setLayout(gridBagLayout);
panelInnerLeft.setLayout(gridLayout);
panelRight.setLayout(cardLayout);
button1 = new JButton("Button 1");
button2 = new JButton("Button 2");
button3 = new JButton("Button 3");
button4 = new JButton("Button 4");
panelInnerLeft.add(button1,gridLayout);
panelInnerLeft.add(button2,gridLayout);
panelInnerLeft.add(button3,gridLayout);
panelInnerLeft.add(button4,gridLayout);
panelLeft.add(panelInnerLeft);
panelMain.add(panelLeft);
panelMain.add(panelRight);
add(panelMain);
This is the exact code used to make that screenshot.
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class CompoundLayout01 {
private JComponent ui = null;
CompoundLayout01() {
initUI();
}
public void initUI() {
if (ui!=null) return;
ui = new JPanel(new BorderLayout(4,4));
ui.setBorder(new TitledBorder("BorderLayout"));
CardLayout cardLayout = new CardLayout();
JPanel cards = new JPanel(cardLayout);
cards.setBorder(new TitledBorder("CardLayout"));
ui.add(cards);
JPanel lineStart = new JPanel(new GridBagLayout());
lineStart.setBorder(new TitledBorder("GridBagLayout"));
// will appear on the left, in a LTR text orientation locale
ui.add(lineStart, BorderLayout.LINE_START);
JPanel buttonsCentered = new JPanel(new GridLayout(0, 1, 10, 10));
buttonsCentered.setBorder(new TitledBorder("GridLayout"));
// as single component added w/no constraint, will be centered
lineStart.add(buttonsCentered);
for (int ii=1; ii<5; ii++) {
JButton b = new JButton("Button " + ii);
buttonsCentered.add(b);
}
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
CompoundLayout01 o = new CompoundLayout01();
JFrame f = new JFrame(o.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}
panelMain.setLayout(borderLayout);
...
panelMain.add(panelLeft);
panelMain.add(panelRight);
Read the Swing tutorial on Layout Managers.
It will show you how to use the BorderLayout.
You need to specify the "constraint" when you add each component. Like BorderLayout.LINE_START, or BorderLayout.CENTER.
The following is also wrong:
panelInnerLeft.setLayout(gridLayout);
...
panelInnerLeft.add(button1,gridLayout);
panelInnerLeft.add(button2,gridLayout);
panelInnerLeft.add(button3,gridLayout);
panelInnerLeft.add(button4,gridLayout);
Read the section on GridLayout. A GridLayout does not require a constraint.
I don't know if you need some specific layout, but if you want more control, you can take a look at my example. Note that I have to handle some sizes myself. Also if you need even more control you can always use null as layout.
import java.awt.FlowLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.SwingUtilities;
public class NewJFrame extends javax.swing.JFrame {
private JPanel jPanel1;
private JButton jButton1;
private JButton jButton2;
private JButton jButton3;
private JPanel jPanel2;
private JButton jButton4;
private JPanel ButtonsPanel;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
NewJFrame inst = new NewJFrame();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}
public NewJFrame() {
super();
initGUI();
}
private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
jPanel1 = new JPanel();
getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.setLayout(null);
ButtonsPanel = new JPanel();
FlowLayout ButtonsPanelLayout = new FlowLayout();
jPanel1.add(ButtonsPanel);
ButtonsPanel.setLayout(ButtonsPanelLayout);
ButtonsPanel.setBounds(12, 23, 104, 215);
ButtonsPanel.setBorder(
BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
jButton1 = new JButton();
ButtonsPanel.add(jButton1);
jButton1.setText("jButton1");
jButton1.setPreferredSize(new java.awt.Dimension(85, 23));
jButton2 = new JButton();
ButtonsPanel.add(jButton2);
jButton2.setText("jButton2");
jButton2.setPreferredSize(new java.awt.Dimension(85, 23));
jButton3 = new JButton();
ButtonsPanel.add(jButton3);
jButton3.setText("jButton3");
jButton3.setPreferredSize(new java.awt.Dimension(85, 23));
jButton4 = new JButton();
ButtonsPanel.add(jButton4);
jButton4.setText("jButton4");
jButton4.setPreferredSize(new java.awt.Dimension(85, 23));
jPanel2 = new JPanel();
jPanel1.add(jPanel2);
jPanel2.setLayout(null);
jPanel2.setBounds(122, 23, 250, 215);
jPanel2.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
pack();
setSize(400, 300);
} catch (Exception e) {
e.printStackTrace();
}
}
}
I can't get my Jbuttons to show up on my JPanel or JFrame, I'm new to programming still and struggling.
SimpleCalculator:
package simplecalculator;
public class SimpleCalculator {
public static void main(String[] args) {
MyFrame aFrame = new MyFrame();
aFrame.setVisible(true);
}
}
MyFrame:
import javax.swing.*;
public class MyFrame extends JFrame {
public MyFrame(){
super();
this.setTitle("Simple Calculator");
this.setVisible(true);
MyPanel aPanel = new MyPanel();
this.getContentPane().add(aPanel);
aPanel.setVisible(true);
this.setSize(500, 300);
}
}
MyPanel:
package simplecalculator;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class MyPanel extends JPanel implements ActionListener {
int ans=0;
JTextField result = new JTextField();
JButton but0 = new JButton("0");
JButton but1 = new JButton("1");
JButton but2 = new JButton("2");
JButton but3 = new JButton("3");
JButton but4 = new JButton("4");
JButton but5 = new JButton("5");
JButton but6 = new JButton("6");
JButton but7 = new JButton("7");
JButton but8 = new JButton("8");
JButton but9 = new JButton("9");
public MyPanel() {
super();
result.setVisible(true);
result.setLocation(20, 20);
but0.setText("0");
but0.setVisible(true);
but0.setLocation(40, 40);
}
I'm trying to make a simple calculator but I'm stuck. Someone please help.
You need to add your buttons to your panel.
For example:
JPanel n;
n.add(but0);
n.add(but1);
etc...
Also try using a Layout with your calculator.
For example:
JPanel n = new JPanel(new GridLayout(1,4));
You can also use different layouts to suit your needs. Check this website out for more layouts: https://www.tutorialspoint.com/swing/swing_layouts.htm
So, I'm brand spankin' new to programming, so thanks in advance for your help. I'm trying to put this base 2 to base 10/base 10 to base 2 calculator I have made into a GUI. For the life of me I can't figure out how to nicely format it. I'm trying to make it look like the following: The two radio buttons on top, the input textfield bellow those, the convert button bellow that, the output field bellow that, and the clear button bellow that. Any ideas on how I can accomplish this?
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.*;
#SuppressWarnings("serial")
public class GUI extends JFrame implements ActionListener
{
private JTextField input;
private JTextField output;
private JRadioButton base2Button;
private JRadioButton base10Button;
private JButton convert;
private JButton clear;
private Container canvas = getContentPane();
private Color GRAY;
public GUI()
{
this.setTitle("Base 10-2 calc");
this.setLayout(new FlowLayout(FlowLayout.LEFT));
//this.setLayout(new GridLayout(2,2));
base2Button = new JRadioButton( "Convert to base 2");
base10Button = new JRadioButton( "Convert to base 10");
ButtonGroup radioGroup = new ButtonGroup();
radioGroup.add(base2Button);
radioGroup.add(base10Button);
JPanel radioButtonsPanel = new JPanel();
radioButtonsPanel.setLayout( new FlowLayout(FlowLayout.LEFT) );
radioButtonsPanel.add(base2Button);
radioButtonsPanel.add(base10Button);
canvas.add(radioButtonsPanel);
base2Button.setSelected( true );
base10Button.setSelected( true );
input = new JTextField(18);
//input = new JFormattedTextField(20);
canvas.add(input);
output = new JTextField(18);
//output = new JFormattedTextField(20);
canvas.add(output);
convert = new JButton("Convert!");
convert.addActionListener(this);
canvas.add(convert);
clear = new JButton("Clear");
clear.addActionListener(this);
canvas.add(clear);
output.setBackground(GRAY);
output.setEditable(false);
this.setSize(300, 200);
this.setVisible(true);
this.setLocation(99, 101);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
GUI app = new GUI();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
#Override
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if(s.equals("Convert!"))
{
String numS = input.getText();
int numI = Integer.parseInt(numS);
if(base2Button.isSelected())
{
output.setText(Integer.toBinaryString(Integer.valueOf(numI)));
}
if(base10Button.isSelected())
{
output.setText("" + Integer.valueOf(numS,2));
}
}
if(s.equals("Clear"))
{
input.setText("");
output.setText("");
}
}
}
For a simple layout, you could use a GridLayout with one column and then use a bunch of child panels with FlowLayout which align the components based on the available space in a single row. If you want more control, I'd suggest learning about the GridBagLayout manager which is a more flexible version of GridLayout.
public class ExampleGUI {
public ExampleGUI() {
init();
}
private void init() {
JFrame frame = new JFrame();
// Set the frame's layout to a GridLayout with one column
frame.setLayout(new GridLayout(0, 1));
frame.setPreferredSize(new Dimension(300, 300));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Child panels, each with FlowLayout(), which aligns the components
// in a single row, until there's no more space
JPanel radioButtonPanel = new JPanel(new FlowLayout());
JRadioButton button1 = new JRadioButton("Option 1");
JRadioButton button2 = new JRadioButton("Option 2");
radioButtonPanel.add(button1);
radioButtonPanel.add(button2);
JPanel inputPanel = new JPanel(new FlowLayout());
JLabel inputLabel = new JLabel("Input: ");
JTextField textField1 = new JTextField(15);
inputPanel.add(inputLabel);
inputPanel.add(textField1);
JPanel convertPanel = new JPanel(new FlowLayout());
JButton convertButton = new JButton("Convert");
convertPanel.add(convertButton);
JPanel outputPanel = new JPanel(new FlowLayout());
JLabel outputLabel = new JLabel("Output: ");
JTextField textField2 = new JTextField(15);
outputPanel.add(outputLabel);
outputPanel.add(textField2);
// Add the child panels to the frame, in order, which all get placed
// in a single column
frame.add(radioButtonPanel);
frame.add(inputPanel);
frame.add(convertPanel);
frame.add(outputPanel);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
ExampleGUI example = new ExampleGUI();
}
}
The end result:
I was wondering how I would move my buttons to the bottom of the frame/window.
What I have now is this:
And what I want it to look like is this:
Here is my code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Font;
public class checkbook{
static JButton Button[] = new JButton[8];
JLabel begin, accountName, balance;
JFrame frame;
JPanel jpanel, jpanel1;
Container contentPane;
private JTextField textAccount, textBalance;
public static void main(String[] args){
checkbook checkbook = new checkbook();
checkbook.startFrame();
}
checkbook(){
frame = new JFrame("Checkbook");
}
public void startFrame(){
String bottomButtons[] = {
"Create a New Account", "Load Trans from a file", "Add New Transactions", "Search Transactions",
"Sort Transactions", "View/Delete Transactions", "Backup Transactions", "Exit"
};
Container contentPane = frame.getContentPane();
contentPane.setLayout(new FlowLayout());
Font beginFont = new Font("Arial", Font.PLAIN + Font.BOLD, 22);
textAccount = new JTextField(" ", 15);
textBalance = new JTextField("0.0", 15);
begin = new JLabel("Use The Buttons below To Manage Transactions");
begin.setFont(beginFont);
contentPane.add(begin);
//Container contentPane1 = frame.getContentPane();
//contentPane1.setLayout(new FlowLayout());
JPanel jpanel = new JPanel();
accountName = new JLabel ("Account Name: ");
jpanel.add(accountName);
jpanel.add(textAccount);
balance = new JLabel ("Balance: ");
jpanel.add(balance);
jpanel.add(textBalance);
JPanel jpanel1 = new JPanel();
jpanel1.setLayout(new GridLayout(2,4));
for(int i = 0; i < 8; i++){
Button[i] = new JButton(bottomButtons[i]);
jpanel1.add(Button[i]);
//Button[i].addActionListener(AL);
}
frame.pack();
frame.setSize(800, 300);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.add(jpanel);
frame.add(jpanel1);
}
}
So I was wondering what would I do to move the buttons down to the bottom. Would I have to use a BorderLayout and use BorderLayout.SOUTH etc or is there a way to do this with the GridLayout I already have. If I have to use the BorderLayout how would I keep my buttons in order (2 rows, 4 columns).
Would I have to use a BorderLayout and use BorderLayout.SOUTH
Yes, but the best answer is for you to try it and see what happens. Why don't you?
If I have to use the BorderLayout how would I keep my buttons in order(2 rows, 4 columns).
Nest JPanels. Put the buttons in a GridLayout using JPanel, and place that JPanel BorderLayout.SOUTH, or better, BorderLayout.PAGE_END
e.g.,
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.*;
#SuppressWarnings("serial")
public class NestedPanels extends JPanel {
private static final String TITLE_TEXT = "Use The Buttons Below To Manage Transactions";
private static final String[] BTN_TEXTS = { "Create a New Account",
"Load a Trans from a File", "Add New Transactions",
"Search Transactions", "Sort Transactions",
"View/Delete Transactions", "Backup Transaction", "Exit" };
private static final int TITLE_POINTS = 24;
public NestedPanels() {
JLabel titleLabel = new JLabel(TITLE_TEXT, SwingConstants.CENTER);
titleLabel.setFont(titleLabel.getFont().deriveFont(Font.BOLD,
TITLE_POINTS));
JPanel titlePanel = new JPanel();
titlePanel.add(titleLabel); // put it in a JPanel so it will expand to fill BoxLayout
JPanel accountBalancePanel = new JPanel();
accountBalancePanel.add(new JLabel("Account Name:"));
accountBalancePanel.add(new JTextField(10));
accountBalancePanel.add(Box.createHorizontalStrut(20));
accountBalancePanel.add(new JLabel("Balance:"));
accountBalancePanel.add(new JTextField(10));
JPanel northPanel = new JPanel();
northPanel.setLayout(new BoxLayout(northPanel, BoxLayout.PAGE_AXIS));
northPanel.add(titlePanel);
northPanel.add(accountBalancePanel);
JPanel southBtnPanel = new JPanel(new GridLayout(2, 4, 1, 1));
for (String btnText : BTN_TEXTS) {
southBtnPanel.add(new JButton(btnText));
}
setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
setLayout(new BorderLayout());
add(northPanel, BorderLayout.NORTH);
add(Box.createRigidArea(new Dimension(400, 400))); // just an empty placeholder
add(southBtnPanel, BorderLayout.SOUTH);
}
private static void createAndShowGui() {
NestedPanels mainPanel = new NestedPanels();
JFrame frame = new JFrame("Nested Panels Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
Which displays as:
I have counted the curly braces and cannot figure out why the class body is incomplete.
Everytime I try to fix the class it messes the whole class up.
The problem is the very last class in the code. The very last curly brace is the one giving me trouble with the class. I'm using Eclipse to write this in.
Here is the code for the whole program:
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Stocks {
public static void main(String [] args) {
JFrame frame = new JFrame ("Java Stocks");
frame.setSize(700,700);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel (new GridBagLayout());
frame.add(panel);
frame.getContentPane().add(panel, BorderLayout.WEST);
GridBagConstraints c = new GridBagConstraints ();
JButton button1 = new JButton("Profile");
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(40, 40, 40, 40);
panel.add(button1, c);
button1.addActionListener(new Action());
}
static class Action implements ActionListener {
public void actionPerformed (ActionEvent e) {
JFrame frame2 = new JFrame("Your Stocks");
frame2.setVisible(true);
frame2.setSize(600,600);
JLabel label = new JLabel("Your Personal Stocks");
JPanel panel = new JPanel();
frame2.add(panel);
panel.add(label);
}
public static void main(String [] args) {
GridBagConstraints c = new GridBagConstraints ();
JButton button2 = new JButton("Market");
c.gridx = 0;
c.gridy = 1;
c.insets = new Insets(40, 40, 40, 40);
button2.addActionListener(new Action());
}
static class Action2 implements ActionListener {
public void actionPerformed (ActionEvent e) {
JFrame frame2 = new JFrame("Your Stocks");
frame2.setVisible(true);
frame2.setSize(600,600);
JLabel label = new JLabel("Your Personal Stocks");
JPanel panel = new JPanel();
frame2.add(panel);
panel.add(label);
}
public static void main(String [] args) {
GridBagConstraints c = new GridBagConstraints ();
JButton button3 = new JButton("Users");
c.gridx = 0;
c.gridy = 2;
c.insets = new Insets(40, 40, 40, 40);
button3.addActionListener(new Action());
}
static class Action3 implements ActionListener {
public void actionPerformed (ActionEvent e) {
JFrame frame2 = new JFrame("Your Stocks");
frame2.setVisible(true);
frame2.setSize(600,600);
JLabel label = new JLabel("Your Personal Stocks");
JPanel panel = new JPanel();
frame2.add(panel);
panel.add(label);
}
public static void main(String [] args) {
GridBagConstraints c = new GridBagConstraints ();
JButton button4 = new JButton("Notes");
c.gridx = 0;
c.gridy = 3;
c.insets = new Insets(40, 40, 40, 40);
button4.addActionListener(new Action());
}
static class Action4 implements ActionListener {
public void actionPerformed (ActionEvent e) {
JFrame frame2 = new JFrame("Your Stocks");
frame2.setVisible(true);
frame2.setSize(600,600);
JLabel label = new JLabel("Your Personal Stocks");
JPanel panel = new JPanel();
frame2.add(panel);
panel.add(label);
}
public static void main(String [] args) {
GridBagConstraints c = new GridBagConstraints ();
JButton button5 = new JButton("Information");
c.gridx = 0;
c.gridy = 4;
c.insets = new Insets(40, 40, 40, 40);
button5.addActionListener(new Action());
}
static class Action5 implements ActionListener {
public void actionPerformed (ActionEvent e) {
JFrame frame2 = new JFrame("Your Stocks");
frame2.setVisible(true);
frame2.setSize(600,600);
JLabel label = new JLabel("Your Personal Stocks");
JPanel panel = new JPanel();
frame2.add(panel);
panel.add(label);
}
Due to confusing indentation, it is not obvious that you are nesting your nested classes inside each other. Action5 is nested in Action4, which is nested in Action3, which is nested in Action2, which is nested in Action, which is nested in Stocks.
Either place 5 braces at the very end of the file, to close all your classes, or even better, nest them all directly in Stocks, not within each other.
Try to use crlt+shift + f to format your code
When you clean this up, your code should look like something like this:
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
class Action implements ActionListener {
public void actionPerformed (ActionEvent e) {
JFrame frame2 = new JFrame("Your Stocks");
frame2.setVisible(true);
frame2.setSize(600,600);
JLabel label = new JLabel("Your Personal Stocks");
JPanel panel = new JPanel();
frame2.add(panel);
panel.add(label);
}
}
public class Stocks {
public static void main(String [] args) {
JFrame frame = new JFrame ("Java Stocks");
frame.setSize(700,700);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel (new GridBagLayout());
frame.add(panel);
frame.getContentPane().add(panel, BorderLayout.WEST);
GridBagConstraints c = new GridBagConstraints ();
JButton button1 = new JButton("Profile");
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(40, 40, 40, 40);
panel.add(button1, c);
button1.addActionListener(new Action());
}
}
My advice #1: first learn to code with keyboard, not mouse.
My advice #2: if you aren't sure what static modifier is for and what are the implications, don't use it.