how to assign an arraylist value to a button - java

I am making a vending machine in which there are 6 slots within the vending machine, each with its own soda. We are to have an arraylist of enums for the brands of the sodas. I have created the buttons for each of the canslots but do not know how to assign an arraylist value for one can slot to the appropriate button as well as to have once you click the button for the canslot to decrease by 1 until it is empty.
My code for the actual vending machine is here:
public class VendingFrame extends JFrame {
private ArrayList<CanSlot> CanSlots = new ArrayList<>();
public VendingFrame() {
CanSlots.add(new CanSlot(Brand.PEPSI));
CanSlots.add(new CanSlot(Brand.COKE));
CanSlots.add(new CanSlot(Brand.SUNKIST));
CanSlots.add(new CanSlot(Brand.DIETPEPSI));
CanSlots.add(new CanSlot(Brand.MTDEW));
CanSlots.add(new CanSlot(Brand.SPRITE));
JPanel panel = new JPanel();
JButton button = new JButton("Pepsi");
button.setPreferredSize(new Dimension(100, 80));
JButton button2 = new JButton("Coke");
button2.setPreferredSize(new Dimension(100, 80));
JButton button3 = new JButton("Diest Pepsi");
button3.setPreferredSize(new Dimension(100, 80));
JButton button4 = new JButton("Sunkist");
button4.setPreferredSize(new Dimension(100, 80));
JButton button5 = new JButton("Mountain Dew");
button5.setPreferredSize(new Dimension(100, 80));
JButton button6 = new JButton("Sprite");
button6.setPreferredSize(new Dimension(100, 80));
JPanel picpanel = new JPanel();
JPanel buttonPanel = new JPanel();
JLabel label = new JLabel();
setLayout(new BorderLayout());
buttonPanel.setLayout(new GridLayout(6, 1));
add(panel);
picpanel.add(label);
label.setIcon(new javax.swing.ImageIcon("C:\\Users\\iacol\\Desktop\\cans.jpg.jpg"));
buttonPanel.add(button);
button.addActionListener(new ClickListener());
buttonPanel.add(button2);
button2.addActionListener(new ClickListener2());
buttonPanel.add(button3);
button3.addActionListener(new ClickListener3());
buttonPanel.add(button4);
button4.addActionListener(new ClickListener4());
buttonPanel.add(button5);
button5.addActionListener(new ClickListener5());
buttonPanel.add(button6);
button6.addActionListener(new ClickListener6());
add(picpanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.EAST);
setSize(700, 700);
setTitle("Vending Machine");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
}

You can make, HashMap<Button, List<Product>>();
Using how to id the Button object and retrieve the button list on the value.

Related

null pointer error at textfield even though value is entered

I keep getting a null exception error and I cant seem to fix it. I know a null pointer error is usually raised when your trying to use a value that is null but I pass a value to the textfield so Im confused. Looking for feedback:
exact error code: (this is where I add the actionlistner to button bt28)
"AWT-EventQueue-0" java.lang.NullPointerException
at GUI$4.actionPerformed(GUI.java:231) ...etc"
P.s. this class is called in a main class which is why that's not here. I've imported awt, swing and util.
public class GUI{
CardLayout cl=new CardLayout();
private String cardNum, pin;
JLabel header1,header2,header3,header4;
JButton bt01= new JButton("");
//bt01.setBounds(100,150,100,40);
JButton bt02= new JButton("");
JButton bt03= new JButton("");
JButton bt04= new JButton("");
JButton bt05= new JButton("");
JButton bt06= new JButton("");
JButton bt07= new JButton("");
JButton bt08= new JButton("Continue");
JButton bt21= new JButton("");
JButton bt22= new JButton("");
JButton bt23= new JButton("");
JButton bt24= new JButton("");
JButton bt25= new JButton("");
JButton bt26= new JButton("");
JButton bt27= new JButton("");
JButton bt28= new JButton("Continue");
JButton bt31= new JButton("");
JButton bt32= new JButton("");
JButton bt33= new JButton("");
JButton bt34= new JButton("");
JButton bt35= new JButton("Balance Query");
JButton bt36= new JButton("Withdraw");
JButton bt37= new JButton("Deposit");
JButton bt38= new JButton("");
JButton bt41= new JButton("A");
JButton bt42= new JButton("B");
JButton bt43= new JButton("C");
JButton bt44= new JButton("D");
JButton bt45= new JButton("E");
JButton bt46= new JButton("");
JButton bt47= new JButton("Business Accounts");
JButton bt48= new JButton("Cancel");
JPanel panelCont=new JPanel();
JPanel GUI1= new JPanel(new BorderLayout());
JPanel GUI2= new JPanel(new BorderLayout());
JPanel GUI3= new JPanel(new BorderLayout());
JPanel GUI4= new JPanel(new BorderLayout());
public GUI(){
JFrame frame1 = new JFrame ("JLCB Automated Banking Machine v 1.0 ");
frame1.setPreferredSize (new Dimension(500, 200));
header1= new JLabel("JLCB Automated Banking Machine");
JPanel nPanel1= new JPanel();
nPanel1.add(header1);
GUI1.add(nPanel1,BorderLayout.NORTH);
JLabel cardnumLabel =new JLabel("Enter Card Number");
JTextField txt= new JTextField(15);
txt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent cl){
cardNum=txt.getText();
txt.setText(null);
}
});
JPanel cPanel1= new JPanel();
cPanel1.add(cardnumLabel);
cPanel1.add(txt);
GUI1.add(cPanel1, BorderLayout.CENTER);
JPanel wPanel1=new JPanel(new GridLayout(4,0));
wPanel1.add(bt01);
wPanel1.add(bt02);
wPanel1.add(bt03);
wPanel1.add(bt04);
JPanel ePanel1=new JPanel(new GridLayout(4,0));
ePanel1.add(bt05);
ePanel1.add(bt06);
ePanel1.add(bt07);
ePanel1.add(bt08);
GUI1.add(wPanel1,BorderLayout.WEST);
GUI1.add(ePanel1,BorderLayout.EAST);
//................................
// .
// GUI2 .
//................................
JFrame frame2 = new JFrame ("JLCB Automated Banking Machine v 1.0 ");
frame2.setPreferredSize (new Dimension(500, 200));
header2= new JLabel("JLCB Automated Banking Machine");
JPanel nPanel2= new JPanel();
nPanel2.add(header1);
GUI2.add(nPanel2,BorderLayout.NORTH);
JLabel pinLabel =new JLabel("Enter your pin");
JTextField txt2= new JTextField(15);
txt2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent cl){
pin=txt2.getText();
txt2.setText(null);
}
});
JPanel cPanel2= new JPanel();
cPanel2.add(pinLabel);
cPanel2.add(txt2);
GUI2.add(cPanel2, BorderLayout.CENTER);
JPanel wPanel2=new JPanel(new GridLayout(4,0));
wPanel2.add(bt21);
wPanel2.add(bt22);
wPanel2.add(bt23);
wPanel2.add(bt24);
JPanel ePanel2=new JPanel(new GridLayout(4,0));
ePanel2.add(bt25);
ePanel2.add(bt26);
ePanel2.add(bt27);
ePanel2.add(bt28);
GUI2.add(wPanel2,BorderLayout.WEST);
GUI2.add(ePanel2,BorderLayout.EAST);
//................................
// .
// GUI3 .
//................................
JFrame frame3 = new JFrame ("JLCB Automated Banking Machine v 1.0 ");
frame3.setPreferredSize (new Dimension(500, 200));
header3= new JLabel("JLCB Automated Banking Machine");
JPanel nPanel3= new JPanel();
nPanel3.add(header1);
GUI3.add(nPanel3,BorderLayout.NORTH);
JLabel txt3 =new JLabel("Please select from the buttons on the right");
JPanel cPanel3= new JPanel();
cPanel3.add(txt3);
GUI3.add(cPanel3, BorderLayout.CENTER);
JPanel wPanel3=new JPanel(new GridLayout(4,0));
wPanel3.add(bt31);
wPanel3.add(bt32);
wPanel3.add(bt33);
wPanel3.add(bt34);
JPanel ePanel3=new JPanel(new GridLayout(4,0));
ePanel3.add(bt35);
ePanel3.add(bt36);
ePanel3.add(bt37);
ePanel3.add(bt38);
GUI3.add(wPanel3,BorderLayout.WEST);
GUI3.add(ePanel3,BorderLayout.EAST);
//..................................
// .
// GUI4 .
//..................................
JFrame frame4 = new JFrame ("JLCB Automated Banking Machine v 1.0 ");
frame4.setPreferredSize (new Dimension(500, 200));
header4= new JLabel("JLCB Automated Banking Machine");
JPanel nPanel4= new JPanel();
nPanel4.add(header4);
GUI4.add(nPanel4,BorderLayout.NORTH);
JPanel cPanel4= new JPanel();
GUI4.add(cPanel4, BorderLayout.CENTER);
JPanel wPanel4=new JPanel(new GridLayout(4,0));
wPanel4.add(bt41);
wPanel4.add(bt42);
wPanel4.add(bt43);
wPanel4.add(bt44);
JPanel ePanel4=new JPanel(new GridLayout(4,0));
ePanel4.add(bt45);
ePanel4.add(bt46);
ePanel4.add(bt47);
ePanel4.add(bt48);
GUI4.add(wPanel4,BorderLayout.WEST);
GUI4.add(ePanel4,BorderLayout.EAST);
//---------------------------------------------------
panelCont.setLayout(cl);
panelCont.add(GUI1, "1");
panelCont.add(GUI2, "2");
panelCont.add(GUI3, "3");
cl.show(panelCont,"1");
String a ="100001 1111 15984789.74";
bt08.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
cl.show(panelCont,"2");
}
});
bt28.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent r){
if (cardNum.equals(a.substring(0, a.indexOf(" "))) && pin.equals(a.substring(a.indexOf(" ")+1,a.lastIndexOf(" ")))){
cl.show(panelCont,"3");
String acc="a";
}
else{
JOptionPane.showMessageDialog(null,"Invalid accoun number or pin");
cl.show(panelCont,"3");
}
}
});
bt35.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
cl.show(panelCont,"3");
}
});
bt36.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
cl.show(panelCont,"3");
}
});
bt37.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
cl.show(panelCont,"3");
}
});
frame1.add(panelCont);
//(JFrame.DO_NOTHING_ON_CLOSE)
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.pack();
frame1.setVisible(true);
}
}
The issue is that your values for cardNum and pin are never assigned in the actionListeners for txt and txt2. You should pull the values in the event that the user clicks the continue button. See my code below.
bt28.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent r) {
cardNum = txt.getText();
pin = txt2.getText();
if (cardNum.equals(a.substring(0, a.indexOf(" "))) && pin.equals(a.substring(a.indexOf(" ") + 1, a.lastIndexOf(" ")))) {
cl.show(panelCont, "3");
String acc = "a";
} else {
JOptionPane.showMessageDialog(null, "Invalid accoun number or pin");
cl.show(panelCont, "3");
}
}
});

