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
Related
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");
}
}
I have been trying to make a calculator in Javax swing. I know how to make one button have one command, but I don't know how to give more buttons more commands in Java? I want to give buttons a0-clear actions.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Calculator extends JFrame {
private JButton a0;
private JButton a1;
private JButton a2;
private JButton a3;
private JButton a4;
private JButton a5;
private JButton a6;
private JButton a7;
private JButton a8;
private JButton a9;
private JButton clear;
private JButton plusminus;
private JButton plus;
private JButton minus;
private JButton multiply;
private JButton divide;
private JButton equal;
private JButton decimal;
private JLabel resultLabel;
public Calculator() {
setLayout(new FlowLayout());
a0 = new JButton("0");
add(a0);
a1 = new JButton("1");
add(a1);
a2 = new JButton("2");
add(a2);
a3 = new JButton("3");
add(a3);
a4 = new JButton("4");
add(a4);
a5 = new JButton("5");
add(a5);
a6 = new JButton("6");
add(a6);
a7 = new JButton("7");
add(a7);
a8 = new JButton("8");
add(a8);
a9 = new JButton("9");
add(a9);
decimal = new JButton(".");
add(decimal);
clear = new JButton("C");
add(clear);
plusminus = new JButton("+/-");
add(plusminus);
plus = new JButton("+");
add(plus);
minus = new JButton("-");
add(minus);
multiply = new JButton("X");
add(multiply);
divide = new JButton("/");
add(divide);
equal = new JButton("=");
add(equal);
resultLabel = new JLabel("");
add(resultLabel);
}
public class event implements ActionListener {
public void actionPerformed(ActionEvent event) {
a0.addActionListener(this);
a1.addActionListener(this);
a2.addActionListener(this);
a3.addActionListener(this);
a4.addActionListener(this);
a5.addActionListener(this);
a6.addActionListener(this);
a7.addActionListener(this);
a8.addActionListener(this);
a9.addActionListener(this);
clear.addActionListener(this);
decimal.addActionListener(this);
plusminus.addActionListener(this);
plus.addActionListener(this);
minus.addActionListener(this);
multiply.addActionListener(this);
divide.addActionListener(this);
equal.addActionListener(this);
if (event.getSource() == a1) {
resultLabel.setText("0");
}
}
}
public static void main(String[] args) {
Calculator gui = new Calculator();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setSize(300, 800);
gui.setVisible(true);
gui.setTitle("Caclculator");
}
}
I am going to show you for 1-button, do the same for all other,
public class Calculator extends JFrame implements ActionListener {
JButton a0 = new JButton("0");
a0.addActionListener(this);
add(a0);
public void actionPerformed(ActionEvent event) {
String command = e.getActionCommand();
switch(command){
case "0":
//do whatever want while button '0' press
break;
case "1":
//do whatever want while button '1' press
break;
case "2":
//do whatever want while button '2' press
break;
}
}
}
You will create a class which implements ActionListener.
Then you create and instance of this class and then add this as ActionListener to every button(Code Below):
#SuppressWarnings("serial")
public class Calculator extends JFrame {
// array with the number 0-9
private JButton[] numbers = new JButton[9];
private JButton clear;
private JButton plusminus;
private JButton plus;
private JButton minus;
private JButton multiply;
private JButton divide;
private JButton equal;
private JButton decimal;
private JLabel resultLabel = new JLabel("");
public Calculator() {
//
setSize(300, 300);
setLayout(new FlowLayout());
// The Class which implements the ActionListener
EventListener listener = new EventListener();
// Create each button from 0-9 here
for (int i = 0; i < numbers.length; i++) {
numbers[i] = new JButton(i + "");
numbers[i].addActionListener(listener);
add(numbers[i]);
}
// Create the other Buttons
decimal = new JButton(".");
add(decimal);
clear = new JButton("C");
add(clear);
plusminus = new JButton("+/-");
add(plusminus);
plus = new JButton("+");
add(plus);
minus = new JButton("-");
add(minus);
multiply = new JButton("X");
add(multiply);
divide = new JButton("/");
add(divide);
equal = new JButton("=");
add(equal);
clear.addActionListener(listener);
decimal.addActionListener(listener);
plusminus.addActionListener(listener);
plus.addActionListener(listener);
minus.addActionListener(listener);
multiply.addActionListener(listener);
divide.addActionListener(listener);
equal.addActionListener(listener);
add(resultLabel);
}
public static void main(String[] args) {
Calculator gui = new Calculator();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setSize(300, 800);
gui.setVisible(true);
gui.setTitle("Caclculator");
}
// TODO EventListener
class EventListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
Object v = event.getSource();
if (v == clear) {
// do something..
} else if (v == decimal) {
// do something...
} else if (v == plusminus) {
// do something...
}
// etc continue this.....
}
}
}
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.
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");