Hello there. I have done a thorough search on google how to get the selectedValue in the JList and use the value to calculate the price of the menu, as shown below in the figure. However, I can't figure out a way to implement the button Calculate feature. Please see the image below for the description of the problem.
import javax.swing.*;
import java.awt.Font;
import java.awt.event.*;
public class JResto extends JFrame implements ActionListener {
private JTextField t1;
private JButton b1;
private JList list;
private String[] values = {"Fried Rice (Rs 150)", "Fried Noodle (Rs 125)", "Chop Soy (Rs 75)", "Wang Tang (Rs 35)"};
public JResto(){
super("Sumen Restaurant");
setBounds(100, 100, 450, 440);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
JLabel lblSumenRestaurant = new JLabel("Sumen Restaurant");
lblSumenRestaurant.setFont(new Font("Tahoma", Font.BOLD, 12));
lblSumenRestaurant.setBounds(156, 11, 127, 29);
add(lblSumenRestaurant);
list = new JList(values);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setBounds(115, 52, 200, 129);
add(list);
JLabel lblQuantity = new JLabel("Quantity");
lblQuantity.setBounds(115, 225, 46, 14);
add(lblQuantity);
t1 = new JTextField();
t1.setBounds(171, 222, 86, 20);
add(t1);
t1.setColumns(10);
JButton b1 = new JButton("Calculate");
b1.setBounds(171, 304, 89, 23);
add(b1);
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
}
}
The code now works !
managed to do it with list.isSelectedIndex()
you can use jlist item with the methode add
list = new JList(values); // not used eny more and will dispare soon
Related
When I try to add some function to get data from jtext field under public void actionPerformed(ActionEvent arg0) My GUI goes blank I have attached the images below I can't figure out what's wrong with it.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Register extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
private JLabel lblNewLabel;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Register frame = new Register();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Register() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblName = new JLabel("Name");
lblName.setBounds(56, 56, 56, 16);
contentPane.add(lblName);
JLabel lblUsername = new JLabel("Username");
lblUsername.setBounds(56, 85, 77, 16);
contentPane.add(lblUsername);
JLabel lblPassword = new JLabel("Password");
lblPassword.setBounds(56, 114, 56, 16);
contentPane.add(lblPassword);
JLabel lblAge = new JLabel("Age");
lblAge.setBounds(56, 143, 56, 16);
contentPane.add(lblAge);
JLabel lblGender = new JLabel("Gender");
lblGender.setBounds(56, 172, 56, 16);
contentPane.add(lblGender);
textField = new JTextField();
textField.setBounds(130, 53, 116, 19);
contentPane.add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setBounds(130, 82, 116, 22);
contentPane.add(textField_1);
textField_1.setColumns(10);
textField_2 = new JTextField();
textField_2.setBounds(130, 111, 116, 22);
contentPane.add(textField_2);
textField_2.setColumns(10);
textField_3 = new JTextField();
textField_3.setBounds(130, 140, 116, 22);
contentPane.add(textField_3);
textField_3.setColumns(10);
textField_4 = new JTextField();
textField_4.setBounds(130, 169, 116, 22);
contentPane.add(textField_4);
textField_4.setColumns(10);
JButton btnSubmit = new JButton("Submit");
btnSubmit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
btnSubmit.setBounds(169, 215, 97, 25);
contentPane.add(btnSubmit);
lblNewLabel = new JLabel("New label");
lblNewLabel.setBounds(330, 56, 56, 16);
contentPane.add(lblNewLabel);
}
}
EDIT 1: Added String name to action just to get name.
JButton btnSubmit = new JButton("Submit");
btnSubmit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String Name;
}
});
and This happened
GUI went blank:
You're using an absolute layout. Since you can't pack() the window, you need to at least validate() its contents.
Register frame = new Register();
frame.validate();
frame.setVisible(true);
Now you can see the real problem: you picked bounds for lblPassword and lblNewLabel that cut off the text on my computer. You really need to use a layout, maybe like this.
I have made a simple digital business card in Java swing using Eclipse, I am brand new to Java and programming in general so please bear with me. Basically what I want to do is add a button that says "Our Work" or "Portfolio" for example, which when clicked will open a new window where I can then add pictures or links or false reviews etc.
I know this is probably quite simple, but I struggle to understand a lot of tutorials when it is based on other peoples code.
package dbuscard;
import java.awt.EventQueue;
import javax.swing.JFrame;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.Font;
import javax.swing.JSeparator;
public class card {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
card window = new card();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public card() {
initialize();
}
/**
* Initialise the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.getContentPane().setBackground(new Color(153, 204, 255));
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblLogo = new JLabel("");
lblLogo.setBounds(127, 11, 219, 104);
frame.getContentPane().add(lblLogo);
lblLogo.setIcon(new ImageIcon("Images\\zlogoimg.png"));
JLabel lblNewLabel = new JLabel("t: 01254 777494");
lblNewLabel.setFont(new Font("Source Code Pro Light", Font.PLAIN, 11));
lblNewLabel.setBounds(20, 187, 126, 14);
frame.getContentPane().add(lblNewLabel);
JLabel lblEApexuxdesigngmailcom = new JLabel("e: apexuxdesign#gmail.com");
lblEApexuxdesigngmailcom.setFont(new Font("Source Code Pro Light", Font.PLAIN, 11));
lblEApexuxdesigngmailcom.setBounds(20, 204, 200, 14);
frame.getContentPane().add(lblEApexuxdesigngmailcom);
JLabel lblVisitWebsite = new JLabel("visit website");
lblVisitWebsite.setFont(new Font("Source Code Pro Light", Font.BOLD, 11));
lblVisitWebsite.setBounds(10, 237, 117, 14);
frame.getContentPane().add(lblVisitWebsite);
JLabel facebook = new JLabel("");
facebook.setBounds(282, 204, 64, 47);
frame.getContentPane().add(facebook);
facebook.setIcon(new ImageIcon("Images\\facebook.png"));
JLabel twitter = new JLabel("");
twitter.setBounds(320, 204, 72, 47);
frame.getContentPane().add(twitter);
twitter.setIcon(new ImageIcon("Images\\twitter.png"));
JLabel youtube = new JLabel("");
youtube.setBounds(356, 204, 68, 47);
frame.getContentPane().add(youtube);
youtube.setIcon(new ImageIcon("Images\\youtube.png"));
JLabel lblSeanHutchinson = new JLabel("Sean Hutchinson");
lblSeanHutchinson.setFont(new Font("Source Code Pro Light", Font.PLAIN, 11));
lblSeanHutchinson.setBounds(20, 128, 126, 14);
frame.getContentPane().add(lblSeanHutchinson);
JLabel lblUxDesigner = new JLabel("UX Designer");
lblUxDesigner.setFont(new Font("Source Code Pro Light", Font.PLAIN, 11));
lblUxDesigner.setBounds(20, 145, 107, 14);
frame.getContentPane().add(lblUxDesigner);
JLabel lblNewLabel_1 = new JLabel("CEO - Apex UX Design");
lblNewLabel_1.setFont(new Font("Source Code Pro Light", Font.PLAIN, 11));
lblNewLabel_1.setBounds(20, 162, 158, 14);
frame.getContentPane().add(lblNewLabel_1);
}
}
Simple. Add an action listener which uses the method setVisible(true); on a seperate JFrame. Example code:
package com.nonsense;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class del {
private JFrame frame, frame2;
/**
* Launch the application.
*/
public static void main(String[] args) {
del window = new del();
window.frame.setVisible(true);
}
/**
* Create the application.
*/
public del() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame2 = new JFrame();
frame2.setBounds(100, 100, 450, 300);
frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame2.getContentPane().setLayout(null);
JButton btnOpenWindow = new JButton("Open Window");
btnOpenWindow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
frame2.setVisible(true);
}
});
btnOpenWindow.setBounds(167, 118, 120, 23);
frame.getContentPane().add(btnOpenWindow);
}
}
I use "DISPOSE_ON_CLOSE" so that the second window does not terminate the program when the X button is pressed.
This is the code. My main frame is orbital_app and I would like it to when I click on JButton (button), data is being saved, the current window closes and another window orbital_app opens.
public class signup_try {
private JFrame frame;
private JTextField txtname;
private JTextField textusername;
private JTextField txtpass;
private JTextField textmail;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
signup_try window = new signup_try();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public signup_try() {
initialize();
}
Connection connection=null;
/**
* Initialize the contents of the frame.
*/
private void initialize() {
connection=dbase.dBase();
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel label = new JLabel("Orbital");
label.setForeground(SystemColor.activeCaption);
label.setFont(new Font("Arial Black", Font.BOLD, 17));
label.setBounds(170, 11, 71, 25);
frame.getContentPane().add(label);
JLabel label_1 = new JLabel("Name:");
label_1.setForeground(SystemColor.activeCaption);
label_1.setFont(new Font("Arial Black", Font.PLAIN, 12));
label_1.setBounds(21, 66, 46, 14);
frame.getContentPane().add(label_1);
txtname = new JTextField();
txtname.setColumns(10);
txtname.setBounds(102, 63, 200, 22);
frame.getContentPane().add(txtname);
textusername = new JTextField();
textusername.setColumns(10);
textusername.setBounds(102, 108, 200, 22);
frame.getContentPane().add(textusername);
txtpass = new JTextField();
txtpass.setColumns(10);
txtpass.setBounds(102, 150, 200, 22);
frame.getContentPane().add(txtpass);
textmail = new JTextField();
textmail.setColumns(10);
textmail.setBounds(102, 192, 200, 22);
frame.getContentPane().add(textmail);
JLabel label_2 = new JLabel("Username:");
label_2.setForeground(SystemColor.activeCaption);
label_2.setFont(new Font("Arial Black", Font.PLAIN, 12));
label_2.setBounds(21, 111, 71, 14);
frame.getContentPane().add(label_2);
JLabel label_3 = new JLabel("Password:");
label_3.setForeground(SystemColor.activeCaption);
label_3.setFont(new Font("Arial Black", Font.PLAIN, 12));
label_3.setBounds(21, 154, 71, 14);
frame.getContentPane().add(label_3);
JLabel label_4 = new JLabel("Email:");
label_4.setForeground(SystemColor.activeCaption);
label_4.setFont(new Font("Arial Black", Font.PLAIN, 12));
label_4.setBounds(21, 196, 46, 14);
frame.getContentPane().add(label_4);
JButton button = new JButton("Sign Up");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
String query="insert into Users(Name, Username, Password, Email) values(?,?,?,?)";
PreparedStatement prepstat=connection.prepareStatement(query);
prepstat.setString(1, txtname.getText());
prepstat.setString(2, textusername.getText());
prepstat.setString(3, txtpass.getText());
prepstat.setString(4, textmail.getText());
prepstat.execute();
JOptionPane.showMessageDialog(null, "Data saved");
prepstat.close();
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, e);
}
I get the error saying that The method setVisible(boolean) is undefined for the type orbital_app. what should I do to fix this? Here I want to close this present frame(signup_try) and go to other frame (orbital_app).. setVisible is underlined red and says "The method setVisible(boolean) is undefined for the type orbital_app".
frame.dispose();
orbital_app orb=new orbital_app();
orb.setVisible(true);
}
});
button.setBounds(170, 239, 91, 23);
frame.getContentPane().add(button);
}
}
Orbital_app code(in this code setVisible works properly without any error):
package project;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Font;
import java.awt.SystemColor;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Color;
import javax.swing.UIManager;
public class orbital_app{
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
orbital_app window = new orbital_app();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public orbital_app() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.getContentPane().setBackground(UIManager.getColor("Button.background"));
frame.setResizable(false);
frame.setBounds(100, 100, 450, 260);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel toplbl = new JLabel("Orbital");
toplbl.setForeground(SystemColor.activeCaption);
toplbl.setFont(new Font("Arial Black", Font.BOLD, 17));
toplbl.setVerticalAlignment(SwingConstants.TOP);
toplbl.setBounds(182, 11, 71, 25);
frame.getContentPane().add(toplbl);
JLabel infolbl = new JLabel("Multipurpose app == orbital 1.0\r\n");
infolbl.setFont(new Font("Arial", Font.PLAIN, 11));
infolbl.setForeground(SystemColor.activeCaption);
infolbl.setBounds(138, 47, 165, 25);
frame.getContentPane().add(infolbl);
JButton signup_btn = new JButton("Sign Up");
signup_btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
frame.dispose();
signup_form sup_for=new signup_form();
3.setVisible works here
sup_for.setVisible(true);
}
});
signup_btn.setFont(new Font("Arial", Font.PLAIN, 11));
signup_btn.setForeground(SystemColor.activeCaption);
signup_btn.setBounds(61, 133, 91, 23);
frame.getContentPane().add(signup_btn);
JButton signin_btn = new JButton("Sign In");
signin_btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
frame.dispose();
signin_form log_for=new signin_form();
4.and here too
log_for.setVisible(true);
}
});
signin_btn.setFont(new Font("Arial", Font.PLAIN, 11));
signin_btn.setForeground(SystemColor.activeCaption);
signin_btn.setBounds(285, 133, 91, 23);
frame.getContentPane().add(signin_btn);
JLabel notelbl = new JLabel("note: click Sign Up for new account or Sign In for existing account.");
notelbl.setHorizontalAlignment(SwingConstants.CENTER);
notelbl.setBounds(10, 199, 406, 25);
frame.getContentPane().add(notelbl);
}
}
I faced the same problem, it is solved by exteding the JFrame class.
Your class should extends JFrame, because setVisible() method belongs to JFrameclass.
There seems to be a few issues with your code.
Most importantly: You have multiple entry points in your program.
You should only have one public static void main(String[] args) ... function in your application, that is where the 'program starts'.
The error message you get (The method setVisible(boolean) is undefined for the type orbital_app) comes from the fact that the class orbital_app donĀ“t have a setVisible function, one of its members does, but that doesn't matter.
Your orbital_app have a private member that is a JFrame, which makes it possible for you to call the JFrames methods from inside the orbital_app by accessing the frame, but you cant reach it from outside.
It seems you have mixed up inheritance and ownership.
If you wish your orbital_app class to be a JFrame, you need to inherit from the JFrame. Else you could just implement the methods you wish to make public for your other classes.
Or you could just create a getter for the private JFrame object so that you can access it from outside.
I'm trying to make my JPanel scrollable but it's not working. I made a JPanel, added components to it, then added my JPanel to a JScrollPane. This is what you're supposed to do, right? What am I doing wrong?
import java.awt.Dimension;
import java.awt.Font;
import java.awt.SystemColor;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ScrollPaneLayout;
import javax.swing.SwingConstants;
public class RegisterPane extends JPanel {
private JTextField txtJohnDoe;
private JTextField txtExampledomaincom;
private JPasswordField passwordField;
private JPasswordField passwordField_1;
/**
* Create the panel.
*/
public RegisterPane() {
setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(6, 36, 300, 450);
panel.setLayout(null);
JLabel lblFirstName = new JLabel("First Name");
lblFirstName.setBounds(28, 6, 92, 16);
panel.add(lblFirstName);
txtJohnDoe = new JTextField();
txtJohnDoe.setText("John Doe");
txtJohnDoe.setBounds(124, 0, 251, 28);
panel.add(txtJohnDoe);
txtJohnDoe.setColumns(10);
txtExampledomaincom = new JTextField();
txtExampledomaincom.setText("Example#domain.com");
txtExampledomaincom.setColumns(10);
txtExampledomaincom.setBounds(124, 40, 251, 28);
panel.add(txtExampledomaincom);
JLabel lblEmail = new JLabel("Email");
lblEmail.setBounds(28, 46, 92, 16);
panel.add(lblEmail);
JLabel lblGender = new JLabel("Gender");
lblGender.setBounds(28, 89, 55, 16);
panel.add(lblGender);
JRadioButton rdbtnMale = new JRadioButton("Male");
rdbtnMale.setBounds(124, 85, 92, 23);
panel.add(rdbtnMale);
JRadioButton rdbtnFemale = new JRadioButton("Female");
rdbtnFemale.setBounds(283, 85, 92, 23);
panel.add(rdbtnFemale);
JLabel lblPassword = new JLabel("Password");
lblPassword.setBounds(28, 157, 55, 16);
panel.add(lblPassword);
JLabel passDirections = new JLabel();
passDirections.setHorizontalAlignment(SwingConstants.CENTER);
passDirections.setFont(new Font("Lucida Grande", Font.PLAIN, 10));
passDirections.setBackground(SystemColor.window);
passDirections.setText("Password's Must be at Least 6 characters long and contain 1 non letter character");
passDirections.setBounds(29, 117, 346, 28);
panel.add(passDirections);
passwordField = new JPasswordField();
passwordField.setBounds(124, 151, 251, 28);
panel.add(passwordField);
passwordField_1 = new JPasswordField();
passwordField_1.setBounds(124, 195, 251, 28);
panel.add(passwordField_1);
JLabel lblRetypePassword = new JLabel("Password Again");
lblRetypePassword.setBounds(28, 201, 92, 16);
panel.add(lblRetypePassword);
JCheckBox chckbxSubscribeToNews = new JCheckBox("Subscribe to News Letter");
chckbxSubscribeToNews.setSelected(true);
chckbxSubscribeToNews.setHorizontalAlignment(SwingConstants.CENTER);
chckbxSubscribeToNews.setBounds(29, 244, 346, 23);
panel.add(chckbxSubscribeToNews);
JLabel lblNewLabel = new JLabel("New label");
lblNewLabel.setBounds(28, 280, 55, 16);
panel.add(lblNewLabel);
JScrollPane scroll = new JScrollPane(panel);
scroll.setSize(new Dimension(450,300));
panel.setAutoscrolls(true);
add(scroll);
}
}
Here's my JFrame class
import java.awt.BorderLayout;
public class MainFrame extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainFrame frame = new MainFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public MainFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setLayout(new BorderLayout(0, 0));
setResizable(false);
setContentPane(new RegisterPane());
//RegisterPane isn't scrolling ^
}
}
Thanks in Advance for your help!
I'm seeing setLayout(null) alot in your code.
The JViewport uses the component's/view's preferred size as a bases for determining if the view expands beyond the visible bounds of the JScrollPane, because you've seen fit to ignore this feature, the components have begun to break down.
Swing is designed to work layout managers and it makes it much easier to develop complex user interfaces that can work across a multitude of platforms and environments
Is there a method that will add a string to JLabel without editing what was already there? I know about the setText method but in my program, a calculator, I want to add the button clicked to the JLabel with out overriding what is already there. Im I doing this right? Here's my code (if you need it):
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.border.EmptyBorder;
public class calc extends JFrame implements ActionListener {
private JPanel contentPane;
public JLabel results;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
calc frame = new calc();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public calc() {
setTitle("Calculator");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 255, 179);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
JPanel panel = new JPanel();
panel.setToolTipText("Numbers will appear here once clicked!");
panel.setBackground(Color.WHITE);
contentPane.add(panel, BorderLayout.NORTH);
results = new JLabel("");
panel.add(results);
JPanel panel_1 = new JPanel();
contentPane.add(panel_1, BorderLayout.CENTER);
panel_1.setLayout(null);
JButton one = new JButton("1");
one.addActionListener(this);
one.setBounds(6, 19, 61, 29);
panel_1.add(one);
JButton two = new JButton("2");
two.addActionListener(this);
two.setBounds(67, 19, 61, 29);
panel_1.add(two);
JButton three = new JButton("3");
three.addActionListener(this);
three.setBounds(127, 19, 61, 29);
panel_1.add(three);
JButton four = new JButton("4");
four.addActionListener(this);
four.setBounds(6, 48, 61, 29);
panel_1.add(four);
JButton five = new JButton("5");
five.addActionListener(this);
five.setBounds(67, 48, 61, 29);
panel_1.add(five);
JButton six = new JButton("6");
six.addActionListener(this);
six.setBounds(127, 48, 61, 29);
panel_1.add(six);
JButton seven = new JButton("7");
seven.addActionListener(this);
seven.setBounds(6, 75, 61, 29);
panel_1.add(seven);
JButton eight = new JButton("8");
eight.addActionListener(this);
eight.setBounds(67, 75, 61, 29);
panel_1.add(eight);
JButton nine = new JButton("9");
nine.addActionListener(this);
nine.setBounds(127, 75, 61, 29);
panel_1.add(nine);
JButton zero = new JButton("0");
zero.addActionListener(this);
zero.setBounds(6, 102, 122, 29);
panel_1.add(zero);
JButton decimal = new JButton(".");
decimal.addActionListener(this);
decimal.setBounds(127, 102, 61, 29);
panel_1.add(decimal);
JButton add = new JButton("+");
add.addActionListener(this);
add.setBounds(184, 19, 61, 29);
panel_1.add(add);
JButton sub = new JButton("-");
sub.addActionListener(this);
sub.setBounds(184, 48, 61, 29);
panel_1.add(sub);
JButton times = new JButton("x");
times.addActionListener(this);
times.setBounds(184, 75, 61, 29);
panel_1.add(times);
JButton div = new JButton("\u00F7");
div.addActionListener(this);
div.setBounds(184, 102, 61, 29);
panel_1.add(div);
JSeparator separator = new JSeparator();
separator.setBounds(6, 6, 239, 12);
panel_1.add(separator);
}
public void actionPerformed(ActionEvent al) {
String name = al.getActionCommand();
}
}
Just get the orginal text, add "blah" (or anything else), and set it back.
results.setText(results.getText() + "blah");
Don't use a JLabel which is really designed to just display text. Instead you can use a non-editable JTextField which is designed to be updated. Then you can just use:
String name = al.getActionCommand();
textField.replaceSelection(name);
and the character will be appended to the end of the text field.
Also, don't use a null layout and the setBounds() method. Swing was designed to be used with layout managers.