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);
}
});
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);
}
}
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?
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
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");
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.