Java Panel Not Displaying - java

public class Test extends JFrame
{
private static final long serialVersionUID = 1L;
public static void main(String [] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
MyPanel p = new MyPanel();
p.setVisible(true);
}
});
}
}
The Panel code is dictates how this MyPanel should look.
public class MyPanel extends JPanel
{
private static final long serialVersionUID = 1L;
private JTextField txtUsername;
public MyPanel()
{
setLayout(null);
JPanel panel = new JPanel();
panel.setLayout(null);
panel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
panel.setBackground(SystemColor.control);
panel.setBounds(0, 0, 500, 500);
add(panel);
ImageIcon icon= new ImageIcon("C:/Users/Admin/Desktop/testPic.jpg");
JLabel wlabel = new JLabel(icon);
wlabel.setBounds(20, 10, 400, 222);
panel.add(wlabel);
JPanel panel_1 = new JPanel();
panel_1.setBounds(36, 244, 614, 159);
panel.add(panel_1);
panel_1.setLayout(null);
JLabel lblUsername = new JLabel("Username:");
lblUsername.setBounds(40, 40, 100, 20);
panel_1.add(lblUsername);
lblUsername.setFont(new Font("Tahoma", Font.PLAIN, 18));
txtUsername = new JTextField();
txtUsername.setBounds(179, 52, 195, 30);
panel_1.add(txtUsername);
txtUsername.setColumns(10);
JButton btnSubmit = new JButton("SUBMIT");
btnSubmit.setBounds(424, 65, 145, 44);
panel_1.add(btnSubmit);
btnSubmit.setFont(new Font("Tahoma", Font.PLAIN, 18));
btnSubmit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
}
});
}
}
Why don't I see the actual panel? The code compiles and runs but I don't see anything on my screen.

You've got to add your JPanel to your JFrame. It's the JFrame that is the top level window that displays the entire GUI. Without a top level window either created directly, as described above, or indirectly, such as when you create a JOptionPane, the JPanel will never be seen.
So, rather than this:
public void run()
{
MyPanel p = new MyPanel();
p.setVisible(true);
}
Do this:
public void run()
{
Test test = new Test();
test.setVisible(true);
}
And then create your MyPanel in the Test constructor, and add it to Test there via a call to add(...).
Next we'll talk about why null layouts and setBounds(...) is a very bad thing.
Key tutorial links:
Using Swing Components
Using Top Level Windows
Laying Out Components in a Container

Related

How to switch JPanel from different classes with mouseClicked?

