I learning java swing, and I'm a little confused with location of add() function
Style 1:
JButton b1 = new JButton("1");
add(b1);
JButton b2 = new JButton("2");
add(b2);
JButton b3 = new JButton("3");
add(b3);
JButton b4 = new JButton("4");
add(b4);
JButton b5 = new JButton("5");
add(b5);
Style 2:
JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
JButton b3 = new JButton("3");
JButton b4 = new JButton("4");
JButton b5 = new JButton("5");
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
I usually grouping add() function like style 2, But i'm worried that it's not recommended.
Is there any recommended style? or both is acceptable style?
Related
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);
}
}
In the below program I have created a button that when clicked should show another jframe with added components. But when I click on this button it does not show any components only a blank jframe appears with title.
What's my problem can anyone explain it?
b1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
JFrame f1 = new JFrame();
f1.setVisible(true);
f1.setSize(800, 700);
f1.setTitle("Calc");
f1.getContentPane().setLayout(new FlowLayout());
JTextField t1 = new JTextField(10);
JTextField t2 = new JTextField(10);
JTextField t3 = new JTextField(10);
JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
JButton b3 = new JButton("3");
JButton b4 = new JButton("4");
JButton b5 = new JButton("5");
JButton b6 = new JButton("6");
JButton b7 = new JButton("7");
JButton b8 = new JButton("8");
JButton b9 = new JButton("9");
JButton b10 = new JButton("0");
JButton b11 = new JButton("Add");
JButton b12 = new JButton("Sub");
JButton b13 = new JButton("Mul");
JButton b14 = new JButton("Div");
JButton b15 = new JButton("=");
JButton b16 = new JButton("CLR");
f.getContentPane().add(t1);
f.getContentPane().add(t2);
f.getContentPane().add(t3);
f.getContentPane().add(b1);
f.getContentPane().add(b2);
f.getContentPane().add(b3);
f.getContentPane().add(b4);
f.getContentPane().add(b5);
f.getContentPane().add(b6);
f.getContentPane().add(b7);
f.getContentPane().add(b8);
f.getContentPane().add(b9);
f.getContentPane().add(b10);
f.getContentPane().add(b11);
f.getContentPane().add(b12);
f.getContentPane().add(b13);
f.getContentPane().add(b14);
f.getContentPane().add(b15);
f.getContentPane().add(b16);
}
});
Possibly typo error in your case as address by the other answers.
Use f1 instead of f
Move the line f1.setVisible(true); below to the bottom when you are done with adding buttons to it, i.e. f1.getContentPane().add(b16);
In your code you have refrenced a JFrame as f1 and you are adding component to f so it will not add anything to your frame so,
try using to add in f1 instead of f and also move f1.setVisible(true); below after adding every component tof1 as i have shown below
b1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
JFrame f1 = new JFrame();
f1.setSize(800, 700);
f1.setTitle("Calc");
f1.getContentPane().setLayout(new FlowLayout());
JTextField t1 = new JTextField(10);
JTextField t2 = new JTextField(10);
JTextField t3 = new JTextField(10);
JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
JButton b3 = new JButton("3");
JButton b4 = new JButton("4");
JButton b5 = new JButton("5");
JButton b6 = new JButton("6");
JButton b7 = new JButton("7");
JButton b8 = new JButton("8");
JButton b9 = new JButton("9");
JButton b10 = new JButton("0");
JButton b11 = new JButton("Add");
JButton b12 = new JButton("Sub");
JButton b13 = new JButton("Mul");
JButton b14 = new JButton("Div");
JButton b15 = new JButton("=");
JButton b16 = new JButton("CLR");
f1.getContentPane().add(t1);
f1.getContentPane().add(t2);
f1.getContentPane().add(t3);
f1.getContentPane().add(b1);
f1.getContentPane().add(b2);
f1.getContentPane().add(b3);
f1.getContentPane().add(b4);
f1.getContentPane().add(b5);
f1.getContentPane().add(b6);
f1.getContentPane().add(b7);
f1.getContentPane().add(b8);
f1.getContentPane().add(b9);
f1.getContentPane().add(b10);
f1.getContentPane().add(b11);
f1.getContentPane().add(b12);
f1.getContentPane().add(b13);
f1.getContentPane().add(b14);
f1.getContentPane().add(b15);
f1.getContentPane().add(b16);
f1.setVisible(true);
}
});
I'm trying to remodel a phone keypad, however I cannot see the results without resizing the results window, which I dont want.
here is my code:
the code would run just fine before I entered the code for the display area. but after that, I have to continuously resize it(which I dont want) ive tried resizing the display area itself.
import java.awt.*;
import javax.swing.*;
import javax.swing.border.Border;
public class mobilePhone{
private JFrame keypadFrame;
private JPanel panels1;
private JTextArea displayArea;
private JButton b1;
private JButton b2;
private JButton b3;
private JButton b4;
private JButton b5;
private JButton b6;
private JButton b7;
private JButton b8;
private JButton b9;
private JButton b10;
private JButton b11;
private JButton b12;
private JButton b13;
private JButton b14;
private JButton b15;
public mobilePhone(){
Interface();
}
public void Interface(){
keypadFrame = new JFrame("Mobile Calculator");
keypadFrame.setVisible(true);
keypadFrame.setSize(270, 300);
keypadFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
keypadFrame.setResizable(false);
displayArea = new JTextArea(2,5);
displayArea.setBackground(Color.WHITE);
Border DABorder = BorderFactory.createLineBorder(Color.RED,4);
displayArea.setBorder(DABorder);
Font DAFont = new Font("ariel",Font.BOLD,30);
displayArea.setFont(DAFont);
displayArea.setLineWrap(true);
panels1 = new JPanel(new GridLayout(5,1));
panels1.setBackground(Color.DARK_GRAY);
Border panelBorder = BorderFactory.createLineBorder(Color.BLUE,5);
panels1.setBorder(panelBorder);
b1 = new JButton("7");
b2 = new JButton("8");
b3 = new JButton("9");
b4 = new JButton("4");
b5 = new JButton("5");
b6 = new JButton("6");
b7 = new JButton("1");
b8 = new JButton("2");
b9 = new JButton("3");
b10 = new JButton("*");
b11 = new JButton("0");
b12 = new JButton("#");
b13 = new JButton("CLEAR");
panels1.add(b1);
panels1.add(b2);
panels1.add(b3);
panels1.add(b4);
panels1.add(b5);
panels1.add(b6);
panels1.add(b7);
panels1.add(b8);
panels1.add(b9);
panels1.add(b10);
panels1.add(b11);
panels1.add(b12);
keypadFrame.add(panels1,BorderLayout.CENTER);
keypadFrame.add(displayArea, BorderLayout.NORTH);
keypadFrame.add(b13, BorderLayout.EAST);
}
public static void main(String[] args){
new mobilePhone();
}
}
Take keypadFrame.setVisible(true); and place it at the end of your Interface method (and then have a look at Code Conventions for the Java TM Programming Language, it will make it easier for people to read your code and for you to read others)
Alternatively, call revalidate and repaint on the frame's contentPane
I am facing problem in arranging my labels in the frame;
I just want someone to guide me in the right direction.
What i want to do is to create a JButton and place it the left half of the frame, while the right half will have JTextField in the north and 12 JButtonsat the bottom of the JTextField like the calculator.
this is my code
import java.awt.*;
import javax.swing.*;
public class Code {
JFrame f = new JFrame("The Front View of a Microwave Oven");
JPanel p1 = new JPanel(new BorderLayout());
JPanel p2 = new JPanel();
JPanel p3 = new JPanel(new GridLayout(4,3));
JPanel p4 = new JPanel(new BorderLayout());
JTextField text = new JTextField("Time to be displayed here");
JButton b = new JButton("Food to be placed here");
JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
JButton b3 = new JButton("3");
JButton b4 = new JButton("4");
JButton b5 = new JButton("5");
JButton b6 = new JButton("6");
JButton b7 = new JButton("7");
JButton b8 = new JButton("8");
JButton b9 = new JButton("9");
JButton b0 = new JButton("0");
JButton start = new JButton("Start");
JButton stop = new JButton ("Stop");
public void ui(){
p2.add(text, BorderLayout.NORTH);
p2.add(p3, BorderLayout.CENTER);
p4.add(b, BorderLayout.WEST);
p4.add(p2, BorderLayout.EAST);
p3.add(b1);
p3.add(b2);
p3.add(b3);
p3.add(b4);
p3.add(b5);
p3.add(b6);
p3.add(b7);
p3.add(b8);
p3.add(b9);
p3.add(b0);
p3.add(start);
p3.add(stop);
f.add(p4);
f.setSize(370, 300);
f.setVisible(true);
f.setLayout(new BorderLayout());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
it shows me the big button on the right correctly .. but how can i place the 12 buttons with the JTextField on the right of the JFrame?
LayOut Managers are the way to go for these issues. you can also look at this beginner program and you can also look at this Stackoverflow Post - just the code posted in question
Also you can set layout as null like panel.setLayout(null) and label.setBounds(10,10,20,100) to adjust position anywhere you want using x,y coordinates and hight and width. It is a simple way to do it.
But Layout Manager are mostly used and saves you from playing with pixels.
Hello this is really long I'm sorry My code works but how do I print multiple numbers to my label with just pressing buttons. My code isn't complete but I just want a push in the right direction. Thank you for any help you can give.
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
public class NumericKeyPadPanel extends JPanel {
private JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, clear;
private JLabel label1;
private JPanel primary, panel2, panel3, panel4;
public NumericKeyPadPanel() {
label1 = new JLabel();
primary = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
panel4 = new JPanel();
//Set up for the center grid
panel2.setLayout(new GridLayout(4, 3));
panel2.setBackground(Color.gray);
panel2.setBorder(BorderFactory.createLineBorder(Color.black, 4));
//set up for the top box
panel3.setBackground(Color.white);
panel3.setBorder(BorderFactory.createEtchedBorder());
panel3.add(label1);
//set up for the bottom
panel4.setBackground(Color.gray);
//set up for the buttons for center and bottom
ButtonListener listener1 = new ButtonListener();
ButtonListener listener2 = new ButtonListener();
ButtonListener listener3 = new ButtonListener();
ButtonListener listener4 = new ButtonListener();
ButtonListener listener5 = new ButtonListener();
ButtonListener listener6 = new ButtonListener();
ButtonListener listener7 = new ButtonListener();
ButtonListener listener8 = new ButtonListener();
ButtonListener listener9 = new ButtonListener();
ButtonListener listener10 = new ButtonListener();
ButtonListener listener11 = new ButtonListener();
ButtonListener listener12 = new ButtonListener();
ButtonListener listener13 = new ButtonListener();
b1 = new JButton("1");
b1.addActionListener(listener1);
b2 = new JButton("2");
b2.addActionListener(listener2);
b3 = new JButton("3");
b3.addActionListener(listener3);
b4 = new JButton("4");
b4.addActionListener(listener4);
b5 = new JButton("5");
b5.addActionListener(listener5);
b6 = new JButton("6");
b6.addActionListener(listener6);
b7 = new JButton("7");
b7.addActionListener(listener7);
b8 = new JButton("8");
b8.addActionListener(listener8);
b9 = new JButton("9");
b9.addActionListener(listener9);
b10 = new JButton("*");
b10.addActionListener(listener10);
b11 = new JButton("0");
b11.addActionListener(listener11);
b12 = new JButton("#");
b12.addActionListener(listener12);
clear = new JButton("Clear");
clear.addActionListener(listener13);
panel2.add(b1);
panel2.add(b2);
panel2.add(b3);
panel2.add(b4);
panel2.add(b5);
panel2.add(b6);
panel2.add(b7);
panel2.add(b8);
panel2.add(b9);
panel2.add(b10);
panel2.add(b11);
panel2.add(b12);
panel4.add(clear);
//set up for main panel
primary.setLayout(new BorderLayout());
primary.setBackground(Color.gray);
primary.add(panel2, BorderLayout.CENTER);
primary.add(panel3, BorderLayout.NORTH);
primary.add(panel4, BorderLayout.SOUTH);
add(primary);
}
// this is the listener
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (event.getSource() == b1) {
label1.setText("1");
}
}
}
}
Store the text that you want to print on a label in a separate instance variable (String). Every time a button is pressed, append the number to this text and set it on the label.
Simply append the result to what the label already contains
label.setText(label.getText() + "1");