How is it possible to add a container for border layout?

I want to add labels and buttons above and below the border layout. How can I do that? Here is what I did:
import java.awt.*;
import javax.swing.*;
class homework{
public static void main(String[] args) {
JFrame frame= new JFrame("border layout");
frame.setVisible(true);
JLabel label=new JLabel("Container of BorderLayout");
JButton button1 = new JButton("NORTH");
JButton button2 = new JButton("SOUTH");
JButton button3 = new JButton("EAST");
JButton button5 = new JButton("CENTER");
JButton button4 = new JButton("WEST");
JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
frame.add(panel2);
label.setLayout(new FlowLayout(0));
panel2.add(label);
panel1.setLayout(new BorderLayout());
panel1.add(button1,BorderLayout.NORTH);
panel1.add(button2,BorderLayout.SOUTH);
panel1.add(button3,BorderLayout.EAST);
panel1.add(button4,BorderLayout.WEST);
panel1.add(button5,BorderLayout.CENTER);
frame.add(panel1);
frame.pack();
}
}
Above and below of border layout, set new 2 containers (for example JPanel) and make them flow layout. enter image description here
JPanel panel = new JPanel();
frame.getContentPane().add(panel, BorderLayout.NORTH);
JRadioButton rdbtnNewRadioButton = new JRadioButton("New radio button");
panel.add(rdbtnNewRadioButton);
JRadioButton rdbtnNewRadioButton_1 = new JRadioButton("New radio button");
panel.add(rdbtnNewRadioButton_1);
JPanel panel_1 = new JPanel();
frame.getContentPane().add(panel_1, BorderLayout.SOUTH);
JLabel lblNewLabel = new JLabel("New label");
panel_1.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("New label");
panel_1.add(lblNewLabel_1);
add something like that before frame.pack(); code.
Actually what do you mean by above and below? Do you mean north and south? If It is you should have something like this enter image description here
and you should write code this way
import java.awt.*;
class homework{
public static void main(String[] args) {
JFrame frame= new JFrame("border layout");
frame.setVisible(true);
JLabel label=new JLabel("Container of BorderLayout");
JButton button3 = new JButton("EAST");
JButton button5 = new JButton("CENTER");
JButton button4 = new JButton("WEST");
JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
frame.getContentPane().add(panel2);
label.setLayout(new FlowLayout(0));
panel2.add(label);
panel1.setLayout(new BorderLayout());
panel1.add(button3,BorderLayout.EAST);
panel1.add(button4,BorderLayout.WEST);
panel1.add(button5,BorderLayout.CENTER);
frame.getContentPane().add(panel1);
JPanel panel = new JPanel();
panel1.add(panel, BorderLayout.NORTH);
JLabel lblNewLabel = new JLabel("New label");
panel.add(lblNewLabel);
JRadioButton rdbtnNewRadioButton = new JRadioButton("New radio button");
panel.add(rdbtnNewRadioButton);
JPanel panel_1 = new JPanel();
panel1.add(panel_1, BorderLayout.SOUTH);
JLabel lblNewLabel_1 = new JLabel("New label");
panel_1.add(lblNewLabel_1);
JRadioButton rdbtnNewRadioButton_1 = new JRadioButton("New radio button");
panel_1.add(rdbtnNewRadioButton_1);
frame.pack();
}
}
In two conditions, you should add two containers into your code and make them flow layout.

