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.
Related
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 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.
First time posting so go easy on me.
I am new to Java and am trying to get 3 JPanels to line up on top of each other. The first image is how I want it to look and it does sometimes when I run the program but as you can see by the other images it doesn't line up every time I run it. Sometimes not even showing some of the images/components.
So how can I get three JPanels to line up one after the other vertically?
import java.awt.event.ActionListener;
import java.awt.*;
import javax.swing.*;
public class FrameMain {
static final int MY_MINIMUM = 0;
static final int MY_MAXIMUM = 100;
public static void main(String[] args) {
JFrame frame1 = new JFrame("Harvest Frame Test");
frame1.setVisible(true);
frame1.setSize(800,700);
frame1.setResizable(false);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Container Panel
JPanel container = new JPanel();
container.setSize(800,700);
container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
frame1.add(container);
//First Panel
JPanel panel1 = new JPanel();
panel1.setAlignmentX( Component.LEFT_ALIGNMENT );//0.0
container.add(panel1);
JButton button1 = new JButton("Add Water");
panel1.add(button1);
JButton button2 = new JButton("Add Food");
panel1.add(button2);
JButton button3 = new JButton("Add Medicine");
panel1.add(button3);
ImageIcon image = new ImageIcon("C:/Users/Nick/Documents/EclipseArt/plant.gif");
JLabel imagelabel = new JLabel(image);
panel1.add(imagelabel);
JProgressBar pbar = new JProgressBar();
pbar.setMinimum(MY_MINIMUM);
pbar.setMaximum(MY_MAXIMUM);
// add to JPanel
panel1.add(pbar);
// Second Panel
JPanel panel2 = new JPanel();
panel2.setAlignmentX( Component.LEFT_ALIGNMENT );//0.0
container.add(panel2);
JButton button4 = new JButton("Add Water");
panel2.add(button4);
JButton button5 = new JButton("Add Food");
panel2.add(button5);
JButton button6 = new JButton("Add Medicine");
panel2.add(button6);
ImageIcon image1 = new ImageIcon("C:/Users/Nick/Documents/EclipseArt/plant.gif");
JLabel imagelabel1 = new JLabel(image1);
panel2.add(imagelabel1);
JProgressBar pbar1 = new JProgressBar();
pbar1.setMinimum(MY_MINIMUM);
pbar1.setMaximum(MY_MAXIMUM);
// add to JPanel
panel2.add(pbar1);
// Third Panel
JPanel panel3 = new JPanel();
panel3.setAlignmentX( Component.LEFT_ALIGNMENT );//0.0
container.add(panel3);
JButton button7 = new JButton("Add Water");
panel3.add(button7);
JButton button8 = new JButton("Add Food");
panel3.add(button8);
JButton button9 = new JButton("Add Medicine");
panel3.add(button9);
ImageIcon image2 = new ImageIcon("C:/Users/Nick/Documents/EclipseArt/plant.gif");
JLabel imagelabel2 = new JLabel(image2);
panel3.add(imagelabel2);
JProgressBar pbar2 = new JProgressBar();
pbar2.setMinimum(MY_MINIMUM);
pbar2.setMaximum(MY_MAXIMUM);
// add to JPanel
panel3.add(pbar2);
}
//static class Action implements ActionListener {
//public void actionPerformed (ActionEvent e){
//}
//}
}
Move the frame1.setVisible(true); all the way to the bottom. Changing Components on a frame that is already visible can cause issues.
Hello this is really long I'm sorry My code works but how do I print multiple numbers to my label with just pressing buttons. My code isn't complete but I just want a push in the right direction. Thank you for any help you can give.
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
public class NumericKeyPadPanel extends JPanel {
private JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, clear;
private JLabel label1;
private JPanel primary, panel2, panel3, panel4;
public NumericKeyPadPanel() {
label1 = new JLabel();
primary = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
panel4 = new JPanel();
//Set up for the center grid
panel2.setLayout(new GridLayout(4, 3));
panel2.setBackground(Color.gray);
panel2.setBorder(BorderFactory.createLineBorder(Color.black, 4));
//set up for the top box
panel3.setBackground(Color.white);
panel3.setBorder(BorderFactory.createEtchedBorder());
panel3.add(label1);
//set up for the bottom
panel4.setBackground(Color.gray);
//set up for the buttons for center and bottom
ButtonListener listener1 = new ButtonListener();
ButtonListener listener2 = new ButtonListener();
ButtonListener listener3 = new ButtonListener();
ButtonListener listener4 = new ButtonListener();
ButtonListener listener5 = new ButtonListener();
ButtonListener listener6 = new ButtonListener();
ButtonListener listener7 = new ButtonListener();
ButtonListener listener8 = new ButtonListener();
ButtonListener listener9 = new ButtonListener();
ButtonListener listener10 = new ButtonListener();
ButtonListener listener11 = new ButtonListener();
ButtonListener listener12 = new ButtonListener();
ButtonListener listener13 = new ButtonListener();
b1 = new JButton("1");
b1.addActionListener(listener1);
b2 = new JButton("2");
b2.addActionListener(listener2);
b3 = new JButton("3");
b3.addActionListener(listener3);
b4 = new JButton("4");
b4.addActionListener(listener4);
b5 = new JButton("5");
b5.addActionListener(listener5);
b6 = new JButton("6");
b6.addActionListener(listener6);
b7 = new JButton("7");
b7.addActionListener(listener7);
b8 = new JButton("8");
b8.addActionListener(listener8);
b9 = new JButton("9");
b9.addActionListener(listener9);
b10 = new JButton("*");
b10.addActionListener(listener10);
b11 = new JButton("0");
b11.addActionListener(listener11);
b12 = new JButton("#");
b12.addActionListener(listener12);
clear = new JButton("Clear");
clear.addActionListener(listener13);
panel2.add(b1);
panel2.add(b2);
panel2.add(b3);
panel2.add(b4);
panel2.add(b5);
panel2.add(b6);
panel2.add(b7);
panel2.add(b8);
panel2.add(b9);
panel2.add(b10);
panel2.add(b11);
panel2.add(b12);
panel4.add(clear);
//set up for main panel
primary.setLayout(new BorderLayout());
primary.setBackground(Color.gray);
primary.add(panel2, BorderLayout.CENTER);
primary.add(panel3, BorderLayout.NORTH);
primary.add(panel4, BorderLayout.SOUTH);
add(primary);
}
// this is the listener
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (event.getSource() == b1) {
label1.setText("1");
}
}
}
}
Store the text that you want to print on a label in a separate instance variable (String). Every time a button is pressed, append the number to this text and set it on the label.
Simply append the result to what the label already contains
label.setText(label.getText() + "1");
How do I connect a frame to an existing frame?
The code below is the code for the appletframe. What I want to do is add the other code which is for the frame to be connected to the bottom of the AppletFrame, so that when I drag the Appletframe the frame code with we dragged with it as well. Basically I want the frame code to be attached with the appletFrame so that both the frames are together.
AppletFrame
appletFrame = new JFrame(Settings.serverName);
Loader.webclient = false;
appletFrame.setLayout(new BorderLayout());
appletFrame.setDefaultCloseOperation(3);
appletPanel.setLayout(new BorderLayout());
appletFrame.setIconImage(Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("/resources/icon.png")));
appletPanel.add(this);
appletPanel.setPreferredSize(new Dimension(767, 537));
appletFrame.getContentPane().add(appletPanel, "Center");
appletFrame.pack();
appletFrame.setLocationRelativeTo(null);
appletFrame.setVisible(true);
JMenuBar jmenubar = new JMenuBar();
appletFrame.setJMenuBar(jmenubar);
Layout = new FlowLayout();
ImageIcon keyboard = new ImageIcon(Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("/resources/keyboard.png")));
ImageIcon wrench = new ImageIcon(Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("/resources/wrench.png")));
Button1 = new JButton("Vote");
Button2 = new JButton("Item List");
Button3 = new JButton("Screenshot");
Button4 = new JButton(wrench);
Button5 = new JButton(keyboard);
Button4.setBorder(null);
Button4.setBorderPainted(false);
Button4.setContentAreaFilled(false);
Button5.setBorder(null);
Button5.setBorderPainted(false);
Button5.setContentAreaFilled(false);
jmenubar.setLayout(Layout);
jmenubar.add(Button1);
jmenubar.add(Button2);
jmenubar.add(Button3);
jmenubar.add(Button4);
jmenubar.add(Button5);
Button1.addActionListener(this);
Button2.addActionListener(this);
Button3.addActionListener(this);
Button4.addActionListener(this);
Button5.addActionListener(this);
Button1.setText("Vote");
Button2.setText("Item List");
Button3.setText("Screenshot");
Frame which I want it to be attached with the AppletFrame. I want this to be attached to the bottom of the appletFrame, but I don't know how to do it.
JFrame frame = new JFrame();
frame.setSize(775,121);
frame.setResizable(false);
JTextArea textArea = new JTextArea("TEST");
textArea.setSize(400,400);
textArea.setLineWrap(true);
textArea.setEditable(false);
textArea.setVisible(true);
JScrollPane scroll = new JScrollPane (textArea);
scroll.setVerticalScrollBarPolicy (JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
frame.add(scroll);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
As I alluded to in my first comment, this GUI would be better combined into a single top-level container.
Here is an SSCCE1 (mentioned in my 2nd comment) that shows the basic idea, though now I have a better idea of the effect required, the JSplitPane seems less appropriate. Here I just combine the GUI elements into the same layout.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class TestGUI extends JPanel {
TestGUI() {
JFrame appletFrame = new JFrame("Settings.serverName");
appletFrame.setLayout(new BorderLayout());
appletFrame.setDefaultCloseOperation(3);
JPanel appletPanel = new JPanel(new BorderLayout());
appletPanel.add(this);
appletPanel.setPreferredSize(new Dimension(767, 537));
appletFrame.getContentPane().add(appletPanel, BorderLayout.CENTER);
// Don't use a menu-bar as a tool-bar!
JToolBar jmenubar = new JToolBar();
appletPanel.add(jmenubar, BorderLayout.PAGE_START);
JButton Button1 = new JButton("Vote");
JButton Button2 = new JButton("Item List");
JButton Button3 = new JButton("Screenshot");
JButton Button4 = new JButton("wrench");
JButton Button5 = new JButton("keyboard");
Button4.setBorder(null);
Button4.setBorderPainted(false);
Button4.setContentAreaFilled(false);
Button5.setBorder(null);
Button5.setBorderPainted(false);
Button5.setContentAreaFilled(false);
jmenubar.setLayout(new FlowLayout());
jmenubar.add(Button1);
jmenubar.add(Button2);
jmenubar.add(Button3);
jmenubar.add(Button4);
jmenubar.add(Button5);
JTextArea textArea = new JTextArea("TEST", 4, 65 );
textArea.setLineWrap(true);
textArea.setEditable(false);
textArea.setVisible(true);
JScrollPane scroll = new JScrollPane (
textArea,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
appletPanel.add(scroll, BorderLayout.PAGE_END);
appletFrame.pack();
appletFrame.setLocationByPlatform(true);
appletFrame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater( new Runnable() {
public void run() {
new TestGUI();
}
});
}
}
And yes, this would have arrived sooner if I'd had an SSCCE to start with. ;)