Stupid question, but I cant seem to find answer, and when I do it doesn't work. So i want to add new JPanel on already existing JPanel. Sometimes when i add it it just opens a new window when I run it, other times nothing happens. Anyways here is the code:
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Main extends JFrame {
private static final long serialVersionUID = 1L;
public static void main(String[] args)
{
new Main().setVisible(true);
}
private Main()
{
super("Vending machine");
JPanel p = new JPanel();
JLabel title = new JLabel("Vending machine: ");
JButton button1 = new JButton("1");
JButton button2 = new JButton("2");
JButton button3 = new JButton("5");
JLabel label1 = new JLabel("Enter code: ");
JTextField text1= new JTextField(3);
JButton ok= new JButton("OK");
JButton button4 = new JButton("Return change");
JLabel label2 = new JLabel("Result is: ");
JTextField text2= new JTextField(3);
JLabel label3 = new JLabel("Current: ");
JTextField text3= new JTextField(3);
title.setBounds(200,5,250,80);
title.setFont (title.getFont ().deriveFont (22.0f));
p.add(title);
p.setLayout(null);
button1.setBounds(530,46,120,60);
p.add(button1);
button2.setBounds(530,172,120,60);
p.add(button2);
button3.setBounds(530,298,120,60);
p.add(button3);
label1.setBounds(555,414,120,60);
p.add(label1);
text1.setBounds(530,454,120,30);
p.add(text1);
ok.setBounds(530,550,120,60);
p.add(ok);
button4.setBounds(360,550,120,60);
p.add(button4);
label2.setBounds(230,530,120,60);
p.add(label2);
text2.setBounds(200,575,120,30);
p.add(text2);
label3.setBounds(50,530,120,60);
p.add(label3);
text3.setBounds(38,575,120,30);
p.add(text3);
getContentPane().add(p);
setSize(700,700);
setVisible(true);
}
}
I want to add new JPanel on this place: vending machine:
Thank you!
Even if you could do this, it will give you harm for every time you want another changes on this frame.
Instead of locating a JPanel into another JPanel, use layouts.
You shouldnt use static variables and a null layout.
Use appropriate layout managers. Maybe the main panel uses a BorderLayout. Then you add your main component to the CENTER and a second panel to the EAST. The second panel can also use a BorderLayout. You can then add the two components to the NORTH, CENTER or SOUTH as you require.
Related
Learning to use MigLayout. I am trying to make a very simple form that is two columns, a label, a text input, repeat, until a submit button on the bottom that should span both columns. However it does not seem to be working as expected. The text fields are not spreading and neither is the button. It looks like this
Here is my code -
package com.mypackage;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import net.miginfocom.swing.MigLayout;
public class DiceOddCalculator extends JFrame {
public DiceOddCalculator(String title) {
this.setSize(500, 500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setTitle(title);
//Layout and panel to house components
MigLayout layout = new MigLayout("wrap 2, debug");
JPanel panel = new JPanel(layout);
//Components
JLabel maxDiceLabel = new JLabel("Max value of die");
JTextField maxDiceInput = new JTextField();
JLabel protectionCriteriaLabel = new JLabel("Die protection Criteria:");
JTextField protectionCriteriaInput = new JTextField("");
JLabel numOfDiceLabel = new JLabel("Number of dice:");
JTextField numOfDiceInput = new JTextField("");
JButton addDice = new JButton("Add dice");
//add to panel
panel.add(maxDiceLabel);
panel.add(maxDiceInput);
panel.add(protectionCriteriaLabel);
panel.add(protectionCriteriaInput);
panel.add(numOfDiceLabel);
panel.add(numOfDiceInput);
panel.add(addDice, "span");
//Turn window on
this.setContentPane(panel);
this.setVisible(true);
}
public static void main(String[] args) {
DiceOddCalculator f = new DiceOddCalculator("Dice Odds Simulator");
}
}
What needs to change in my code so that the JTextField and Button are the appropriate widths?
I figured out it had to do with how I was constructing MigLayout. Using
MigLayout layout = new MigLayout("wrap 2, debug", "[fill, grow]", "") made the component stretch across its column as I was expecting.
I'm working on a question that simply states to make an GUI that looks like This calculator, it doesn't have to function, just look like it. So I think I have the JPanel and JButton components right but I can't organize the fields to make it come out right. I'm pretty new so any crash course on how to organize a GUI would be appreciated.
Here's what I have so far:
// Using a JPanel to help lay out components.
import java.awt.GridLayout;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextField;
public class Calculator extends JFrame
{
private final JPanel buttonJPanel2, buttonJPanel3, buttonJPanel4,
buttonJPanel5; // panel to hold buttons
private final JButton[] buttons3, buttons4, buttons5;
private final JButton[] buttons2;
private final JTextField buttonJPanel1;
// no-argument constructor
public Calculator()
{
super("Calculator");
buttonJPanel1 = new JTextField();
add(buttonJPanel1, BorderLayout.NORTH); // add panel1 to JFrame
buttons2 = new JButton[4];
buttons2[0] = new JButton("7");
buttons2[1] = new JButton("8");
buttons2[2] = new JButton("9");
buttons2[3] = new JButton("/");
buttonJPanel2 = new JPanel();
buttonJPanel2.setLayout(new GridLayout(1, buttons2.length));
add(buttonJPanel2, BorderLayout.AFTER_LAST_LINE); // add panel2 to JFrame
buttons3 = new JButton[4];
buttons3[0] = new JButton("4");
buttons3[1] = new JButton("5");
buttons3[2] = new JButton("6");
buttons3[3] = new JButton("*");
buttonJPanel3 = new JPanel();
buttonJPanel3.setLayout(new GridLayout(1, buttons3.length));
add(buttonJPanel3, BorderLayout.AFTER_LAST_LINE); // add panel3 to JFrame
buttons4 = new JButton[4];
buttons4[0] = new JButton("1");
buttons4[1] = new JButton("2");
buttons4[2] = new JButton("3");
buttons4[3] = new JButton("-");
buttonJPanel4 = new JPanel();
buttonJPanel4.setLayout(new GridLayout(1, buttons4.length));
add(buttonJPanel4, BorderLayout.AFTER_LAST_LINE); // add panel4 to JFrame
buttons5 = new JButton[4];
buttons2[0] = new JButton("0");
buttons5[1] = new JButton(".");
buttons5[2] = new JButton("=");
buttons5[3] = new JButton("+");
buttonJPanel5 = new JPanel();
buttonJPanel5.setLayout(new GridLayout(1, buttons5.length));
add(buttonJPanel5, BorderLayout.AFTER_LAST_LINE); // add panel5 to
//JFrame
}
public static void main(String[] args)
{
PanelFrame panelFrame = new PanelFrame();
panelFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panelFrame.setSize(700, 500);
panelFrame.setVisible(true);
}
} // end class PanelFrame
In short: every component has to be declared,
JButton button1;
initialized
button1 = new JButton("Click me!");
and added to the component above it in the hierarchy (in this case the panel)
panel1.add(button1);
If you do not add the components to the panel and the panel to the frame they will not be part of the GUI, thus not visible.
A JPanel can be set to adjust its layout in different ways using a LayoutManager as you have done with GridLayout (which seems fitting for a calculator). I suggest you read about how to use the grid layout here.
Hope I could help :)
You need to add the buttons to the `JPanel'. For example:
for(JButton b : buttons2) { buttonJPanel2.add(b); }
BorderLayout accepts one component at each location, so if you set BorderLayout.AFTER_LAST_LINE twice, the last add overwrites previous one.
import java.awt.*;
import java.applet.*;
import javax.swing.*;
import java.awt.event.*;
public class GUI extends JFrame implements ActionListener
{
GUI()
{
JFrame frame = new JFrame();
JLabel L_Name = new JLabel("Name");
JLabel L_Roll = new JLabel("Roll no");
JLabel L_Year = new JLabel("Year");
JLabel L_Branch = new JLabel("Branch");
JLabel L_Marks = new JLabel("Aggr Marks");
JTextField T_Name = new JTextField(15);
JTextField T_Roll = new JTextField(15);
JTextField T_Year = new JTextField(15);
JTextField T_Branch = new JTextField(15);
JTextField T_Marks = new JTextField(15);
JLabel R_Name = new JLabel("");
JLabel R_Roll = new JLabel("");
JLabel R_Year = new JLabel("");
JLabel R_Branch = new JLabel("");
JLabel R_Marks = new JLabel("");
JLabel R_aggr = new JLabel("");
JButton submit = new JButton("Submit");
JPanel p1 = new JPanel(new GridLayout(6,3,7,7));
p1.add(L_Name);
p1.add(T_Name);
p1.add(R_Name);
p1.add(L_Roll);
p1.add(T_Roll);
p1.add(R_Roll);
p1.add(L_Year);
p1.add(T_Year);
p1.add(R_Year);
p1.add(L_Branch);
p1.add(T_Branch);
p1.add(R_Branch);
p1.add(L_Marks);
p1.add(T_Marks);
p1.add(R_Marks);
p1.add(R_aggr);
submit.addActionListener(this);
p1.add(submit);
frame.add(p1);
frame.setLayout(null);
frame.setLocationRelativeTo(null);
frame.setSize(300,330);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
}
public static void main(String args[])
{
GUI g = new GUI();
}
}
I've added the JPanel to the JFrame, it is still not visible.
I am not getting any errors
This is what I am trying to do:
Create a GUI to enter the details of a student on the left side of the window. The following details are required: Name, Roll number, Branch (use radio button), Year (drop down list) and Aggregate marks. On clicking a submit button the consolidated details along with the total marks should be printed on the right side of the GUI (Use Swing and frame).
You're shooting yourself in the foot with this line:
frame.setLayout(null);
When you do this, you the programmer are completely responsible for specifying all the sizes and positions of components added to the container. While null layouts and setBounds() might seem to Swing newbies like the easiest and best way to create complex GUI's, the more Swing GUI'S you create the more serious difficulties you will run into when using them. They won't resize your components when the GUI resizes, they are a royal witch to enhance or maintain, they fail completely when placed in scrollpanes, they look gawd-awful when viewed on all platforms or screen resolutions that are different from the original one.
So don't do this but instead use the layout managers and let them do the work for you.
Also as a side recommendation, I recommend that you try to improve the formatting of your code that you post in here and your code in general. Good formatting including using an indentation style that is uniform and consistent will help others (us!) to better understand your code, and more importantly, it will help you to better understand your code and thus fix your own bugs. Also it shows that you're willing to put in extra effort to make it easier for the volunteers here to help you, and that effort is much appreciated.
e.g., your code should look like this:
import java.awt.*;
import java.applet.*;
import javax.swing.*;
import java.awt.event.*;
public class GUI extends JFrame implements ActionListener {
GUI() {
JFrame frame = new JFrame();
JLabel L_Name = new JLabel("Name");
JLabel L_Roll = new JLabel("Roll no");
JLabel L_Year = new JLabel("Year");
JLabel L_Branch = new JLabel("Branch");
JLabel L_Marks = new JLabel("Aggr Marks");
JTextField T_Name = new JTextField(15);
JTextField T_Roll = new JTextField(15);
JTextField T_Year = new JTextField(15);
JTextField T_Branch = new JTextField(15);
JTextField T_Marks = new JTextField(15);
JLabel R_Name = new JLabel("");
JLabel R_Roll = new JLabel("");
JLabel R_Year = new JLabel("");
JLabel R_Branch = new JLabel("");
JLabel R_Marks = new JLabel("");
JLabel R_aggr = new JLabel("");
JButton submit = new JButton("Submit");
JPanel p1 = new JPanel(new GridLayout(6, 3, 7, 7));
p1.add(L_Name);
p1.add(T_Name);
p1.add(R_Name);
p1.add(L_Roll);
p1.add(T_Roll);
p1.add(R_Roll);
p1.add(L_Year);
p1.add(T_Year);
p1.add(R_Year);
p1.add(L_Branch);
p1.add(T_Branch);
p1.add(R_Branch);
p1.add(L_Marks);
p1.add(T_Marks);
p1.add(R_Marks);
p1.add(R_aggr);
submit.addActionListener(this);
p1.add(submit);
frame.add(p1);
// frame.setLayout(null); // *** Get rid of
frame.setLocationRelativeTo(null);
// frame.setSize(300, 330); // *** Get rid of
frame.pack(); // **** add
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
}
public static void main(String args[]) {
GUI g = new GUI();
}
}
okay ... my question is rather straight forward so I doubt ill need to add any code but I will if need be.
Whenever I create a GUI frame and add a couple of panels to it and run my application, the contents are not displayed until I either re-size the window or minimize it on the toolbar then restore it. What could be the cause of that and how can I solve it?
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public final class Calculator extends JFrame
{
//initialise various variables for use within the program
//BUTTONS
private final JButton additionButton = new JButton("+");
private final JButton subtractionButton = new JButton("-");
private final JButton divisionButton = new JButton("/");
private final JButton multiplicationButton = new JButton("*");
//PANELS
private JPanel operatorPanel;
private JPanel operandPanel;
//LABELS
private JLabel operationLabel;
//constructor to initialise the frame and add components into it
public Calculator()
{
super("Clancy's Calculator");
setLayout(new BorderLayout(5, 10));
setSize(370, 200);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
//create a message label to display the operation that has just taken place
operationLabel = new JLabel("YOU HAVE PERFORMED SOME OPERATION",SwingConstants.CENTER);
add(getOperatorPanel(), BorderLayout.NORTH);
add(getOperandPanel(), BorderLayout.CENTER);
add(operationLabel, BorderLayout.SOUTH);
}
//setter method for the operator panel
public void setOperatorPanel()
{
operatorPanel = new JPanel();
operatorPanel.setLayout(new FlowLayout());
operatorPanel.add(additionButton);
operatorPanel.add(subtractionButton);
operatorPanel.add(multiplicationButton);
operatorPanel.add(divisionButton);
}
//getter method for the operator panel
public JPanel getOperatorPanel()
{
setOperatorPanel();
return operatorPanel;
}
//setter method for operands panel
public void setOperandPanel()
{
operandPanel = new JPanel();
operandPanel.setLayout(new GridLayout(3, 2, 5, 5));
//LABELS
JLabel operandOneLabel = new JLabel("Enter the first Operand: ");
JLabel operandTwoLabel = new JLabel("Enter the second Operand: ");
JLabel answerLabel = new JLabel("ANSWER: ");
//TEXT FIELDS
JTextField operandOneText = new JTextField(); //retrieves one operand
JTextField operandTwoText = new JTextField(); //retrieves another operand
JTextField answerText = new JTextField(); //displays answer
answerText.setEditable(false); //ensure the answer field is not editable
operandPanel.add(operandOneLabel);
operandPanel.add(operandOneText);
operandPanel.add(operandTwoLabel);
operandPanel.add(operandTwoText);
operandPanel.add(answerLabel);
operandPanel.add(answerText);
}
//getter method for operand panel
public JPanel getOperandPanel()
{
setOperandPanel();
return operandPanel;
}
/** main method */
public static void main(String[] args)
{
new Calculator();
}
}
I notice you're programatically setting a new layout manager. Whenever you add, remove or change a java gui, you need to call invalidate() or revalidate() to make java re-create the GUI. See if calling invalidate() before setVisible(true) fixes the problem
below is my user class for guiFrames. I want a class that creates several JFrames with methods and pass those methods into cardLayout. The reason for this is each JFrame will be have different buttons displayed depending on what the user has selected.
So, I thought I would create methods for the individual panels and depending on the parameter passed have different buttons displayed. I need the panels to be displayed in a cardLayout. But I am unable to pass the pass the methods into the cardLayout.add(); because it says the method type is invalid. So I tried to make the method return a Component but its not working out. Help please.
import javax.swing.*;
import java.awt.*;
public class guiFrames extends JFrame{
public guiFrames(){
}
public Component inputFrame(){
JFrame inputFrame = new JFrame("Input");
JPanel inputPnl = new JPanel();
inputPnl.setLayout(new GridLayout(3,2));
JLabel loginLbl = new JLabel("Login");
inputPnl.add(loginLbl);
JTextField loginTxt = new JTextField();
inputPnl.add(loginTxt);
JLabel pwLbl = new JLabel("Password");
inputPnl.add(pwLbl);
JTextField pwTxt = new JTextField();
inputPnl.add(pwTxt);
JPanel buttonPnl = new JPanel();
buttonPnl.setLayout(new FlowLayout(FlowLayout.LEFT, 1,5));
JButton submit = new JButton("Submit");
buttonPnl.add(submit);
JButton output = new JButton("Output");
buttonPnl.add(output);
JPanel container = new JPanel();
container.setLayout(new BorderLayout());
container.add(inputPnl, BorderLayout.CENTER);
container.add(buttonPnl, BorderLayout.SOUTH);
inputFrame.add(container);
inputFrame.pack();
inputFrame.setVisible(true);
inputFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
return;
}
public void cardView(){
JFrame cardFrame = new JFrame();
JPanel cardGUI = new JPanel();
CardLayout cards = new CardLayout();
cardGUI.setLayout(cards);
cardGUI.add(inputFrame(), "first");
cardFrame.add(cardGUI, BorderLayout.CENTER);
cardFrame.pack();
cardFrame.setVisible(true);
cardFrame.setDefaultCloseOperation(cardFrame.EXIT_ON_CLOSE);
}
}
At the end of inputFrame() you aren't returning anything. You need to return the inputFrame, like this:
return inputFrame;
Hope that helps.