how to print multiple numbers to a label with keypad buttons - java

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");

Related

Java GUI How to make an event when I press a JButton?

I want to make a keyboard. If I press the button it has to come up at 2.(pic). I think it's a similar way to make a calculator. Can I have some advice?
Actually I don't even know if this right. Am I OK making a JButton like that?
This is my code.
package assignment;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Random extends JFrame {
Random() {
setTitle("보안 키보드");
setLayout(new BorderLayout(10, 10));
showNorth();
showCenter();
showSouth();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(450,500);// 창크기를 정한다
setVisible(true);}
void showNorth() {
JTextField area1 = new JTextField();
JTextField area2 = new JTextField();
JPanel panel = new JPanel(new GridLayout(2, 0));
}
area2.setText("보안문자를 입력하세요.");
area1.setHorizontalAlignment(JTextField.CENTER);
area2.setHorizontalAlignment(JTextField.CENTER);
area1.setEditable(false);
area2.setEditable(false);
panel.add(area1);
panel.add(area2);
add(panel, BorderLayout.NORTH);
}
void showCenter() {
JPanel p3 = new JPanel(new GridLayout(4, 4));
p3.setLayout(new GridLayout(5, 5, 5, 5));
// 버튼 생성하기
JButton ba = new JButton("");
JButton bb = new JButton("");
JButton bc = new JButton("");
JButton bd = new JButton("");
JButton b0 = new JButton("0");
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 er1 = new JButton("하나\n지움");
JButton erall = new JButton("전체\n지움");
p3.add(ba);// 버튼을 패널에 부착시킨다
p3.add(bb);
p3.add(bc);
p3.add(bd);
p3.add(b0);
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(er1);
p3.add(erall);
add(p3, BorderLayout.CENTER); // 패널을 프레임의 중앙에 추가한다.
}
void showSouth() {
JPanel p4 = new JPanel();
JButton complete = new JButton("입력완료"); // 입력완료 버튼 생성
p4.add(complete);
p4.setLayout((LayoutManager) new FlowLayout(FlowLayout.TRAILING));
add(p4, BorderLayout.SOUTH);
}
public static void main(String[] args) {
new Random();
}
}
First of all, the code needs extreme refactor. But as you the OOP beginner, lets dive into your problem. Your class called Random needs to implement ActionListener interface and override actionPerformed method:
#Override
public void actionPerformed(ActionEvent e) {
}
Then, what you want to do is to add action listeners and set action commands for the buttons, for instance:
button.addActionListener(this);
button.addActionCommand("button")
Now you can know which button was clicked and represent something on your JTextField:
#Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("button")){
textField.setText("button was clicked");
}
}

How to separate a JTextField from the rest of the GridLayout

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);
}
}

Adding components to a JFrame

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);
}
});

java code not showing results without resizing frame

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

Splitting panes for screen and battons

I have this code I have been working on trying to get these panels split. I want to get one panel for the screen with the text area and another for the buttons. I need help, I got stuck somewhere. I want to make sure that i produce a phone-like interface
Thank you
Here is what I have done
import java.awt.*;
import javax.swing.*;
class Phone {
public static void main(String[] args) {
JFrame phone = new JFrame("My First Gui");
phone.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel jsp1 = new JPanel();
JPanel jsp2 = new JPanel();
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, true, jsp1, jsp2);
// splitPane.setOneTouchExpandable(true);
getContentPane().add(splitPane);
jsp1.add(new James());
jsp2.add(new Doris());
phone.getContentPane().add(jsp1);
phone.getContentPane().add(jsp2);
phone.pack();
phone.show();
}
}
class James extends JPanel {
public James() {
BorderLayout bb = new BorderLayout();
setLayout(bb);
JLabel txt1 = new JLabel("Phone ");
JTextArea tx1 = new JTextArea(300, 100);
add(tx1, bb.CENTER);
add(txt1, bb.NORTH);
// add(txt1);
// add(txt2);
// add(b1);
}
}
class Doris extends JPanel {
public Doris() {
GridLayout grd = new GridLayout(4, 3, 2, 2);
setLayout(grd);
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 bs = new JButton("*");
JButton b0 = new JButton("0");
JButton bt = new JButton("#");
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b6);
add(b7);
add(b8);
add(b9);
add(bs);
add(b0);
add(bt);
}
}
JPanel jsp1 = new JPanel();
JPanel jsp2 = new JPanel();
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, true, jsp1, jsp2);
// splitPane.setOneTouchExpandable(true);
getContentPane().add(splitPane);
jsp1.add(new James());
jsp2.add(new Doris());
//phone.getContentPane().add(jsp1);
//phone.getContentPane().add(jsp2);
First you add the jsp1 and jsp2 to the split pane which is ok. Then you add the split pane to the content pane which is also ok.
2 But then you add jsp1 and jsp2 to the content pane which is NOT ok. A component can only have a single parent. If you want the panels to be in the split pane then just add them to the split pane.
//phone.show();
phone.setVisible(true);
Don't use the show() method it is deprecated. Instead you should use the setVisible() method.
Read the section from the Swing tutorial on How to Use Split Panes for more information and working examples.

Categories