Can't get panels to display in JFrame - java

I am trying to design a calculator shell (non-functioning calculator) using Java but for some reason I cannot get the program to display my buttons. What am I doing wrong here?
import java.awt.*;
import javax.swing.*;
import java.awt.color.*;
public class Calculator extends JFrame {
public Calculator() {
JPanel P1 = new JPanel();
P1.setLayout(new GridLayout(4, 4));
//Panel Buttons
for (int i = 1; i <= 9; i++) {
P1.add(new JButton("" + i));
}
P1.add(new JButton("" + 0));
P1.add(new JButton("."));
P1.add(new JButton("*"));
P1.add(new JButton("/"));
P1.add(new JButton("+"));
P1.add(new JButton("-"));
P1.add(new JButton("="));
P1.setBackground(Color.cyan);
P1.setForeground(new Color(100, 1, 1));
//Content panel
JPanel P2 = new JPanel();
P2.setLayout(new BorderLayout());
P2.add(new JTextField("Hello world"), BorderLayout.NORTH);
P2.add(P1, BorderLayout.CENTER);
}
public static void main(String[] args) {
Calculator frame = new Calculator();
frame.setTitle("Simple Calculator");
frame.setSize(250, 250);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
OK here is what I have now...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame
{
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
Calculator()
{
super("Wk 3 Calculator"); setBounds(100,100,300,100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container con = this.getContentPane();
con.add(p1);
con.add(p2);
p1.setLayout(new GridLayout(4, 4));
//Panel Buttons
for (int i =1; i <=9; i++){
p1.add(new JButton ("" + i));
}
p1.add(new JButton (""+0));
p1.add(new JButton ("."));
p1.add(new JButton ("*"));
p1.add(new JButton ("/"));
p1.add(new JButton ("+"));
p1.add(new JButton ("-"));
p1.add(new JButton ("="));
//Content panel
p2.setLayout(new BorderLayout());
p2.add (new JTextField("Hello world"),BorderLayout.NORTH);
p2.add(p1, BorderLayout.CENTER);
//Frame specs
Calculator frame = new Calculator();
frame.setSize(250,250);
frame.setTitle("Simple Calculator");
frame.add(p1, BorderLayout.NORTH);
frame.add(p2, BorderLayout.SOUTH);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args){new Calculator();}
}
It is still not working :(

You're not adding anything to the frame...
Try something along the lines of
add(P1, BorderLayout.NORTH);
add(P2, BorderLayout.SOUTH);
For starters...
You might like to have a look at
Creating a UI with Swing
Initial Threads
Code Conventions for the Java Programming Language

You are not adding the panels to the Frame. Put add(p1, BorderLayout.NORTH) and add(p2, BorderLayout.SOUTH) at the end of your constructor.
public Calculator()
{
//rest of the construtor
add(p1, BorderLayout.NORTH);
add(p2, BorderLayout.SOUTH);
}
And rename them so they start with a small letter. Capital letters are for class names.

add your P2 inside JFrame
this.add(P2);
this.pack();

this.add(P2);
this.pack();
this.repain();

Well:
1). Call Swing-related code on the EDT (Event Dispatch Thread), not on the main thread. See here for more details.
2). You're not adding the P1 and P2 panels to the JFrame, so when you display the frame, it doesn't contain anything. Try calling add (P1) and add (P2).
3). You should call frame.pack() before you call frame.setVisible (true).
EDIT:
I rewrote your code (you still have a loong way into making a functional application, but it's a start):
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator
{
private JFrame frame;
public Calculator()
{
frame = new JFrame ("Simple Calculator");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLayout (new BorderLayout ());
JPanel panel = new JPanel(new GridLayout(4, 4));
JPanel p2 = new JPanel(new BorderLayout());
for (int i = 1; i <= 9; i++)
{
panel.add (new JButton ("" + i));
}
panel.add(new JButton (""+0));
panel.add(new JButton ("."));
panel.add(new JButton ("*"));
panel.add(new JButton ("/"));
panel.add(new JButton ("+"));
panel.add(new JButton ("-"));
panel.add(new JButton ("="));
frame.add(panel, BorderLayout.CENTER);
frame.add (new JTextField("Hello world"), BorderLayout.SOUTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater (new Runnable ()
{
#Override
public void run ()
{
new Calculator();
}
});
}
}

Related

Swing resizing elements

I'm just trying out swing for java developement and can't for the life of me work out how to make a button have a specific size, or at least size how I want it to...
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class GUI implements ActionListener {
int count = 0;
JLabel label;
public GUI() {
JFrame frame = new JFrame("GUI");
JButton button = new JButton("CLick this");
button.addActionListener(this);
button.setPreferredSize(new Dimension(256, 256));
label = new JLabel("Number of clicks: 0");
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(30, 30, 10, 30));
panel.setPreferredSize(new Dimension(1000, 700));
panel.setLayout(new GridLayout(0, 1));
panel.add(button);
panel.add(label);
frame.add(panel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
new GUI();
}
#Override
public void actionPerformed(ActionEvent e) {
count++;
label.setText("Number of clicks " + count);
}
}
This is my basic code - I want to make the button a specific size, or size to the width of the panel, how do I do this?

JPanel inside JPanel in JAVA

public static void main(String[] args) {
JTextField text = new JTextField();
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5, 4));
JPanel panel2 = new JPanel();
panel2.setLayout(new FlowLayout());
JLabel imgLabel1 = new JLabel(new ImageIcon("C:\\Users\\Arthur\\Downloads\\abs.jpg"));
JLabel imgLabel2 = new JLabel(new ImageIcon("C:\\Users\\Arthur\\Downloads\\abss.jpg"));
imgLabel1.setPreferredSize(new Dimension(100,100));
imgLabel2.setPreferredSize(new Dimension(100,100));
panel2.add(imgLabel1);
panel2.add(imgLabel2);
for(int i=0; i<20; i++){
panel.add(panel2);
}
frame.add(text, BorderLayout.NORTH);
frame.add(panel, BorderLayout.CENTER);
frame.setPreferredSize(new Dimension(1280,700));
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
I want to make a memory game, I need to put two images in each cell of the JPanel of 4x5. For this I created a JPanel 1x2 with two images inside and put it in the JPanel of 4x5. But the result is:
Result:
So, if understand correctly, you're problem is, you're not seeing 20 new panels, only one.
The problem is, a component can only reside in a single container, once, so doing something like...
for (int i = 0; i < 20; i++) {
panel.add(panel2);
}
is the equivalent of doing something like...
panel.add(panel2);
You actually need to create a new instance of the component on each iteration of the loop
What I would suggest you do is create a "wrapper" or "card" panel which can contain the two images. In my testing I just used coloured panels, but you get the idea...
public class WrapperPane extends JPanel {
public WrapperPane() {
setLayout(new FlowLayout());
add(makePanel(Color.RED));
add(makePanel(Color.GREEN));
// This is just for demonstration purposes
setBorder(new LineBorder(Color.DARK_GRAY));
}
protected JPanel makePanel(Color background) {
JPanel panel = new JPanel();
panel.setBackground(background);
panel.setPreferredSize(new Dimension(100, 100));
return panel;
}
}
The you'd just have to do something like...
JTextField text = new JTextField();
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5, 4));
for (int i = 0; i < 20; i++) {
panel.add(new WrapperPane());
}
frame.add(text, BorderLayout.NORTH);
frame.add(panel, BorderLayout.CENTER);
// Don't do this, just let the content make it's own
// calculations
//frame.setPreferredSize(new Dimension(1280, 700));
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
And you'd end up with something like...

