Just imported my image, Need a sub panel on the right - java

Just imported an image, but need a subPanel on the right with the dimensions (200,700) I've tried using the imported image as a panel but it just spans me with errors, any ideas?
package dodge;
import java.awt.*;
import javax.swing.*;
public class Dodge extends JFrame {
private ImageIcon image;
private JLabel label;
Dodge(){
JFrame frame = new JFrame();
frame.setResizable(false);
frame.pack();
setLayout(new FlowLayout());
JPanel image = new JPanel();
image = new ImageIcon(getClass().getResource("Road.jpg"));
label = new JLabel (image);
add(label);
}
public static void main(String[] args) {
//
JFrame frame = new JFrame();
frame.setResizable(false);
frame.pack();
Dodge gui = new Dodge();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setVisible(true);
gui.pack();
gui.setTitle("Dodge The Cars");
JPanel subPanel1= new JPanel();
subPanel1.setBackground(Color.DARK_GRAY);
subPanel1.setPreferredSize(new Dimension (250,700));
JLabel label = new JLabel ("Menu");
subPanel1.add(label);

You have 3 JFrames in your code. You create a frame in the main method. Then you create a Dodge class which is a JFrame. Finally in the constructor of the Dodge class you create another frame.
I suggest your read the Swing tutorial on How to Use Icons for working examples that will show you how to better structure your program. Then it should be easier to solve your problem.

Related

JPanel doesn't add button and text field

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.

pack() method of JFrame doesn't work (java,ubuntu)

i'm new in java .
i just learn JPanel and JFrame.
i got this note from java software solutions:
" The pack method of the frame sets its size appropriately based on
its contents—in this case the frame is sized to accommodate the size
of the panel it contains."
so i wrote this code :
public static void main (String [] args){
JFrame frame = new JFrame("test");
JPanel panel = new JPanel();
JLabel label1= new JLabel("");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
//frame.setSize(1000, 500);
frame.getContentPane().add(panel);
Color darkBlue = new Color(8,40,94);
panel.setSize(1000, 500);
panel.setBackground(darkBlue);
}
but it the result is a really tiny window that i should maximize it with mouse to see the content
but when i set frame size every thing work great!
and i use Ubuntu.
so what's the reason of this problem?
From the order of your code:
JFrame frame = new JFrame("test");
JPanel panel = new JPanel();
JLabel label1= new JLabel("");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
You did not add anything into the frame before you pack() it. pack() means let the frame decide its size based on the components being added to it.
Since you have no components added to it before you pack() it, you receive a small window with visually nothing inside (until you resize the window).
When the frame is being resized, paintManager will be consulted to paint the contentPane, hence if you add before pack(), not only the frame will be resized nicely for you, the components within it will be painted as well.
To see the components within the JFrame:
public static void main (String [] args){
JFrame frame = new JFrame("test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JLabel label1= new JLabel("");
panel.add(label1); //Add label to panel
frame.add(panel); //Add panel (with label) to frame
frame.pack(); //Let the frame adjust its size based on the added components
frame.setVisible(true);
}
public static void main (String [] args){
JFrame frame = new JFrame("test");
JPanel panel = new JPanel();
JLabel label1= new JLabel("");
Color darkBlue = new Color(8,40,94);
panel.setPreferredSize(new Dimension(1000, 500));
panel.setBackground(darkBlue);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setSize(1000, 500);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
You should use pack() after setting the sizes.
Furthermore panel.setPreferredSize() works better than setSize() for you :)
call jframe.pack() before jframe.setVisible() method !
public static void main (String [] args){
JFrame frame = new JFrame("test");
JPanel panel = new JPanel();
JLabel label1= new JLabel("");
panel.setSize(1000, 500);
frame.getContentPane().add(panel);
Color darkBlue = new Color(8,40,94);
panel.setBackground(darkBlue);
frame.pack() ;
frame.setVisible(true); }
You will also need to check the default layout of JFrame , which is flow layout !

How To Have A TabPane For A ScrollPane With A TextPane in Java

I'm creating a notepad program in Java and would like to have different tabs to for each document opened. I'm having trouble getting the tabs to be displayed. This is my test document so far that I have looked over here at first and I modified it for the text document. How to add a JScrollPane onto a JTabbedPane using a null layout?.
This is what I currently have:
import java.awt.*;
import javax.swing.*;
public class Test extends JFrame {
private static void CreateAndShowGui() {
JFrame frame = new JFrame("Program");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextPane txt = new JTextPane();
JPanel noWrapPanel = new JPanel(new BorderLayout());
noWrapPanel.add(txt);
JScrollPane scroll = new JScrollPane(noWrapPanel);
JPanel topPanel = new JPanel();
topPanel.setLayout(new BorderLayout());
frame.add(topPanel);
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(scroll, BorderLayout.CENTER);
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("Welcome", panel);
topPanel.add(tabbedPane);
frame.add(scroll);
frame.setSize(400, 200);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
UIManager.put("swing.boldmetal", Boolean.FALSE);
CreateAndShowGui();
}
});
}
}
What am I doing wrong? :(
you are adding scroll pane to frame .
frame.add(scroll);
but you should add scroll pane to jpanel and add pane to scroll pane.you have done that part.
so remove this incorrect line.
frame.add(scroll);
this is complete code without using extends jframe .
this is complete code using extends jframe
note: you have extends your class by jframe class but you are creating a new frame .you don't need to create a frame variable .you can either remove extends part or directly use your class as a jframe without creating a new frame.

Adding Panel to Panel

I anted to add a JPanel to my already existing JPanel so I could have a small window with a JTextField on top with a name and a scrollable JTextArea below it with some description. I made a class that extends JPanel with the following constructor:
import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import java.awt.*;
public class LocationWindow extends JPanel {
public JTextField name;
public JTextArea desc;
public JScrollPane scroll;
public LocationWindow(){
super();
setBorder (new TitledBorder(new EtchedBorder(), "Display Area"));
setLayout(new BorderLayout());
setVisible(true);
setBounds(30, 40, 700, 290);
name = new JTextField(10);
name.setText("name");
desc = new JTextArea(5,10);
scroll = new JScrollPane(desc);
scroll.setVerticalScrollBarPolicy ( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS );
desc.setEditable (true);
desc.setLineWrap(true);
desc.setText("random text");
add(name);
add(desc);
add(scroll);
validate();
}
}
It almost works, as it gives me the window with the borders and a scroll, but both the JTextField and JTextArea are missing.
As you are using BorderLayout for the JPanel,
setLayout(new BorderLayout());
the components will always be added to the center if you dont specify the position. add(scroll); is same as add(scroll,BorderLayout.CENTER); as you're adding all via add the last added component only be visible.Refer this as well
The next is you are adding JTextArea seperately so it will be removed from ScrollPane.Just add scrollpane to Panel no need to add all components.[Add the parent component alone]
add(name,BorderLayout.NORTH);
//add(desc);Noo need to add desc as it is already added in JScrollPane
add(scroll,BorderLayout.CENTER);
There is no need for setVisible for JPanel.JPanel needs to be embedded in Container like JFrame to be visible
//setVisible(true);Wont do anything
So call like this
JFrame frame = new JFrame();
frame.add(new LocationWindow());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
You can use below code to adding panel to panel.
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
LocationWindow loc = new LocationWindow();
frame.add(loc);
frame.setSize(300, 200);
frame.setVisible(true);
}

Java buttons not showing up on JFrame

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

Categories

Resources