Trouble with scroll pane with nested layouts

I am having trouble adding a scroll pane to a nested panel. Here is what I have:
public class board {
public static void addComponentsToPane(Container pane) {
pane.setLayout(new GridLayout(1, 0));
JPanel left = new JPanel();
pane.add(left);
left.setLayout(new BoxLayout(left, BoxLayout.Y_AXIS));
JPanel leftTop = new JPanel();
leftTop.setPreferredSize(new Dimension(266, 300));
leftTop.setBackground(Color.black);
left.add(leftTop);
JScrollPane scrollPane = new JScrollPane(leftTop); //problem is here
left.add(scrollPane);
JButton jb = new JButton();
jb.setPreferredSize(new Dimension(250,50));
leftTop.add(jb);
JButton jb1 = new JButton();
jb1.setPreferredSize(new Dimension(250,50));
leftTop.add(jb1);
JButton jb2 = new JButton();
jb2.setPreferredSize(new Dimension(250,50));
leftTop.add(jb2);
JButton jb3 = new JButton();
jb3.setPreferredSize(new Dimension(250,50));
leftTop.add(jb3);
JButton jb4 = new JButton();
jb4.setPreferredSize(new Dimension(250,50));
leftTop.add(jb4);
JButton jb5 = new JButton();
jb5.setPreferredSize(new Dimension(250,50));
leftTop.add(jb5);
JButton jb6 = new JButton();
jb6.setPreferredSize(new Dimension(250,50));
leftTop.add(jb6);
JPanel leftBottom = new JPanel();
leftBottom.setPreferredSize(new Dimension(266, 300));
leftBottom.setBackground(Color.red);
left.add(leftBottom);
JPanel middle = new JPanel();
pane.add(middle);
middle.setLayout(new BoxLayout(middle, BoxLayout.Y_AXIS));
JPanel middleTop = new JPanel();
middleTop.setPreferredSize(new Dimension(266, 200));
middleTop.setBackground(Color.green);
middle.add(middleTop);
JPanel middleBottom = new JPanel();
middleBottom.setPreferredSize(new Dimension(266, 400));
middleBottom.setBackground(Color.yellow);
middle.add(middleBottom);
JPanel right = new JPanel();
right.setPreferredSize(new Dimension(266, 600));
right.setBackground(Color.blue);
pane.add(right);
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("GridBagLayoutDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
createAndShowGUI();
}
});
}
}
I am just messing around with JPanels and I cannot seem to add scrolling to the left top pane. I think i initialized scrollPane right, but am I adding it to the wrong pane?
Your initial problem is here
leftTop.setPreferredSize(new Dimension(266, 300));
This is overriding what the layout manager (FlowLayout in this case) would otherwise provide to the JScrollPane in order for it to know how to manage the view (when to show the scrollbars for instance)
The next problem you will have is, FlowLayout won't do what you want it to. Instead you might want to use GridLayout or maybe GridBagLayout instead
JPanel leftTop = new JPanel(new GridBagLayout());
//leftTop.setPreferredSize(new Dimension(266, 300));
leftTop.setBackground(Color.black);
JScrollPane scrollPane = new JScrollPane(leftTop); //problem is here
left.add(scrollPane);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1;
gbc.insets = new Insets(5, 10, 5, 10);
JButton jb = new JButton();
jb.setPreferredSize(new Dimension(250, 50));
leftTop.add(jb, gbc);
JButton jb1 = new JButton();
jb1.setPreferredSize(new Dimension(250, 50));
leftTop.add(jb1, gbc);
JButton jb2 = new JButton();
jb2.setPreferredSize(new Dimension(250, 50));
leftTop.add(jb2, gbc);
JButton jb3 = new JButton();
jb3.setPreferredSize(new Dimension(250, 50));
leftTop.add(jb3, gbc);
JButton jb4 = new JButton();
jb4.setPreferredSize(new Dimension(250, 50));
leftTop.add(jb4, gbc);
JButton jb5 = new JButton();
jb5.setPreferredSize(new Dimension(250, 50));
leftTop.add(jb5, gbc);
JButton jb6 = new JButton();
jb6.setPreferredSize(new Dimension(250, 50));
leftTop.add(jb6, gbc);
Know, if that's not meeting your needs, you will need to create a custom component which implements Scrollable, which will allow you to specify PreferredScrollableViewportSize which will tell the JScrollPane what the preferred size of the viewable area should be, rather then using the preferredSize of the view

