I am fairly new to java, and I am starting a sort of glossary program.
At the start, I am trying to use a JFrame with buttons on.
But only 1 button shows up when I run it.
Also, I don't think I am positioning the buttons right.
package glossary;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.*;
public class Glossary {
public static void main(String[] args) {
JFrame frame = new JFrame("Glossary");
frame.setVisible(true);
frame.setSize(400,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel1 = new JPanel();
frame.add(panel1);
JButton LookUpWord = new JButton("Look up word");
panel1.add(LookUpWord, BorderLayout.NORTH);
JPanel panel2 = new JPanel();
frame.add(panel2);
JButton SubmitNewWord = new JButton("Submit word");
panel2.add(SubmitNewWord, BorderLayout.SOUTH);
}
}
Please tell me what I am doing horribly wrong!
I think you are just getting mixed up about where to add your components. You probably aren't intending to add the buttons to NORTH and SOUTH, but rather adding the panels to the frame at NORTH and SOUTH. Also, wait until you've added all your components before calling frame.setVisible(true).
Try this:
public static void main(String[] args) {
JFrame frame = new JFrame("Glossary");
frame.setSize(400, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton LookUpWord = new JButton("Look up word"); // create the button
JPanel panel1 = new JPanel(); // create the panel
panel1.add(LookUpWord); // add the button to the panel
frame.add(panel1, BorderLayout.NORTH); // add the panel to the frame
JButton SubmitNewWord = new JButton("Submit word");
JPanel panel2 = new JPanel();
panel2.add(SubmitNewWord);
frame.add(panel2, BorderLayout.SOUTH);
frame.setVisible(true);
}
Add frame.setVisible(true);
Hoping this tutorial help you
How to Make Frames
Related
So I created a new jFrame and in it I have 2 jPanels - jPanel1 and jPanel2.
I need these 2 jPanels to swap places, the first one goes to the place of the second, and the second to the place of the first (with everything that will be inside jPanels).
What is the best way to do this?
import javax.swing.*;
import java.awt.GridLayout;
public class Class {
static JFrame frame;
static JPanel panel1 = new JPanel();
static JPanel panel2 = new JPanel();
public static void main(String[] args) {
panel1.add(new JLabel("Panel 1"));
panel2.add(new JLabel("Panel 2"));
frame = new JFrame();
frame.getContentPane().setLayout(new GridLayout(1,2));
frame.setVisible(true);
frame.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel1);
frame.getContentPane().add(panel2);
new Timer(2000,e -> {
frame.getContentPane().removeAll();
frame.getContentPane().add(panel2);
frame.getContentPane().add(panel1);
frame.getContentPane().revalidate();
}).start();
}
}
It depends on the layout you use in your parent panel, but basically, you must remove both components (content1 and content2) from the common parent panel and add them again at the other position.
I made a simple example with a (horizontal) flow layout and two panels on it with just one label on it - but it should work for more complex panels, too.
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
public class TestFrame extends JFrame {
public static void main(String[] args) throws InterruptedException {
TestFrame f = new TestFrame();
f.getContentPane().setLayout(new FlowLayout());
JPanel pnl1 = new JPanel();
pnl1.add(new JLabel("My first label"));
JPanel pnl2 = new JPanel();
pnl2.add(new JLabel("My second label"));
f.getContentPane().add(pnl1);
f.getContentPane().add(pnl2);
f.setSize(400,400);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.setVisible(true);
// wait 2 seconds and switch panels
System.out.println("before sleep");
Thread.sleep(2000);
System.out.println("after sleep");
// remove the old content - could be more "precise" in real world - not just remove all components
f.getContentPane().removeAll();
// add the components in different order (in flow layout)
f.getContentPane().add(pnl2);
f.getContentPane().add(pnl1);
// render again
f.revalidate();
}
}
How do I display two JPanels in the 'North' in borderlayout?
Here's and example code that outputs a GUI with three distinct rows, Top, Middle, Bottom. There's one button covering the first row, 3 buttons covering the second row, and one covering the bottom row.
package borderLayoutDemo;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
public class BorderLayoutDemo {
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame fj = new JFrame("Demonstration of Border Layout");
fj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton jbtn1 = new JButton("UP");
JButton jbtn2 = new JButton("DOWN");
JButton jbtn3 = new JButton("LEFT");
JButton jbtn4 = new JButton("RIGHT");
JButton jbtn5 = new JButton("MIDDLE");
JPanel pnl = new JPanel();
pnl.setLayout(new BorderLayout());
pnl.add(jbtn1, BorderLayout.NORTH);
pnl.add(jbtn2, BorderLayout.SOUTH);
pnl.add(jbtn3, BorderLayout.WEST);
pnl.add(jbtn4, BorderLayout.EAST);
pnl.add(jbtn5, BorderLayout.CENTER);
fj.add(pnl);
fj.pack();
fj.setVisible(true);
}
}
Output of above code:
output of above code
However, I'd like there to be two jpanels in the North section so it'd make 4 "rows" like this:
|---------------button--------------| //north
|---------------button2-------------| //north
----------------center--------------- //center
|---------------button3-------------| //south
I've tried simply just adding it as follows:
pnl.add(jbtn1, BorderLayout.NORTH);
pnl.add(jbtn2, BorderLayout.NORTH);
But what happens here is the second button just replaces the first one:
|---------------button2-------------| //north
----------------center--------------- //center
|---------------button3-------------| //south
How would I get two rows in the north layout area?
Creating a more complex GUI is straightforward when you think of a GUI as a JFrame with as many JPanels as necessary to define the GUI.
Here's the GUI you were looking for.
I created a JPanel for each section of the JFrame (NORTH, CENTER, and SOUTH). Each of those JPanels used a BorderLayout so that when you expand the GUI, the NORTH and SOUTH buttons stay the same height.
Here's the complete runnable example code.
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class BorderLayoutDemo implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new BorderLayoutDemo());
}
#Override
public void run() {
JFrame fj = new JFrame("Demonstration of Border Layout");
fj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fj.add(createNorthPanel(), BorderLayout.NORTH);
fj.add(createCenterPanel(), BorderLayout.CENTER);
fj.add(createSouthPanel(), BorderLayout.SOUTH);
fj.pack();
fj.setLocationByPlatform(true);
fj.setVisible(true);
}
private JPanel createNorthPanel() {
JPanel panel = new JPanel(new BorderLayout());
JButton button1 = new JButton("North Button");
panel.add(button1, BorderLayout.NORTH);
JButton button2 = new JButton("North Button 2");
panel.add(button2, BorderLayout.SOUTH);
return panel;
}
private JPanel createCenterPanel() {
JPanel panel = new JPanel(new BorderLayout());
JButton button = new JButton("Center Button");
panel.add(button, BorderLayout.CENTER);
return panel;
}
private JPanel createSouthPanel() {
JPanel panel = new JPanel(new BorderLayout());
JButton button = new JButton("South Button");
panel.add(button, BorderLayout.SOUTH);
return panel;
}
}
I don't understand why the panel.add(txtnum1) and panel.add(button2) doesn't show up when I compile the program. The panel.add(button) works just fine, my compiler doesn't throw any warning or errors, did I miss something?
package gui;
import javax.swing.*;
import java.awt.*;
public class GUI {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(new Dimension(300, 500));
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setBackground(Color.GRAY);
frame.getContentPane().add(panel);
JButton button = new JButton("Submit");
panel.add(button);
JTextField txtnum1 = new JTextField();
txtnum1.setPreferredSize(new Dimension(30, 50));
panel.add(txtnum1);
JButton button2 = new JButton("Clear");
panel.add(button2);
}
}
When implementing GUI applications with Swing, I like to have this approach in the code that builds the JFrame (we assume a simple GUI that does not have JPanel containers inside JPanel containers and stuff like that):
Create JFrame and initialize it
Create JPanel
Create GUI components for that panel and add them
Add panel to the JFrame (repeat from 2) for every JPanel inside the JFrame)
Make the JFrame visible on the screen
So, your code would look something like this:
package gui;
import javax.swing.*;
import java.awt.*;
public class GUI {
public static void main(String[] args) {
/* step 1 */
JFrame frame = new JFrame();
frame.setSize(new Dimension(300, 500));
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/* step 2 */
JPanel panel = new JPanel();
panel.setBackground(Color.GRAY);
/* step 3 */
JButton button = new JButton("Submit");
panel.add(button);
JTextField txtnum1 = new JTextField();
txtnum1.setPreferredSize(new Dimension(30, 50));
panel.add(txtnum1);
JButton button2 = new JButton("Clear");
panel.add(button2);
/* step 4 */
frame.getContentPane().add(panel);
/* step 5 */
frame.setVisible(true);
}
}
Tested and it works in Eclipse.
You should call setVisible(true) at the end, after all components have been added.
Put frame.setVisible(true); at the end and it will work as expected.
Below is a simplified version of my Java program.
It works fine until I add the line
JComboBox comboBox = new JComboBox(options);
After adding that line nothing no longer shows on the window (no buttons, no labels, no colors and so on...).
Can someone please help me figure out what is wrong with that line of code (it shows no syntax error).
import java.awt.*;
import javax.swing.*;
public class JavaApplication23 {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
frame.setTitle("Test program");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(600, 400);
frame.setVisible(true);
JLabel label = new JLabel("Hello");
JButton button = new JButton("Click");
String[] options = new String[] {"Cat", "Dog"};
JComboBox comboBox = new JComboBox(options); //It goes wrong when I add this line
JPanel topPanel = new JPanel();
JPanel centerPanel = new JPanel();
JPanel bottomPanel = new JPanel();
topPanel.add(label);
bottomPanel.add(button);
centerPanel.add(comboBox);
frame.add(topPanel, BorderLayout.PAGE_START);
frame.add(bottomPanel, BorderLayout.PAGE_END);
frame.add(centerPanel, BorderLayout.CENTER);
}
}
You can do 2 things :
Add frame.setVisible(true); at the end or after adding comboBox in your code instead of amid.
or
Add frame.getRootPane().updateUI(); at the end or after adding comboBox in your code.
To add the above code when you are done with adding or changing components in tree is preferred i.e. in your case at the end of your method.
I am expecting that the reason for the problem in you code is clear. But let me know if not.
I'm having an issue with my Java program where I can add a JButton to the panel in JFrame, but when I create an JTextArea object, the JButton disappears?
package sandBox;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class Main {
public static void main(String[] args) {
System.out.println("Hello World");
JFrame frame = new JFrame("Hello world");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600,500);
frame.setLayout(new BorderLayout());
JButton button2 = new JButton("STOP");
JButton button1 = new JButton("GO");
JTextArea text1 = new JTextArea();
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(button1, BorderLayout.SOUTH);
panel.add(button2, BorderLayout.NORTH);
frame.add(panel);
}
}
Remember
BorderLayout will only allow a single component to occupy each of the available positions. Adding another component will cover the previous component
Where possible, always call setVisible after you've created the UI
To actually add all your components, your example doesn't actually add the JTextArea to the container
Someone like...
//...
// frame.setVisible(true);
//...
frame.add(text1);
frame.add(panel, BorderLayout.SOUTH);
frame.setVisible(true);
Might help