I'm trying to make a calculator as a fun project. But as I try to make it look like a... calculator, it just turns out like one big grid, as follows:
I've tried to follow along with whatever I found on the Internet, but that was a big bust.
Can anyone help me out trying to separate the JTextField so it doesn't do this, and it can be in it's own row?
Here's the code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Main extends JFrame {
public Main() {
// Row = ->
// Column = ^
Container cp = getContentPane();
cp.setLayout(new GridLayout(3, 0));
JTextField txtCalc = new JTextField("0");
txtCalc.setHorizontalAlignment(SwingConstants.RIGHT);
JButton btn0 = new JButton("0");
JButton btn1 = new JButton("1");
JButton btn2 = new JButton("2");
JButton btn3 = new JButton("3");
JButton btn4 = new JButton("4");
JButton btn5 = new JButton("5");
JButton btn6 = new JButton("6");
JButton btn7 = new JButton("7");
JButton btn8 = new JButton("8");
JButton btn9 = new JButton("9");
JButton btn10 = new JButton("10");
cp.add(txtCalc);
cp.add(btn0);
cp.add(btn1);
cp.add(btn2);
cp.add(btn3);
cp.add(btn4);
cp.add(btn5);
cp.add(btn6);
cp.add(btn7);
cp.add(btn8);
cp.add(btn9);
cp.add(btn10);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Calculator");
setSize(600, 600);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(Main::new);
}
}
Edit:
Now I have the answer! This is my fixed code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Main extends JFrame {
public Main() {
Container cp = getContentPane();
cp.setLayout(new GridLayout(3, 0));
JPanel mainPanel = new JPanel(new BorderLayout());
JPanel buttonPanel = new JPanel(new GridLayout(3, 0));
setContentPane(border);
JTextField txtCalc = new JTextField("0");
txtCalc.setHorizontalAlignment(SwingConstants.RIGHT);
txtCalc.setEditable(true);
JButton btn1 = new JButton("1");
JButton btn2 = new JButton("2");
JButton btn3 = new JButton("3");
JButton btn4 = new JButton("4");
JButton btn5 = new JButton("5");
JButton btn6 = new JButton("6");
JButton btn7 = new JButton("7");
JButton btn8 = new JButton("8");
JButton btn9 = new JButton("9");
JButton btn0 = new JButton("0");
mainPanel.add(buttonPanel, BorderLayout.CENTER);
mainPanel.add(txtCalc, BorderLayout.NORTH);
buttonPanel.add(btn1);
buttonPanel.add(btn2);
buttonPanel.add(btn3);
buttonPanel.add(btn4);
buttonPanel.add(btn5);
buttonPanel.add(btn6);
buttonPanel.add(btn7);
buttonPanel.add(btn8);
buttonPanel.add(btn9);
buttonPanel.add(btn0);
public static void main(String[] args) {
SwingUtilities.invokeLater(Main::new);
}
}
Have your main panel use a BorderLayout. Put the JTextField in the North position. Put your buttons in a panel that uses GridLayout, then add that panel to your main panel in the Center position.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Main extends JFrame {
public Main() {
JPanel mainPanel = new JPanel(new BorderLayout());
setContentPane(mainPanel);
JTextField txtCalc = new JTextField("0");
txtCalc.setHorizontalAlignment(SwingConstants.RIGHT);
JPanel buttonPanel = new JPanel(new GridLayout(3, 0));
JButton btn0 = new JButton("0");
JButton btn1 = new JButton("1");
JButton btn2 = new JButton("2");
JButton btn3 = new JButton("3");
JButton btn4 = new JButton("4");
JButton btn5 = new JButton("5");
JButton btn6 = new JButton("6");
JButton btn7 = new JButton("7");
JButton btn8 = new JButton("8");
JButton btn9 = new JButton("9");
buttonPanel.add(txtCalc);
buttonPanel.add(btn0);
buttonPanel.add(btn1);
buttonPanel.add(btn2);
buttonPanel.add(btn3);
buttonPanel.add(btn4);
buttonPanel.add(btn5);
buttonPanel.add(btn6);
buttonPanel.add(btn7);
buttonPanel.add(btn8);
buttonPanel.add(btn9);
mainPanel.add(txtCalc, BorderLayout.NORTH);
mainPanel.add(buttonPanel, BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Calculator");
setSize(600, 600);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(Main::new);
}
}
Related
I am making a vending machine in which there are 6 slots within the vending machine, each with its own soda. We are to have an arraylist of enums for the brands of the sodas. I have created the buttons for each of the canslots but do not know how to assign an arraylist value for one can slot to the appropriate button as well as to have once you click the button for the canslot to decrease by 1 until it is empty.
My code for the actual vending machine is here:
public class VendingFrame extends JFrame {
private ArrayList<CanSlot> CanSlots = new ArrayList<>();
public VendingFrame() {
CanSlots.add(new CanSlot(Brand.PEPSI));
CanSlots.add(new CanSlot(Brand.COKE));
CanSlots.add(new CanSlot(Brand.SUNKIST));
CanSlots.add(new CanSlot(Brand.DIETPEPSI));
CanSlots.add(new CanSlot(Brand.MTDEW));
CanSlots.add(new CanSlot(Brand.SPRITE));
JPanel panel = new JPanel();
JButton button = new JButton("Pepsi");
button.setPreferredSize(new Dimension(100, 80));
JButton button2 = new JButton("Coke");
button2.setPreferredSize(new Dimension(100, 80));
JButton button3 = new JButton("Diest Pepsi");
button3.setPreferredSize(new Dimension(100, 80));
JButton button4 = new JButton("Sunkist");
button4.setPreferredSize(new Dimension(100, 80));
JButton button5 = new JButton("Mountain Dew");
button5.setPreferredSize(new Dimension(100, 80));
JButton button6 = new JButton("Sprite");
button6.setPreferredSize(new Dimension(100, 80));
JPanel picpanel = new JPanel();
JPanel buttonPanel = new JPanel();
JLabel label = new JLabel();
setLayout(new BorderLayout());
buttonPanel.setLayout(new GridLayout(6, 1));
add(panel);
picpanel.add(label);
label.setIcon(new javax.swing.ImageIcon("C:\\Users\\iacol\\Desktop\\cans.jpg.jpg"));
buttonPanel.add(button);
button.addActionListener(new ClickListener());
buttonPanel.add(button2);
button2.addActionListener(new ClickListener2());
buttonPanel.add(button3);
button3.addActionListener(new ClickListener3());
buttonPanel.add(button4);
button4.addActionListener(new ClickListener4());
buttonPanel.add(button5);
button5.addActionListener(new ClickListener5());
buttonPanel.add(button6);
button6.addActionListener(new ClickListener6());
add(picpanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.EAST);
setSize(700, 700);
setTitle("Vending Machine");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
}
You can make, HashMap<Button, List<Product>>();
Using how to id the Button object and retrieve the button list on the value.
I want to add labels and buttons above and below the border layout. How can I do that? Here is what I did:
import java.awt.*;
import javax.swing.*;
class homework{
public static void main(String[] args) {
JFrame frame= new JFrame("border layout");
frame.setVisible(true);
JLabel label=new JLabel("Container of BorderLayout");
JButton button1 = new JButton("NORTH");
JButton button2 = new JButton("SOUTH");
JButton button3 = new JButton("EAST");
JButton button5 = new JButton("CENTER");
JButton button4 = new JButton("WEST");
JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
frame.add(panel2);
label.setLayout(new FlowLayout(0));
panel2.add(label);
panel1.setLayout(new BorderLayout());
panel1.add(button1,BorderLayout.NORTH);
panel1.add(button2,BorderLayout.SOUTH);
panel1.add(button3,BorderLayout.EAST);
panel1.add(button4,BorderLayout.WEST);
panel1.add(button5,BorderLayout.CENTER);
frame.add(panel1);
frame.pack();
}
}
Above and below of border layout, set new 2 containers (for example JPanel) and make them flow layout. enter image description here
JPanel panel = new JPanel();
frame.getContentPane().add(panel, BorderLayout.NORTH);
JRadioButton rdbtnNewRadioButton = new JRadioButton("New radio button");
panel.add(rdbtnNewRadioButton);
JRadioButton rdbtnNewRadioButton_1 = new JRadioButton("New radio button");
panel.add(rdbtnNewRadioButton_1);
JPanel panel_1 = new JPanel();
frame.getContentPane().add(panel_1, BorderLayout.SOUTH);
JLabel lblNewLabel = new JLabel("New label");
panel_1.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("New label");
panel_1.add(lblNewLabel_1);
add something like that before frame.pack(); code.
Actually what do you mean by above and below? Do you mean north and south? If It is you should have something like this enter image description here
and you should write code this way
import java.awt.*;
class homework{
public static void main(String[] args) {
JFrame frame= new JFrame("border layout");
frame.setVisible(true);
JLabel label=new JLabel("Container of BorderLayout");
JButton button3 = new JButton("EAST");
JButton button5 = new JButton("CENTER");
JButton button4 = new JButton("WEST");
JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
frame.getContentPane().add(panel2);
label.setLayout(new FlowLayout(0));
panel2.add(label);
panel1.setLayout(new BorderLayout());
panel1.add(button3,BorderLayout.EAST);
panel1.add(button4,BorderLayout.WEST);
panel1.add(button5,BorderLayout.CENTER);
frame.getContentPane().add(panel1);
JPanel panel = new JPanel();
panel1.add(panel, BorderLayout.NORTH);
JLabel lblNewLabel = new JLabel("New label");
panel.add(lblNewLabel);
JRadioButton rdbtnNewRadioButton = new JRadioButton("New radio button");
panel.add(rdbtnNewRadioButton);
JPanel panel_1 = new JPanel();
panel1.add(panel_1, BorderLayout.SOUTH);
JLabel lblNewLabel_1 = new JLabel("New label");
panel_1.add(lblNewLabel_1);
JRadioButton rdbtnNewRadioButton_1 = new JRadioButton("New radio button");
panel_1.add(rdbtnNewRadioButton_1);
frame.pack();
}
}
In two conditions, you should add two containers into your code and make them flow layout.
I'm fairly new to Swing and GUIs, and so far, only the window will appear, but none of the components will be visible. What can I do about this? Is there something wrong with the visibility or is it with a container?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PhoneCaller
{
JButton button1;
JButton button2;
JButton button3;
JButton button4;
JButton button5;
JButton button6;
JButton button7;
JButton button8;
JButton button9;
JButton buttonDash;
JButton button0;
JButton dialButton;
String phoneNum = "";
public static void main (String[] args)
{
new PhoneCaller();
}
public PhoneCaller()
{
JFrame myFrame = new JFrame();
myFrame.setTitle("Dialer");
myFrame.setSize(200, 250);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel myPanel = new JPanel(new BorderLayout(10,10));
myPanel.setLayout(new BoxLayout(myPanel, BoxLayout.Y_AXIS));
myFrame.add(myPanel);
JPanel myPanel2 = new JPanel();
myPanel2.setLayout(new FlowLayout());
JLabel lab = new JLabel("Enter the number to dial");
myPanel2.add(lab);
JPanel myPanel3 = new JPanel();
myPanel3.setLayout(new GridLayout(4,3,5,5));
button1 = new JButton ("1");
myPanel3.add(button1);
button2 = new JButton ("2");
myPanel3.add(button2);
button3 = new JButton ("3");
myPanel3.add(button3);
button4 = new JButton ("4");
button5 = new JButton ("5");
button6 = new JButton ("6");
button7 = new JButton ("7");
button8 = new JButton ("8");
button9 = new JButton ("9");
button0 = new JButton ("0");
buttonDash = new JButton ("-");
myPanel3.add(button4);
myPanel3.add(button5);
myPanel3.add(button6);
myPanel3.add(button7);
myPanel3.add(button8);
myPanel3.add(button9);
myPanel3.add(button0);
myPanel3.add(buttonDash);
myFrame.setVisible(true);
}
}
I think you forgot to add myPanel2 and myPanel3 inside myPanel.
myPanel.add(myPanel2);
myPanel.add(myPanel3);
You forgot to add myPanel2 and myPanel3 to your JFrame, add the following snippet to the end of your code
myFrame.add(myPanel2);
myFrame.add(myPanel3);
First time posting so go easy on me.
I am new to Java and am trying to get 3 JPanels to line up on top of each other. The first image is how I want it to look and it does sometimes when I run the program but as you can see by the other images it doesn't line up every time I run it. Sometimes not even showing some of the images/components.
So how can I get three JPanels to line up one after the other vertically?
import java.awt.event.ActionListener;
import java.awt.*;
import javax.swing.*;
public class FrameMain {
static final int MY_MINIMUM = 0;
static final int MY_MAXIMUM = 100;
public static void main(String[] args) {
JFrame frame1 = new JFrame("Harvest Frame Test");
frame1.setVisible(true);
frame1.setSize(800,700);
frame1.setResizable(false);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Container Panel
JPanel container = new JPanel();
container.setSize(800,700);
container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
frame1.add(container);
//First Panel
JPanel panel1 = new JPanel();
panel1.setAlignmentX( Component.LEFT_ALIGNMENT );//0.0
container.add(panel1);
JButton button1 = new JButton("Add Water");
panel1.add(button1);
JButton button2 = new JButton("Add Food");
panel1.add(button2);
JButton button3 = new JButton("Add Medicine");
panel1.add(button3);
ImageIcon image = new ImageIcon("C:/Users/Nick/Documents/EclipseArt/plant.gif");
JLabel imagelabel = new JLabel(image);
panel1.add(imagelabel);
JProgressBar pbar = new JProgressBar();
pbar.setMinimum(MY_MINIMUM);
pbar.setMaximum(MY_MAXIMUM);
// add to JPanel
panel1.add(pbar);
// Second Panel
JPanel panel2 = new JPanel();
panel2.setAlignmentX( Component.LEFT_ALIGNMENT );//0.0
container.add(panel2);
JButton button4 = new JButton("Add Water");
panel2.add(button4);
JButton button5 = new JButton("Add Food");
panel2.add(button5);
JButton button6 = new JButton("Add Medicine");
panel2.add(button6);
ImageIcon image1 = new ImageIcon("C:/Users/Nick/Documents/EclipseArt/plant.gif");
JLabel imagelabel1 = new JLabel(image1);
panel2.add(imagelabel1);
JProgressBar pbar1 = new JProgressBar();
pbar1.setMinimum(MY_MINIMUM);
pbar1.setMaximum(MY_MAXIMUM);
// add to JPanel
panel2.add(pbar1);
// Third Panel
JPanel panel3 = new JPanel();
panel3.setAlignmentX( Component.LEFT_ALIGNMENT );//0.0
container.add(panel3);
JButton button7 = new JButton("Add Water");
panel3.add(button7);
JButton button8 = new JButton("Add Food");
panel3.add(button8);
JButton button9 = new JButton("Add Medicine");
panel3.add(button9);
ImageIcon image2 = new ImageIcon("C:/Users/Nick/Documents/EclipseArt/plant.gif");
JLabel imagelabel2 = new JLabel(image2);
panel3.add(imagelabel2);
JProgressBar pbar2 = new JProgressBar();
pbar2.setMinimum(MY_MINIMUM);
pbar2.setMaximum(MY_MAXIMUM);
// add to JPanel
panel3.add(pbar2);
}
//static class Action implements ActionListener {
//public void actionPerformed (ActionEvent e){
//}
//}
}
Move the frame1.setVisible(true); all the way to the bottom. Changing Components on a frame that is already visible can cause issues.
I am trying to make a board game, I have Player 1 label and player 2 label set to the east and west. I am having trouble centre my 3 x 10 buttons. And is there any simpler way of making all 30 buttons at once? Below is my Code:
JPanel panel = new JPanel();
JButton button1 = new JButton();JButton button2 = new JButton();
JButton button3 = new JButton();JButton button4 = new JButton();
JButton button5 = new JButton();JButton button6 = new JButton();
JButton button7 = new JButton();JButton button8 = new JButton();
JButton button9 = new JButton();JButton button10 = new JButton();
JButton button11 = new JButton();JButton button12 = new JButton();
JButton button13 = new JButton();JButton button14 = new JButton();
JButton button15 = new JButton();JButton button16 = new JButton();
JButton button17 = new JButton();JButton button18 = new JButton();
JButton button19 = new JButton();JButton button20 = new JButton();
JButton button21 = new JButton();JButton button22 = new JButton();
JButton button23 = new JButton();JButton button24 = new JButton();
JButton button25 = new JButton();JButton button26 = new JButton();
JButton button27 = new JButton();JButton button28 = new JButton();
JButton button29 = new JButton();JButton button30 = new JButton();
panel.add(button1);panel.add(button2);panel.add(button3);panel.add(button4);
panel.add(button5);panel.add(button6);panel.add(button7);panel.add(button8);
panel.add(button9);panel.add(button10);panel.add(button11);panel.add(button12);
panel.add(button13);panel.add(button14);panel.add(button15);panel.add(button16);
panel.add(button17);panel.add(button18);panel.add(button19);panel.add(button20);
panel.add(button21);panel.add(button22);panel.add(button23);panel.add(button24);
panel.add(button25);panel.add(button26);panel.add(button27);panel.add(button28);
panel.add(button29);panel.add(button30);
frame.add(panel);
Panel p = new Panel();
p.setLayout(new BorderLayout());
p.add(new Button("Throw dice"), BorderLayout.SOUTH);
//p.add(new Button("dice Draw"), BorderLayout.SOUTH);
p.add(new Label("Player 1"), BorderLayout.EAST);
p.add(new Label("Player 2"), BorderLayout.WEST);
frame.add(p);
panel.setLayout(new GridLayout(3,10));
panel.setSize(new Dimension(500, 200));
frame.setSize(new Dimension(600, 300));
}
}
you can make arrays of JButtons and edit them simply by creating a table of JButtons, and use them with a GridLayout cenetred in Borderlayout.CENTER :
JButton [] buttons = new JButton[n];
for(int i=0;i<n;i++){
buttons[i] = new JButton("label "+ i);
buttons[i].set...
buttons[i].set...
gridlayout.add(buttons[i]);
}
borderlayout.add(gridlayout, BorderLayout.CENTER);
panel.setLayout(borderlayout);
hope this helps.
A way to do it is:
panel.setLayout(new GridLayout(3,10));
panel.setSize(new Dimension(500, 200));
//Add this line
panel.setLocation((frame.getWidth()-panel.getWidth())/2, 0); // 0 is just the Y location
frame.setSize(new Dimension(600, 300));
Look into relative layout to manage all your layouts https://stackoverflow.com/a/5346794/643500
Keep in mind that you want all pieces to layout nicely with each other, otherwise issues can be a pain to deal with. Dahmad Boutfounast's solution is one of those nice ones to have.
And definitely use a list\array to manage all those.