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.
Related
I am new to GUI in Java (and coding in gneral I guess), so I am confused on why the JButtons on my JPanel only show up if I initialize and add the JButtons on the class that has the JFrame. What I am trying to do is create a phone keypad with JBUttons and a GridLayout on a JPanel.
If I initialize and add the JButtons to the JPanel on the class that has the JFrame in it. I can not add the JButtons to the JPanel in the class that extends the JPanel and then add the JPanel object to the JFrame.
This works:
public static void main (String[] argv){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBackground(Color.gray);
DialPanel keypad = new DialPanel();
panel.setLayout(new GridLayout(3, 3));
JButton one = new JButton("1");
JButton two = new JButton("2");
JButton three = new JButton("3");
JButton four = new JButton("4");
JButton five = new JButton("5");
JButton six = new JButton("6");
JButton seven = new JButton("7");
JButton eight = new JButton("8");
JButton nine = new JButton("9");
keypad.add(one);
keypad.add(two);
keypad.add(three);
keypad.add(four);
keypad.add(five);
keypad.add(six);
keypad.add(seven);
keypad.add(eight);
keypad.add(nine);
frame.getContentPane().add(keypad);
keypad.setVisible(true);
frame.pack();
frame.setVisible(true);
}
and
public class DialPanel extends JPanel {
DialPanel(){
JPanel panel = new JPanel();
}
}
But this does not work, and I am not sure why:
public static void main (String[] argv){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBackground(Color.gray);
DialPanel keypad = new DialPanel();
frame.getContentPane().add(keypad);
keypad.setVisible(true);
frame.pack();
frame.setVisible(true);
}
and
public class DialPanel extends JPanel {
DialPanel(){
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3, 3));
panel.setBackground(Color.gray);
JButton one = new JButton("1");
JButton two = new JButton("2");
JButton three = new JButton("3");
JButton four = new JButton("4");
JButton five = new JButton("5");
JButton six = new JButton("6");
JButton seven = new JButton("7");
JButton eight = new JButton("8");
JButton nine = new JButton("9");
panel.add(one);
panel.add(two);
panel.add(three);
panel.add(four);
panel.add(five);
panel.add(six);
panel.add(seven);
panel.add(eight);
panel.add(nine);
}
}
With the second option the window is simply just blank, but with everything in the main method it works as intended. Why won't having the buttons in the constructor work, and how can I get it to work (if I can get it to work like that at all)?
You don't need to create one more JPanel inside DialPanel (first line in DialPanel constructor).
DialPanel already extends JPanel by class definition:
public class DialPanel extends JPanel
So you were adding all your buttons to a JPanel that was never be added to any other container.
You must add all your buttons to DialPanel itself.
Correct code is:
public class DialPanel extends JPanel {
DialPanel(){
setLayout(new GridLayout(3, 3));
setBackground(Color.gray);
JButton one = new JButton("1");
JButton two = new JButton("2");
JButton three = new JButton("3");
JButton four = new JButton("4");
JButton five = new JButton("5");
JButton six = new JButton("6");
JButton seven = new JButton("7");
JButton eight = new JButton("8");
JButton nine = new JButton("9");
add(one);
add(two);
add(three);
add(four);
add(five);
add(six);
add(seven);
add(eight);
add(nine);
}
}
I want to create a gridLayout with 4 buttons at the center of my Layout, and a button to PAGE_END,PAGE_START,LINE_END,LINE_START. My code does show the last buttons I told you, but not the grid button ones.
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("Hello World!");
frame.setSize(400,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
JPanel p = new JPanel(new BorderLayout());
GridLayout grid = new GridLayout(2,2);
p.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JButton bg1 = new JButton("Button 1");
p.add(bg1, c);
JButton bg2 = new JButton("Button 2");
p.add(bg2, c);
JButton bg3 = new JButton("Button 3");
p.add(bg3, c);
JButton bg4 = new JButton("Button 4");
p.add(bg4, c);
frame.setLayout(new BorderLayout());
JButton b1 = new JButton("TOP");
JButton b2 = new JButton("LEFT");
JButton b3 = new JButton("RIGHT");
JButton b4 = new JButton("BOTTOM");
frame.add(b1,BorderLayout.PAGE_START);
frame.add(b2,BorderLayout.LINE_START);
frame.add(b3,BorderLayout.LINE_END);
frame.add(b4,BorderLayout.PAGE_END);
}
}
You have created the panel p, but you haven't added it to your frame.
Add this line in your code:
frame.add(p,BorderLayout.CENTER);
Also, you must use this line, if you want the buttons in a grid:
p.setLayout(grid);
instead of p.setLayout(new GridBagLayout());
i am having trouble with designing my button, ive created 13 and made it 5,3 but the last button does not stretch to the width of the 3 columns.
it is meant to look like this
example picture
here is my code, if anyone can offer any suggestions thatd be helpful, thank you.
import javax.swing.*;
public class Frame extends JFrame
{
public static void main(String[]args){
JFrame myFrame = new JFrame("MyFrame");
Panel myPanel = new Panel();
myFrame.add(myPanel);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setSize(300,400);
myFrame.setVisible(true);
}
}
import javax.swing.*;
import java.awt.*;
public class Panel extends JPanel
{
JButton zero,one,two,three,four,five,six,seven,eight,nine,clear, negative,convert;
JRadioButton k2p,p2k;
JTextArea area;
ButtonGroup conversion;
public Panel() {
zero = new JButton("0");
one = new JButton("1");
two = new JButton("2");
three = new JButton("3");
four = new JButton("4");
five = new JButton("5");
six = new JButton("6");
seven = new JButton("7");
eight = new JButton("8");
nine = new JButton("9");
clear = new JButton("C");
negative = new JButton("-");
convert = new JButton("Convert");
area = new JTextArea();
conversion = new ButtonGroup();
conversion.add(k2p= new JRadioButton("Kilograms to Pounds"));
conversion.add(p2k= new JRadioButton("Pounds to Kilograms"));
setLayout(new BorderLayout());
JPanel westPanel = new JPanel();
westPanel.setLayout(new GridLayout(5,3));
westPanel.add(zero);
westPanel.add(one);
westPanel.add(two);
westPanel.add(three);
westPanel.add(four);
westPanel.add(five);
westPanel.add(six);
westPanel.add(seven);
westPanel.add(eight);
westPanel.add(nine);
westPanel.add(negative);
westPanel.add(clear);
westPanel.add(convert);
add(westPanel,BorderLayout.CENTER);
}
}
GridLayout divides its space equally amongst all of its compoenents:
The GridLayout class is a layout manager that lays out a container's components in a rectangular grid. The container is divided into equal-sized rectangles, and one component is placed in each rectangle.
(emphasis mine) Your alternatives are other layout managers, like GridBagLayout, GroupLayout or using a composite layout, which means nesting layout managers one inside the other.
Here I'll be using composite layouts by nesting the GridLayout inside a BorderLayout in the CENTER position and the convert button is in the PAGE_END position:
class Panel extends JPanel {
JButton[] numberButtons = new JButton[10];
JButton nine, clear, negative, convert;
public Panel() {
for (int i = 0; i < numberButtons.length; i++)
numberButtons[i] = new JButton(String.valueOf(i));
clear = new JButton("C");
negative = new JButton("-");
convert = new JButton("Convert");
setLayout(new BorderLayout());
JPanel westPanel = new JPanel(new BorderLayout());
JPanel buttonsPanel = new JPanel(new GridLayout(4, 3));
for (int i = 0; i < numberButtons.length; i++)
buttonsPanel.add(numberButtons[i]);
buttonsPanel.add(negative);
buttonsPanel.add(clear);
westPanel.add(buttonsPanel);
westPanel.add(convert, BorderLayout.PAGE_END);
add(westPanel, BorderLayout.CENTER);
}
}
I also created a JButton[] for the numbered buttons, it's usually a better choice. You can add the other buttons there too, but I think it will only be detrimental.
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.
With this code the buttons align left, but the glue doesn't expand.. I wanted buttons 2 and 3 to float right.
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
JPanel panelBottom = new JPanel();
frame.getContentPane().add(panelBottom, BorderLayout.SOUTH);
panelBottom.setLayout(new FlowLayout(FlowLayout.LEFT));
JButton btnNewButton = new JButton("1");
panelBottom.add(btnNewButton);
Component glue = Box.createGlue();
panelBottom.add(glue);
JButton btnNewButton_1 = new JButton("2");
panelBottom.add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("3");
panelBottom.add(btnNewButton_2);
In order to use glue, you need to use a BoxLayout.