I'm learning javax.swing right now and I'm just trying some things out. But now I'm facing a problem I just can't solve. When I click the JLabel for changing the page (jpanel), nothing happens. It won't remove and show the other jpanel.
I also added a
public final HomeGUI getMainFrame() {
return this;
}
and at the mouseClicked(MouseEvent e)
gui.getMainFrame().removeAll();
and I tried it also with
gui.getMainFrame().mainPanel.removeAll();
My GUI:
public class HomeGUI extends JFrame {
private static final long serialVersionUID = 1L;
JPanel mainPanel = new JPanel(new CardLayout());
final CardLayout cl = new CardLayout();
final JPanel mainPanel = new JPanel(cl);
Panel panel;
Page2 page2;
public HomeGUI() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(1200, 650);
this.setTitle("Terminal");
this.setLocationRelativeTo(null);
this.setLayout(new BorderLayout(1,1));
panel = new Panel();
panel.Inhalt();
page2 = new Page2();
page2.Inhalt();
this.add(mainPanel);
mainPanel.add(panel, "Seite1");
mainPanel.add(page2, "Seite2");
cl.show(mainPanel, "panel");
}
public static void main(String[] args) {
HomeGUI terminal = new HomeGUI();
terminal.setResizable(false);
terminal.setVisible(true);
}
}
the Panel:
class Panel extends JPanel {
private HomeGUI gui;
Page2 page2;
private static final long serialVersionUID = 1L;
JPanel Panel;
JLabel title = new JLabel("Willkommen");
JLabel bgc = new JLabel("");
JLabel menuStrich = new JLabel();
JLabel menuTitle = new JLabel("Menu");
JLabel menuHome = new JLabel("Home");
JLabel menuSeite2 = new JLabel("Seite2");
public void Inhalt() {
this.setBackground(new Color(230, 230, 230));
this.add(title);
this.add(bgc);
this.setLayout(null);
//Seite1
title.setSize(300, 50);
title.setLocation(300, 20);
title.setFont(new Font("Alba Matter", Font.PLAIN, 48));
//Menu
bgc.setLayout(null);
bgc.setOpaque(true);
bgc.setBackground(new Color(66, 78, 245));
bgc.setSize(280, 650);
bgc.setLocation(0, 0);
bgc.add(menuTitle);
bgc.add(menuStrich);
bgc.add(menuHome);
bgc.add(menuSeite2);
menuTitle.setLocation(90, 10);
menuTitle.setSize(100, 50);
menuTitle.setFont(new Font("Bahnschrift", Font.PLAIN, 38));
menuTitle.setForeground(Color.white);
menuStrich.setLocation(10, 55);
menuStrich.setSize(260, 5);
menuStrich.setBackground(new Color(240, 240, 240));
menuStrich.setOpaque(true);
menuHome.setLocation(30, 70);
menuHome.setSize(200, 50);
menuHome.setFont(new Font("Concert One", Font.PLAIN, 36));
menuHome.setForeground(Color.white);
menuSeite2.setLocation(30, 130);
menuSeite2.setSize(200, 50);
menuSeite2.setForeground(Color.LIGHT_GRAY);
menuSeite2.setFont(new Font("Concert One", Font.PLAIN, 32));
menuSeite2.addMouseListener(new menuSeite2Event());
}
private class menuSeite2Event extends MouseAdapter {
#Override
public void mouseClicked(MouseEvent e) {
//show page2
gui.cl.show(gui.mainPanel, "page2");
}
#Override
public void mouseEntered(MouseEvent e) {
menuSeite2.setForeground(Color.CYAN);
}
#Override
public void mouseExited(MouseEvent e) {
menuSeite2.setForeground(Color.LIGHT_GRAY);
}
}
}
class Page2 extends JPanel {
private static final long serialVersionUID = 1L;
JPanel Panel;
JLabel title = new JLabel("Willkommen");
JLabel bgc = new JLabel("");
JLabel menuStrich = new JLabel();
JLabel menuTitle = new JLabel("Menu");
JLabel menuHome = new JLabel("Home");
JLabel menuSeite2 = new JLabel("Seite2");
public void Inhalt() {
this.setBackground(new Color(230, 230, 230));
this.add(title);
this.add(bgc);
this.setLayout(null);
//Seite1
title.setSize(300, 50);
title.setLocation(300, 20);
title.setFont(new Font("Alba Matter", Font.PLAIN, 48));
//Menu
bgc.setLayout(null);
bgc.setOpaque(true);
bgc.setBackground(new Color(66, 78, 245));
bgc.setSize(280, 650);
bgc.setLocation(0, 0);
bgc.add(menuTitle);
bgc.add(menuStrich);
bgc.add(menuHome);
bgc.add(menuSeite2);
menuTitle.setLocation(90, 10);
menuTitle.setSize(100, 50);
menuTitle.setFont(new Font("Bahnschrift", Font.PLAIN, 38));
menuTitle.setForeground(Color.white);
menuStrich.setLocation(10, 55);
menuStrich.setSize(260, 5);
menuStrich.setBackground(new Color(240, 240, 240));
menuStrich.setOpaque(true);
menuHome.setLocation(30, 70);
menuHome.setSize(200, 50);
menuHome.setFont(new Font("Concert One", Font.PLAIN, 36));
menuHome.setForeground(Color.white);
menuHome.addMouseListener(new menuHomeEvent());
menuSeite2.setLocation(30, 130);
menuSeite2.setSize(200, 50);
menuSeite2.setForeground(Color.white);
menuSeite2.setFont(new Font("Concert One", Font.PLAIN, 32));
}
private class menuHomeEvent extends MouseAdapter {
#Override
public void mouseClicked(MouseEvent e) {
//show Home
}
#Override
public void mouseEntered(MouseEvent e) {
menuHome.setForeground(Color.CYAN);
}
#Override
public void mouseExited(MouseEvent e) {
menuHome.setForeground(Color.LIGHT_GRAY);
}
}
}
INFO: all classes are in 1 file.

Change the properties of a JPanel component through an action event