Java GUI Programming close the current Frame

guys I am new to java programming now I have to deal with GUI programming. I have this simple program for admin to create player for now. I have a view class to show the menus, so when user click on create player the interface will be shown.
then within that menu I have a actionListener which goes to another method and show the pop up menu which then show player created successfully. Now the problem is how can I re show the main menu, and not the create player menu.
There will be a few functions for the admin to deal with but now I just have to finish the first function and the rest will be similar. When clicking on the main menu(create player button) it will go to another function(JFrame) to ask for input, again go to another function to getText from the textfield and save it to file
my codes for tpublic void show() {
JFrame frame = new JFrame("Admin");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainPanel = new JPanel();
mainPanel.setPreferredSize(new Dimension(300,300));
panel1 = new JPanel(new GridLayout(6,1));
text = new JLabel("Admin Main Menu");
text.setFont(new Font("Lucida",Font.PLAIN,24));
//set panel layout (rows,cols,hgap,vgap)
panel1.setLayout(new GridLayout(0,1,10,10));
button1 = new JButton("Create a player");
button2 = new JButton("Delete a player");
button3 = new JButton("Top up Player's Chips");
button4 = new JButton("Reset Player's password");
button5 = new JButton("Change admin's password");
button6 = new JButton("Logout");
mainPanel.add(text);
panel1.add(button1);
panel1.add(button2);
panel1.add(button3);
panel1.add(button4);
panel1.add(button5);
panel1.add(button6);
mainPanel.add(panel1);
frame.add(mainPanel);
frame.pack();
frame.setVisible(true);
button1.addActionListener(new createPlayerListener());
button1 will got to create player menu
private class createPlayerListener implements ActionListener
{
#Override
public void actionPerformed(ActionEvent event)
{
JFrame frame1 = new JFrame("Admin");
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
text = new JLabel("Create a Player");
text.setFont(new Font("Lucida",Font.PLAIN,24));
mainPanel = new JPanel(new GridLayout(3,1));
mainPanel.setPreferredSize(new Dimension(500,400));
panel1 = new JPanel();
//panel1.setPreferredSize(new Dimension(300,200));
panel1.setLayout(new GridLayout(4,2,10,10));
name = new JLabel("Enter new player name:");
nameTextfield = new JTextField();
pw = new JLabel("Enter new player password:");
pwTextfield = new JTextField();
chip = new JLabel("Enter new player chips:");
chipTextfield = new JTextField();
button1 = new JButton("Create Player");
mainPanel.add(text);
panel1.add(name);
panel1.add(nameTextfield);
panel1.add(pw);
panel1.add(pwTextfield);
panel1.add(chip);
panel1.add(chipTextfield);
mainPanel.add(panel1);
mainPanel.add(button1);
frame1.add(mainPanel);
frame1.pack();
frame1.setVisible(true);
button1.addActionListener(new playerListener());
}
}
private class playerListener implements ActionListener
{
#Override
public void actionPerformed(ActionEvent event)
{
String name = nameTextfield.getText();
String pw = pwTextfield.getText();
String chip = chipTextfield.getText();
int chips = Integer.parseInt(chip);
//System.out.println(text);
controller.createPlayer(name, pw, chips);
//pop up window player created
JFrame frame2 = new JFrame();
JOptionPane.showMessageDialog(frame2, "Player Created Successfully!");
}
}
Two issues with your code, creating mainPanel twice, you can do so if you define it locally not globally, otherwise change the names to mainPanel1 and mainPanel2, the second issue is you are calling frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); which closes the application once you close frame1. I tried this code and it works, see the changes I made to it,
JPanel mainPanel = new JPanel();
mainPanel.setPreferredSize(new Dimension(300,300));
JPanel panel1 = new JPanel(new GridLayout(6,1));
JLabel text = new JLabel("Admin Main Menu");
text.setFont(new Font("Lucida",Font.PLAIN,24));
//set panel layout (rows,cols,hgap,vgap)
panel1.setLayout(new GridLayout(0,1,10,10));
JButton button1 = new JButton("Create a player");
JButton button2 = new JButton("Delete a player");
JButton button3 = new JButton("Top up Player's Chips");
JButton button4 = new JButton("Reset Player's password");
JButton button5 = new JButton("Change admin's password");
JButton button6 = new JButton("Logout");
mainPanel.add(text);
panel1.add(button1);
panel1.add(button2);
panel1.add(button3);
panel1.add(button4);
panel1.add(button5);
panel1.add(button6);
mainPanel.add(panel1);
add(mainPanel, BorderLayout.CENTER);
button1.addActionListener(new createPlayerListener());
Here is your createPlayerListner modified, the playerListner stays the same,
private class createPlayerListener implements ActionListener
{
#Override
public void actionPerformed(ActionEvent event)
{
JFrame frame1 = new JFrame("Admin");
JLabel text = new JLabel("Create a Player");
text.setFont(new Font("Lucida",Font.PLAIN,24));
JPanel mainPanel = new JPanel(new GridLayout(3,1));
mainPanel.setPreferredSize(new Dimension(500,400));
JPanel panel1 = new JPanel();
//panel1.setPreferredSize(new Dimension(300,200));
panel1.setLayout(new GridLayout(4,2,10,10));
JLabel name = new JLabel("Enter new player name:");
nameTextfield = new JTextField();
JLabel pw = new JLabel("Enter new player password:");
pwTextfield = new JTextField();
JLabel chip = new JLabel("Enter new player chips:");
chipTextfield = new JTextField();
JButton button1 = new JButton("Create Player");
mainPanel.add(text);
panel1.add(name);
panel1.add(nameTextfield);
panel1.add(pw);
panel1.add(pwTextfield);
panel1.add(chip);
panel1.add(chipTextfield);
mainPanel.add(panel1);
mainPanel.add(button1);
frame1.add(mainPanel);
frame1.pack();
frame1.setVisible(true);
button1.addActionListener(new playerListener());
}
}