How can I fix blank screen on jframe and set values of vgap and hgap from textfield

How can I fix blank screen on jframe and set values of vgap and hgap from textfield. i am using borderlayout for this.
import java.awt.*;
import javax.swing.*;
public class d1{
public static void main(String[] args) {
JFrame f1 = new JFrame ("Border Layout") ;
f1.setLayout(new BorderLayout());
f1.setVisible(true);
f1.setSize(400,400);
f1.setLocationRelativeTo(null);
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextField t1 = new JTextField();
t1.setBorder(BorderFactory.createLineBorder(Color.BLACK));
JTextField t2 = new JTextField();
t2.setBorder(BorderFactory.createLineBorder(Color.BLACK));
JPanel p1 = new JPanel ();
p1.setLayout(new BorderLayout());
p1.add(new JButton("East"), BorderLayout.EAST);
p1.add(new JButton("South"), BorderLayout.SOUTH);
p1.add(new JButton("West"), BorderLayout.WEST);
p1.add(new JButton("North"), BorderLayout.NORTH);
p1.add(new JButton("Center"), BorderLayout.CENTER);
JPanel p2 = new JPanel ();
p2.setLayout(new BorderLayout());
JPanel p3 = new JPanel ();
p3.setLayout(new BorderLayout());
p3.add(new JLabel("Vgap"), BorderLayout.WEST);
p3.add(t1, BorderLayout.CENTER);
JPanel p4 = new JPanel ();
p4.setLayout(new BorderLayout());
p4.add(new JLabel("Hgap"), BorderLayout.WEST);
p4.add(t2, BorderLayout.CENTER);
JPanel p5 = new JPanel();
p5.setLayout(new BorderLayout());
p5.add(new JLabel("Container of BorderLayout"));
JPanel p6 = new JPanel();
p6.setLayout(new BorderLayout());
p6.add(new JLabel("BorderLayout Properties"));
JPanel p7 = new JPanel ();
p7.setLayout(new BorderLayout());
p7.add(p6, BorderLayout.NORTH);
p7.add(p2, BorderLayout.SOUTH);
p2.add(p3, BorderLayout.NORTH);
p2.add(p4, BorderLayout.SOUTH);
f1.add(p1, BorderLayout.CENTER);
f1.add(p7, BorderLayout.SOUTH);
f1.add(p5, BorderLayout.NORTH);
}
}
after this code.
There should be space between north-south and center, north, south and center.
I can not fix blank screen problem when frame is opened.
Add your panels before f1.setVisible(True) on frame and f1.pack() the frame after it.
You don't need to set a fixed size for your frame. Your added components should take care of it.
To set your components, look at MigLayout. It's easy to use and set components the way you need it.
please check this link you can find it here but first you should add ActionListener into your textField objects so that they can take numbers from inside of themselves. Providing white space in a Swing GUI
something like that
t1 = new JTextField();
t1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String v = t1.getText();
int numberhGap = Integer.parseInt(v);
}
});
t1.setBorder(BorderFactory.createLineBorder(Color.BLACK));
t2 = new JTextField();
t2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String h = t2.getText();
int numberhGap = Integer.parseInt(h);
}
});
and declare t1 and t2 outside main method.
like this
public class d1{
static JTextField t1;
static JTextField t2;

BorderLayout setSize issue

Before you come with GridBagLayout suggestions, I've tried that but I couldn't get it to work.
I want a frame with the size 800 x 800 and with a center panel of 600x600. Right now, when I run it the center panel is 600x578. Can someone tell me where it goes wrong? It's just 22 pixels.
public void createPlayground()
{
JFrame frame = new JFrame("ForFun Maze");
frame.setSize(WIDTH, HEIGHT);
frame.setResizable(false);
frame.setLayout(new BorderLayout());
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(3,1));
buttonPanel.setPreferredSize(new Dimension(100,600));
buttonPanel.setMaximumSize(new Dimension(100,600));
buttonPanel.setBackground(SIDEBAR);
JButton reset = new JButton();
reset.setText("Reset");
reset.addActionListener(new RestartListener());
reset.setSize(100,180);
JButton pause = new JButton();
pause.setText("Pause");
pause.setSize(100,180);
pause.addActionListener(new PauseListener());
JButton quit = new JButton();
quit.setText("Quit");
quit.setSize(100,180);
quit.addActionListener(new QuitListener());
buttonPanel.add(pause);
buttonPanel.add(reset);
buttonPanel.add(quit);
Location[][] array = null;
if(level == 1)
{
array = glevel1;
}
CenterPanel centerPanel = new CenterPanel(array);
centerPanel.setPreferredSize(new Dimension(600,600));
centerPanel.setMinimumSize(new Dimension(600,600));
centerPanel.setBackground(BACKGROUND);
JPanel leftPanel = new JPanel();
leftPanel.setBackground(SIDEBAR);
leftPanel.setPreferredSize(new Dimension(100,600));
JPanel northPanel = new JPanel();
northPanel.setBackground(SIDEBAR);
northPanel.setPreferredSize(new Dimension(800,100));
JPanel bottomPanel = new JPanel();
bottomPanel.setBackground(SIDEBAR);
bottomPanel.setPreferredSize(new Dimension(800,100));
frame.add(northPanel, BorderLayout.NORTH);
frame.add(leftPanel, BorderLayout.WEST);
frame.add(centerPanel, BorderLayout.CENTER);
frame.add(buttonPanel, BorderLayout.EAST);
frame.add(bottomPanel, BorderLayout.SOUTH);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(dim.width / 2 - frame.getSize().width / 2, dim.height / 2 - frame.getSize().height / 2);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
System.out.println("Size of centerpane"+centerPanel.getWidth()+"x"+centerPanel.getHeight());
}
Use pack() over setSize() (because you don't take the JFrame insets into account
Use setLocationRelativeTo(null) (after calling pack()) to center the frame.
Your calls to setSize() on JComponent's are useless because the LayoutManager's will override them (same goes for setBounds and setLocation).
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.xml.stream.Location;
public class Example {
public void createPlayground() {
JFrame frame = new JFrame("ForFun Maze");
frame.setResizable(false);
frame.setLayout(new BorderLayout());
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(3, 1));
buttonPanel.setPreferredSize(new Dimension(100, 600));
buttonPanel.setMaximumSize(new Dimension(100, 600));
JButton reset = new JButton();
reset.setText("Reset");
reset.setSize(100, 180);
JButton pause = new JButton();
pause.setText("Pause");
pause.setSize(100, 180);
JButton quit = new JButton();
quit.setText("Quit");
quit.setSize(100, 180);
buttonPanel.add(pause);
buttonPanel.add(reset);
buttonPanel.add(quit);
Location[][] array = null;
JPanel centerPanel = new JPanel();
centerPanel.setPreferredSize(new Dimension(600, 600));
centerPanel.setMinimumSize(new Dimension(600, 600));
JPanel leftPanel = new JPanel();
leftPanel.setPreferredSize(new Dimension(100, 600));
JPanel northPanel = new JPanel();
northPanel.setPreferredSize(new Dimension(800, 100));
JPanel bottomPanel = new JPanel();
bottomPanel.setPreferredSize(new Dimension(800, 100));
frame.add(northPanel, BorderLayout.NORTH);
frame.add(leftPanel, BorderLayout.WEST);
frame.add(centerPanel, BorderLayout.CENTER);
frame.add(buttonPanel, BorderLayout.EAST);
frame.add(bottomPanel, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
System.out.println("Size of centerpane" + centerPanel.getWidth() + "x" + centerPanel.getHeight());
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Example().createPlayground();
}
});
}
}

Jframe. adding 2 Jpanel components in one frame. Doesnt work

Problem : i'm trying to add a 2nd Jpanel in my frame but when i add this latter, it overwrites the previous one. The purpose is to have 2 components (Jpanels) in the same frame but it seem to accept only one but not both. The order of appearence should be in one column and two rows:
1: Enter name:
2: TextField
import javax.swing.*;
import java.awt.*;
public class Money2 extends JFrame {
public Money2() {
// setLayout(new GridLayout(2,2));
JPanel p1 = new JPanel();
p1.setLayout(new FlowLayout(FlowLayout.CENTER));
p1.add(new JLabel("Enter name:"));
// -------------------------------------------------------------------------
// p2.setLayout(new FlowLayout(FlowLayout.CENTER));
JPanel p2 = new JPanel(new FlowLayout());
p2.add(new JTextField(8));
add(p1); // add to Jframe
add(p2);
}
/** Main method */
public static void main(String[] args) {
Money2 frame = new Money2();
frame.setTitle("Money Converter App");
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(350, 400);
frame.setVisible(true);
}
}
Both panels cannot occupy the same location in a BorderLayout. You could place panel p1 at a different location:
add(p1, BorderLayout.PAGE_START);

Categories