I'm trying to setup a system where when I press a button the JLabel text will change, but I can't seem to make it work. I've already tested that the action listener works by doing 'system.out.println("test");'. It works fine, but when trying to change a JComponent text it doesn't work. I've already searched for answers and found nothing that works.
Main class:
package com.fcs.app;
public class A {
public static void main(String args[]) {
window w = new window();
w.drawWindow();
}
}
JFrame and JPanel class:
package com.fcs.app;
import java.awt.*;
import javax.swing.*;
public class window extends JPanel {
JFrame jf = new JFrame();
JPanel jp = new JPanel();
JButton b1 = new JButton();
JTextField tf1 = new JTextField();
JTextField tf2 = new JTextField();
JLabel plus = new JLabel();
JLabel equals = new JLabel();
JLabel rt = new JLabel();
int Result = 10;
public void drawWindow() {
//JFrame setup
jf.setSize(400, 400);
jf.setUndecorated(true);
jf.setLayout(null);
jf.setContentPane(jp);
jf.setLocation(100, 100);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
//JPanel setup
jp.setSize(400, 400);
jp.setLocation(0, 0);
jp.setBackground(Color.WHITE);
jp.add(b1);
jp.add(tf1);
jp.add(tf2);
jp.add(plus);
jp.add(equals);
jp.add(rt);
jp.setLayout(null);
jp.setVisible(true);
//JButton setup
b1.setFont(new Font("Times", Font.PLAIN, 15));
b1.setText("Calculate!");
b1.setSize(100, 40);
b1.setLocation(150, 350);
b1.addActionListener(new Listener());
b1.setVisible(true);
//TextField 1 setup
tf1.setSize(120, 50);
tf1.setLocation(140, 20);
tf1.setFont(new Font("Times", Font.PLAIN, 25));
tf1.setHorizontalAlignment(JTextField.CENTER);
tf1.setVisible(true);
//TextField 2 setup
tf2.setSize(120, 50);
tf2.setLocation(140, 120);
tf2.setFont(new Font("Times", Font.PLAIN, 25));
tf2.setHorizontalAlignment(JTextField.CENTER);
tf2.setVisible(true);
//Plus sign Setup
plus.setSize(120, 50);
plus.setLocation(140, 70);
plus.setHorizontalAlignment(JLabel.CENTER);
plus.setFont(new Font("Times", Font.PLAIN, 40));
plus.setText("+");
plus.setVisible(true);
//Equals sign Setup
equals.setSize(120, 50);
equals.setLocation(140, 170);
equals.setHorizontalAlignment(JLabel.CENTER);
equals.setFont(new Font("Times", Font.PLAIN, 40));
equals.setText("=");
equals.setVisible(true);
//Result text
rt.setSize(400, 50);
rt.setLocation(0, 250);
rt.setHorizontalAlignment(JLabel.CENTER);
rt.setFont(new Font("Times", Font.PLAIN, 60));
rt.setText("");
rt.setVisible(true);
}
}
ActionListener class:
package com.fcs.app;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Listener implements ActionListener {
window w = new window();
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
w.rt.setText("Test");
}
}
You are creating new references of Window like window w = new window();
it will create a new instance of window and you are trying to change newly created window.
Try to pass the window object you have created before in window class or
implement an anonymous ActionListener in the window class.
b1.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
rt.setText("Test");
}
}
});

Java - Unable to open a new frame from ActionListener

I am currently designing a login screen, but I ran into a strange issue. I already designed my GUI with the help of swing, and it was time to make functional buttons. I wanted to test my login button and if it would take me to the frame I want, but it is unable to. I can set a JOptionPane.showMessageDialog for example, which works just fine, but I am unable to open another frame from the button. I tried with New JFrameName().setVisible(true), and also JFrameName test = new JFrameName(); test.setVisible(true);, but the methods show up in red. Here is my code.
package com.edu4java.swing.tutrial3;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class LoginView {
public static void main(String[] args) {
JFrame frame = new JFrame("Bus Tour Booking System");
frame.setSize(300, 200);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.add(panel);
placeComponents(panel);
frame.setVisible(true);
}
private static void placeComponents(JPanel panel) {
panel.setLayout(null);
JLabel titleLabel = new JLabel("Bus Tour Booking System");
titleLabel.setBounds(70,15,150,25);
panel.add(titleLabel);
JLabel userLabel = new JLabel("Username: ");
userLabel.setBounds(30, 50, 80, 25);
panel.add(userLabel);
JTextField userText = new JTextField(20);
userText.setBounds(120, 50, 130, 25);
panel.add(userText);
JLabel passwordLabel = new JLabel("Password: ");
passwordLabel.setBounds(30, 80, 80, 25);
panel.add(passwordLabel);
JPasswordField passwordText = new JPasswordField(20);
passwordText.setBounds(120, 80, 130, 25);
panel.add(passwordText);
JButton loginButton = new JButton("login");
loginButton.setBounds(100, 125, 80, 25);
panel.add(loginButton);
ActionListener myButtonListener = new MyButtonListener();
loginButton.addActionListener(myButtonListener);
}
private static class MyButtonListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
//can't access a new frame from here :(
}
}
}
I would be very grateful if someone could help me out, I read a lot on Stackoverflow and Reddit, but just can't find the solution. I am also new to Java, so that doesn't help a lot either :D. Thanks in advance!
P.S. As far as the actual functionality for the login screen, I am going to do that in a later stage.
This is your login class. I put the JFrame frame in the global scope, so you can manipulate it from the ButtonListener method. I also created a SomeFrame class, just to demonstrate the new JFrame that would be created when you click the button. When an action is performed(the button is clicked) a new object of SomeFrame is created. Since SomeFrame extends JFrame we can use the method setVisible() to a SomeFrame object. The SomeFrame frame appears and the LoginView is no longer visible.
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class LoginView {
public static JFrame frame = new JFrame("Bus Tour Booking System");
public static void main(String[] args) {
frame.setSize(300, 200);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.add(panel);
placeComponents(panel);
frame.setVisible(true);
}
private static void placeComponents(JPanel panel) {
panel.setLayout(null);
JLabel titleLabel = new JLabel("Bus Tour Booking System");
titleLabel.setBounds(70,15,150,25);
panel.add(titleLabel);
JLabel userLabel = new JLabel("Username: ");
userLabel.setBounds(30, 50, 80, 25);
panel.add(userLabel);
JTextField userText = new JTextField(20);
userText.setBounds(120, 50, 130, 25);
panel.add(userText);
JLabel passwordLabel = new JLabel("Password: ");
passwordLabel.setBounds(30, 80, 80, 25);
panel.add(passwordLabel);
JPasswordField passwordText = new JPasswordField(20);
passwordText.setBounds(120, 80, 130, 25);
panel.add(passwordText);
JButton loginButton = new JButton("login");
loginButton.setBounds(100, 125, 80, 25);
panel.add(loginButton);
ActionListener myButtonListener = new MyButtonListener();
loginButton.addActionListener(myButtonListener);
}
private static class MyButtonListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
SomeFrame newFrame = new SomeFrame();
newFrame.setVisible(true);
frame.setVisible(false);
}
}
}
This is the SomeFrame class.
import javax.swing.JFrame;
import javax.swing.JPanel;
public class SomeFrame extends JFrame {
public SomeFrame(){
super("something");
this.setSize(300, 200);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
this.add(panel);
this.setVisible(true);
}
}