GUI Layout for User Interface

I am trying to get a userinterface page to display correctly on my page. It has to display a certain way on the screen but I cant get the correct Lay out to show up. Any suggestions or Help?! I've tried many things but Java can get a bit confusing. ATTACHED IS A LINK FOR IMAGE OF HOW ITS SUPPOSED TO LOOK
https://courses.eas.asu.edu/cse205/current/assignments/assignment6/assignment6.html
public CreatePanel(Vector accountList, TransferPanel tPanel)
{
this.accountList = accountList;
this.transferPanel = tPanel;
JLabel label1 =new JLabel("Account ID: ");
JLabel label2 = new JLabel("Amount: ");
JTextField field1 = new JTextField();
field1.setPreferredSize(new Dimension(250,70));
JTextField field2 = new JTextField();
field2.setPreferredSize(new Dimension(250,70));
button1 = new JButton("Create an Account");
JTextArea textArea = new JTextArea();
textArea.setPreferredSize(new Dimension(500, 600));
textArea.append("No account");
textArea.setEditable(true);
JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayout(3,3));
panel1.add(label1);
panel1.add(field1);
panel1.add(label2);
panel1.add(field2);
JPanel panel2 = new JPanel();
panel2.setLayout(new BorderLayout());
panel2.add(button1, BorderLayout.SOUTH);
JPanel panel3 = new JPanel();
panel3.setLayout(new BorderLayout());
panel3.add(textArea, BorderLayout.WEST);
add(panel1);
add(panel3);
add(panel2);
//ActionListener listener = new ButtonListener();
//button1.addActionListener(listener);
}

Categories