doing some self study for a simple GUI interface for Java. Tried to code a simple interface. Here is the below code:
public void MainPanel() {
JFrame frame = new JFrame();
frame.setTitle("Title");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTabbedPane mpt = new JTabbedPane();
mpt.addTab("Intro", new IntroPanel());
mpt.addTab("Catalogue", new CataloguePanel());
mpt.addTab("Order", new OrderPanel());
mpt.addTab("Track", new TrackPanel());
JPanel main = new JPanel();
main.setBackground(Color.white);
JLabel label1 = new JLabel("Intro");
main.add(label1);
frame.add(main);
frame.add(mpt);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
The problem I am currently facing is: If I put frame.add(main) infront of frame.add(mpt), only the tabs will appear but the the label intro. If I put frame.add(mpt) infront of frame.add(main). Intro will appear but not the tabs.
Why is this happening? Why are they overlapping each other? I did it the same way as some tutorial but to no avail..
Try giving it some other layout.
eg. frame.setLayout(new FlowLayout());
Info:
According to Java Naming Conventions your method's name should be like public void mainPanel()
Related
I was trying to write a program, where you put some numbers in a JTextField an then it does something with it. I tried figuring out how to even make an input possible. But the way I'm trying it doesn't work despite Eclipse showing no errors. And yes, I know there is no way to stop this program but this is just a test.
import javax.swing.*;
public class NotenEingabe extends JFrame {
public static void main(String[]args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JLabel label = new JLabel("text");
JTextField field = new JTextField("text");
panel.add(label);
panel.add(field);
frame.setTitle("Grade input");
frame.pack();
frame.add(panel);
frame.setVisible(true);
}
}
I hope it is a real problem and not simply my tiredness.
frame.setVisible(true);
frame.add(panel);
Components should be added to the frame BEFORE the frame is made visible.
The layout manager is not invoked so the components have a size of (0, 0) which means there is nothing to paint.
frame.setTitle("Grade input");
frame.add(panel);
//frame.setSize(700, 700);
frame.pack();
frame.setVisible(true);
First of all when you extend your class from jframe you dont need to instantiate new jFrame just make a new instance of your class then access jframe with that or just delete extend Jframe and use functional approach.
The you didn't set de layout of pane
Test this one then you can choose from veriety of layouts
panel.setLayout(newFlowLayout());
I have a program that opens up a Jframe. inside this Jframe, I have a button that opens another class that has a Jframe too. Is there any way to to get the new class, and new gui opened in the same frame as the first one, and remove the existing content in the first frame?
This is the first class:
public class FirstFrame {
public FirstFrame() {
JFrame frame = new JFrame();
frame.setTitle("Title");
frame.setLayout(new BorderLayout());
frame.setSize(500,350);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Code for a button, that has an actionlistener wich opens the next
//class
frame.setVisible(true);
frame.setResizable(false);
}
public static void main(String[] args){
new FirstFrame();
}
}
The second class is almost like this one. The difference between the two frames is that they have different content.
When I run my program, everything works just fine, but I get two windows. Also, it says in the task manager that the two frames are running as two different programs.
Is there a way to make the content and even the name of the frame change, instead of having to deal with two different frames?
You can change the content pane of your JFrame. Let's say you have two JComponents called a and b. You initialize your frame:
JFrame frame = new JFrame();
frame.setContentPane(a);
Then you can just call
frame.setContentPane(b);
to change the content. You may also need to call revalidate and repaint to refresh the frame.
In the below example, pressing the button changes the content of JFrame from single JButton a to JLabel b. You can use any JComponent instead, such as JPanel containing multiple JComponents.
public static void main(String[] args) {
final JFrame frame = new JFrame();
frame.setTitle("Title");
frame.setSize(500, 350);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton a = new JButton("button A");
frame.setContentPane(a);
frame.setVisible(true); // calling setVisible after content pane has been set to refresh a frame
a.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JComponent b = new JLabel("label B");
frame.setContentPane(b);
frame.revalidate();
}
});
}
This programs take in a string for a parameter. Currently filled with:
"http://localhost/media/svu.mp4"
I have made sure the URL exists.
I am using the VLCj library to create the mediaPlayerComponent (which is placed inside the container (JPanel mainPanel) ). The component mainPanel is then placed inside the JLayeredPanel layers. On top of that, I place a clear (non-opaque) layer (JPanel glassPane). According to everything I've read, this should be working and Eclipse isn't showing any errors or warnings.
The stack trace is as follows:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at client.test.Client.<init>(Client.java:62)
at client.test.Client$1.run(Client.java:44)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
....
The code is below. Line 62 is marked with a comment. The JPanels and JLayeredPanel, as well as the windowDimensions are all created as static objects above the main method in my code.
Any and all help is greatly appreciated.
static JLayeredPane layers = new JLayeredPane();
static JPanel mainPanel, glassPane = new JPanel();
public Client(String toPlay) {
JFrame frame = new JFrame("Client");
mediaPlayerComponent = new EmbeddedMediaPlayerComponent();
MediaPlayer mediaPlayer= mediaPlayerComponent.getMediaPlayer();
frame.setSize(windowDimensions[0], windowDimensions[1]);
frame.setLayout(new BorderLayout());
frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(layers, BorderLayout.CENTER);
layers.setBounds(0,0,windowDimensions[0], windowDimensions[1]);
mainPanel.setBackground(Color.black); /* This is line 62 */
mainPanel.setBounds(0,0,windowDimensions[0], windowDimensions[1]);
mainPanel.setOpaque(true);
mainPanel.add(mediaPlayerComponent);
glassPane.setBackground(Color.white);
glassPane.setBounds(0,0,windowDimensions[0], windowDimensions[1]);
glassPane.setOpaque(false);
layers.add(mainPanel, new Integer(0), 0);
layers.add(glassPane, new Integer(1), 0);
frame.setVisible(true);
mediaPlayer.playMedia(toPlay);
}
mainPanel has not been intitialized, even if it looks like it has. You have this code:
static JPanel mainPanel, glassPane = new JPanel();
This only inititalzes glassPane. In order to initialize mainPanel you have to change your code to this:
static JPanel mainPanel = new JPanel(), glassPane = new JPanel();
You haven't initialized mainPanel. Try adding mainPanel = new JPanel(); above the error line.
You'll also need to call frame.add(mainPanel); after you initialize the panel.
I just want to make a JFrame that will say "Hello world", nothing big, no interaction needed. How do I do this?
I can create the JFrame, however I do not know how to put a JPanel with simple text in it.
Here is what I got so far
JFrame frame = new JFrame("Relief Valve");
frame.setResizable(false);
frame.setLocation(500,300);
JPanel p1 = new JPanel();
frame.setVisible(true);
Instead of creating the JPanel, try:
JLabel label = new JLabel("this is my text");
frame.add(label);
frame.pack();
JFrame window = new JFrame("Hello World App");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new BorderLayout());
window.add(new JLabel("Hello World"), BorderLayout.CENTER);
window.pack();
window.setVisible(true);
window.setLocationRelativeTo(null);
I'm currently on a mobile device but I'll be happy to document that when I get on a computer, feel free to ask any questions though.
You need to create a JLabel (witch is from Swing library) the code for that is:
JLabel label = new JLabel("Hello world");
if u want to set it to a specific loaction you need to create a render method :
public void render(Graphics g){
g.drawString(label,x,y);
}
the x and y is the position of the string with your label.
In order to create a JFrame with a simple text, you have to create a label and attach it to your frame.
Let's assume you have a JFrame created:
JFrame myFrame = new JFrame("My Frame");
Let's create the text label:
JLabel myLabel = new JLabel("Text");
To change a text of already created label:
myLabel.setText("New Text");
And eventually to clear the label:
myLabel.setText("");
Let's connect all the dots:
myFrame.add(myLabel, BorderLayout.CENTER);
myFrame.pack();
To learn more about JFrames, check: https://javatutorial.net/swing-jframe-basics-create-jframe
Alright so here's the problem:
I created this small guessing game and wanted to make it gui based...
but it appears in two different windows -
first window is the menu(button and label)
second window is activated by the button and has the game in it.
Is there a way for me to have both windows in one- AS IN the first window gets overwritten by the second window?
public class Skeleton extends JFrame implements ActionListener
{
JPanel glass = new JPanel();
JButton btn = new JButton("Start");
TextField tf = new TextField();
JLabel label = new JLabel("Enter Guess Here: ");
JLabel answerLabel = new JLabel("Answer:...");
Board bob =new Board();
public Skeleton()
{
setIconImage(new ImageIcon("icon.png").getImage());
getContentPane().setBackground(Color.darkGray);
getContentPane().setForeground(Color.black);
setLayout(new FlowLayout(FlowLayout.CENTER,10,10));
add(label);
label.setForeground(Color.black);
add(tf);
tf.setText("");
add(btn);
btn.setBackground(Color.green);
btn.addActionListener(this);
add(answerLabel);
answerLabel.setForeground(Color.black);
setTitle("Guessing Game");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(300,200);
setLocationRelativeTo(null);
setVisible(true);
setResizable(true);
}
public void actionPerformed(ActionEvent e)
{//GAME FRAME
JFrame frame = new JFrame();
int userGuess= Integer.parseInt(tf.getText());
frame.add(bob);
frame.setVisible(true);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setSize(300,285);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setIconImage(new ImageIcon("icon.png").getImage());
...........
This is obviously not the full code, just the part that uses the GUI.
NOTE: I do have some other within this actionevent which occur after the button is clicked.
Also this is my first game ever :D (if i can call it one)
You create new JFrame into "actionPerformed" method, so it's normal to have two frames (the Skeleton instance and the new one).
Just use "this" instead of a new JFrame instance to keep only one JFrame instance.
Each JFrame will create a new window. If you want to display everything into the same window, create only one JFrame and change its content.
You can change the whole content of a JFrame by setting its contentPane (frame.setContentPane(...)) or by adding/removing Panel to it.
I suggest to take a look at JPanel since you will use it a lot.