Scrolling Text Pane JFrame Java

I'm trying to create a sort of log of all the keys hit, at the moment I just need to figure out how to either:
Link the position of the "text" to the scroll bar to the right
OR
Add a different component which is suited better to hold large amounts of multiple line text.
What am I doing wrong here? Thanks!
public class MacroMakerGui extends JFrame {
public static final long serialVersionUID = 1L;
public static JPanel contentPane;
public static JTextField textField = new JTextField();;
public static MacroKeyListener keylistener = new MacroKeyListener(textField);
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MacroMakerGui frame = new MacroMakerGui();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public MacroMakerGui() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 126, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(null);
setContentPane(contentPane);
JButton btnNewButton = new JButton("Record Macro");
btnNewButton.setBounds(10, 220, 99, 30);
contentPane.add(btnNewButton, null);
textField.setBounds(10, 189, 99, 20);
contentPane.add(textField);
textField.setColumns(10);
JEditorPane editorPane = new JEditorPane();
editorPane.setBounds(10, 11, 84, 153);
contentPane.add(editorPane);
JScrollBar scrollBar = new JScrollBar();
scrollBar.setBounds(93, 11, 17, 153);
contentPane.add(scrollBar);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
btnNewButton.addKeyListener(keylistener);
}
});
}
}
Instead of JScrollBar, use JScrollPanel. Add that to the contentPane, and add your editorPane as a chiled of the JScrollPanel.

Adding components by constructor

I have problem with adding Label by constructor, when I make it by method it's no problem
private void addLabel() {
System.out.println("asd");
JLabel label = new JLabel("asd");
label.setBounds(10, 40, 100, 25);
add(label);
repaint();
validate();
System.out.println("asd2");
}
But when i try to do this same by new class and constructor i doesn't work...
Main frame:
public class Frame extends JFrame {
JButton button = new JButton("new");
AddButton button2 = new AddButton();
public Frame() {
setLayout(null);
setSize(400, 500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
button.setBounds(40, 10, 50, 25);
add(button);
button2.setBounds(40, 40, 100, 25);
add(button);
}
public static void main(String[] args) {
Frame ap = new Frame();
ap.setVisible(true);
}
AddButton class:
public class AddButton extends JPanel {
JLabel label = new JLabel("asd");
public AddButton() {
label.setBounds(10, 40, 100, 25);
add(label);
repaint();
validate();
}
}
Ok i got it, I tried to add "button" two times istead button and button2 :D
Your constructor doesn't make sense, that's not how you should use constructors - constructors are used to create an instance of a class.
When you write
AddButton button2 = new AddButton();
then button2 is of type AddButton, and add doesn't accept this type of object.
You can Edit like this
public class AddButton extends JPanel {
JLabel label;
public AddButton() {
label=new JLabel("asd");
label.setBounds(10, 40, 100, 25);
add(label);
repaint();
validate();
}
}

Categories