Java Button not appearing on (GUI) - java

I've been figuring out a nice way to lay nice foundations when working on a somewhat bigger project than I have before. If i write everything in main it works fine. When doing classes like this the Frame works but the Button i've added doesn't wanna appear:
//main
package taxsystem;
import java.awt.*;
import javax.swing.*;
public class Taxmain
{
public mainFrame mf;
public Interface gui;
public void startApplication()
{
mf = new mainFrame();
mf.startApp();
gui = new Interface();
gui.makeLayout();
}
public static void main(String[] args)
{
Taxmain tm = new Taxmain();
tm.startApplication();
}
}
//The actual Frame
package taxsystem;
import java.awt.*;
import javax.swing.*;
public class mainFrame extends JFrame
{
public void startApp()
{
setResizable(false);
setVisible(true);
setSize(720,340);
setLocation(0,0);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setBackground(Color.WHITE);
setTitle("Tax Handler");
}
}
//the layout (where i create the button that doesn't appear)
package taxsystem;
import java.awt.*;
import javax.swing.*;
public class Interface extends JPanel
{
Taxmain mc;
public JButton testButton;
public void makeLayout()
{
testButton = new JButton();
testButton.setText("Printer");
testButton.setFont(new Font("verdana", Font.ITALIC, 16));
testButton.setForeground(Color.BLACK);
testButton.setFocusable(false);
testButton.setSize(new Dimension(150, 40));
testButton.setLocation(10, 10);
this.setLayout(null);
this.add(testButton);
}
}
Currently it looks like this: https://gyazo.com/fad5dbca6c59905faea0a8ac1fbd424a
Thanks in advance, also are there anyway i can improve the code i have so far?

You need to add the JPanel to your JFrame
public void startApplication()
{
mf = new mainFrame();
mf.startApp();
gui = new Interface();
gui.makeLayout();
mf.add(gui); // here is get's added
}

Related

JFrame window not popping up?

I'm pretty new to java, and I'm trying to make a simple JFrame application, although when I click run, nothing displays. I have followed some youtube tutorials, and i've pretty much copied and pasted this code, although i'm not sure why it works for the youtube demonstration, and not mine. Some help would be greatly appreciated.
Thanks!
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Game extends JFrame{
private JButton clicker;
private int clicks = 0;
public Game(){
createView();
setTitle("Game");
setSize(400, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
setResizable(true);
}
private void createView(){
JPanel panel = new JPanel();
getContentPane().add(panel);
clicker = new JButton("Here's a jbutton");
clicker.addActionListener(new Button());
panel.add(clicker);
}
public static void main(String[] args) {
new Game().setVisible(true);
}
private class Button implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
clicks += 1;
}
}
}

repaint() method does not paint new frame

I have looked at a lot of answers but i still cannot find a solution. I have a JFrame and two JPanels. I want to remove the one panel and replace it with the second when a button is pressed, but the repaint() method does not refresh the frame. Please help.
Here is my code for the frame:
import javax.swing.*;
import java.awt.*;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
public class MainFrame
{
static JFrame mainFrame;
int height = 650;
int width = 1042;
public MainFrame()
{
mainFrame = new JFrame();
mainFrame.setBounds(0, 0, width, height);
mainFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
mainFrame.setResizable(false);
}
public static void main(String[] args)
{
new MainMenu();
mainFrame.setVisible(true);
}
}
This is the code for my MainMenu panel
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import static java.awt.Color.CYAN;
import static java.awt.Color.red;
public class MainMenu extends MainFrame
{
public MainMenu()
{
components();
}
//variable decleration
JPanel menuPanel;
JLabel title;
JButton periodicTable;
private void components()
{
int buttonW = 500;
int buttonH = 50;
//creating panel
menuPanel = new JPanel();
menuPanel.setLayout(null);
menuPanel.setBackground(CYAN);
//creating title label
title = new JLabel("Application Title", SwingConstants.CENTER);
title.setFont(new Font("Calibri Body", 0, 50));
title.setBounds(width / 3 - buttonW / 2, 50, buttonW, buttonH + 10);
//creating periodic table button
periodicTable = new JButton();
periodicTable.setText("Periodic Table");
periodicTable.setBounds(width / 3 - buttonW / 2, 50 + buttonH + 60, buttonW, buttonH);
periodicTable.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
periodicTableActionPerformed(event);
}
});
//adding components to panel
menuPanel.add(title);
menuPanel.add(periodicTable);
//adding panel to MainFrame
mainFrame.add(menuPanel);
}
private void periodicTableActionPerformed(ActionEvent event)
{
mainFrame.remove(menuPanel);
mainFrame.repaint();
new PeriodicTable();
mainFrame.repaint();
}
}
And finally my PeriodicTable panel
import javax.swing.*;
import java.awt.*;
public class PeriodicTable extends MainFrame
{
public PeriodicTable()
{
periodicComponents();
}
JPanel ptPanel;
private void periodicComponents()
{
ptPanel = new JPanel();
ptPanel.setLayout(null);
ptPanel.setBackground(Color.RED);
mainFrame.add(ptPanel);
}
}
I have no idea why you are extending MainFrame. Looks unnecessary to me.
I want to remove the one panel and replace it with the second when a button is pressed
Then use a CardLayout. Read the section from the Swing tutorial on How to Use CardLayout for a working example.
The tutorial will show you how to better structure your code.
Your PeriodicTable extends MainFrame. When creating new PeriodicTable you create with it new MainFrame which has its own instance of JFrame (MainFrame.mainFrame). You need to add that panel to existing mainFrame in MainMenu
I suggest removing changing your PeriodicTable class like this:
import javax.swing.*;
import java.awt.*;
public class PeriodicTable extends JPanel // Not MainFrame, but new custom panel
{
public PeriodicTable()
{
periodicComponents();
}
private void periodicComponents()
{
// You don't need ptPanel anymore, because `this` is JPanel
setLayout(null);
setBackground(Color.RED);
}
}
and change your actionPerformed function to something like this:
private void periodicTableActionPerformed(ActionEvent event)
{
mainFrame.remove(menuPanel); // Remove old panel
mainFrame.add(new PeriodicTable()); // Create and add to existing mainFrame
mainFrame.repaint(); // Just one repaint at the end
// I think it will work even without repaint, because add and remove should schedule repainting as well
}

Stack overflow error GUI programming

I have a big problem i tried to solve it for days. I programmed a little program but it doesn't work.The error is Stackoverflow I already searched this website on and on again .I broke it down to the part wich doesn't works so here is the code.
This is the frame:
package snippet;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class MyFrame extends JFrame {
JButton button;
JLabel label;
TextEdit textEdit = new TextEdit();
public void LetsGo() {
setBounds(0, 0, 800, 510);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Game");
setResizable(false);
setLocationRelativeTo(null);
//Labels
label = new JLabel();
label.setText("Change Me");
label.setBounds(30, 25, 200, 50);
label.setVisible(true);
add(label);
button = new JButton();
button.setText("I Will Change A Text");
button.setBounds(30, 130, 200, 400);
button.addActionListener(new Listener());;
add(button);
}
public class Listener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
textEdit.editTheText();
}
}
And this object should edit the Text :
package snippet;
public class TextEdit {
MyFrame frame = new MyFrame();
public void editTheText(){
frame.label.setText("Text was edited");
}
}
So the real code is much more complex so i won't put all in one Object
Would be great if i receive some help would be very very thankful for that
You are creating a new MyFrame in TextEdit, which I don't think is what you want to do because frame.label will be null.
What you really should be doing is assigning the JFrame inside of Listener.
public class Listener implements ActionListener {
private JFrame frame;
public Listener(JFrame frame) {
this.frame = frame;
}
#Override
public void actionPerformed(ActionEvent e) {
if (this.frame.label != null) {
this.frame.label.setText("Text was edited");
}
}
}
Then for the the other code, you don't have a constructor or your actual class is called LetsGo?
Assuming it is not called LetsGo and it actually is MyFrame, you need an actual constructor.
public MyFrame() {
LetsGo();
}
Then in the LetsGo method, add the frame to the Listener
button.addActionListener(new Listener(this));

JFrame is empty

I'm working on a Java HW and faced this problem. Even though everything seems to be coded correctly, I'm getting blank frame in the end. I'm guessing it has something to do with this part in the driver program:
frame.getContentPane().add(new RandomPanel());
Here is my main program:
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class RandomPanel extends JPanel
{
private JButton randButton;
private JLabel label;
public void NamePanel()
{
JPanel primary = new JPanel();
randButton = new JButton("Whats my name?");
ButtonListener listener = new ButtonListener();
randButton.addActionListener(listener);
label = new JLabel("Displaying random number");
setBackground(Color.pink);
add(label);
add(randButton);
}
class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
label.setText( new Integer(new Random().nextInt(100) + 1).toString() );
}
}
}
And driver program:
import javax.swing.JFrame;
public class RandomPick
{
public static void main (String[] args)
{
JFrame frame = new JFrame("RandomPick");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new RandomPanel());
frame.pack();
frame.setVisible(true);
}
}
You need to invoke namePanel to add the components to the frame.
RandomPanel randomPanel = new RandomPanel();
randomPanel.namePanel();
frame.add(randomPanel);
but you may have intended to use the method as a constructor
public RandomPanel() {
which would mean that the method call would not be necessary

Use variables from other class from other file to another in java

There are two players who will input their name in the JTextFields. What I want to do is that the data I entered from the Welcome frame in Enter.java will be transferred to the JLabels in ActualGame.java.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
public class Enter extends JFrame implements ActionListener {
private String one = "";
private String two = "";
private JTextField txtOne = new JTextField();
private JTextField txtTwo = new JTextField();
public Enter() {
this.setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Welcome");
setSize(200, 130);
setVisible(true);
setResizable(false);
setLocationRelativeTo(null);
add(txtOne);
add(txtTwo);
enter.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
Main main = new Main();
this.setVisible(false);
one = txtOne.getText();
two = txtTwo.getText();
}
}
Main is the main class that holds the JFrame of ActualGame() and also the main class of Enter().
import javax.swing.*;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
public class Main extends JFrame {
public Main() {
add(new ActualGame());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Main");
setSize(400, 557);
setVisible(true);
setResizable(false);
setLocationRelativeTo(null);
}
public static void main(String[] args) {
Enter enter=new Enter();
}
}
the ActualGame:
import java.awt.*;
import javax.swing.*;
public class ActualGame extends JPanel{
private JLabel lblOne = new JLabel(one);//how i wish it would be that easy
private JLabel lblTwo = new JLabel(two);
public ActualGame() {
setLayout(new FlowLayout());
add(lblOne);
add(lblTwo);
}
}
What should I do to be able to use the String variable one and two from Enter.java to ActualGame.java?
I'm new and noob in programming especially java swing. Open to criticisms and suggestions. Thank you.
Suggestions:
Passing information from one object to another is no different with Swing as with other Java programs. You can call methods or constructors and pass information in via parameters.
A key difference though is when to pass information. With event driven programs, this is often triggered by an event, a listener, and so use of the observer design pattern is comment.
For your purposes, the first window could be a modal dialog such as a JOptionPane or a modal JDialog which will make it easier to figure out when to pass information. When using a modal dialog, all code flow in the calling program is paused while the dialog is visible, and then resumes once the dialog is no longer visible. It's easy then to have the calling program query the dialog once this occurs, because you'll know precisely where in your code this will occur.
You'll want to avoid excessive showing of different windows in your application as it can quickly get annoying to the user. A few dialogs here and there are OK, especially if you need the information to be given in a modal fashion, but in general it's better to swap GUI "views" when needed, and a CardLayout is good for this.
But having said this, separate views are often created by separate classes, so the problem of passing information back and forth remains a problem with similar solutions as described above.
Specifically, give your Enter class a getText method that will allow other objects to query it for the state of its JTextField:
public String getTxtOneText() {
return txtOne.getText();
}
Also, change your ActualGame class so that it can accept String information when needed:
class ActualGame extends JPanel {
private JLabel lblOne = new JLabel();
public ActualGame(String text) {
lblOne.setText(text);
setLayout(new FlowLayout());
add(lblOne);
}
public void setLblOneText(String text) {
lblOne.setText(text);
}
}
e.g.,
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Foo {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
ActualGame actualGame = new ActualGame("");
Main main = new Main(actualGame);
main.pack();
Enter enter = new Enter(main);
enter.setVisible(true);
actualGame.setLblOneText(enter.getTxtOneText());
main.pack();
main.setLocationRelativeTo(null);
main.setVisible(true);
}
});
}
}
class Enter extends JDialog implements ActionListener {
private String one = "";
private JTextField txtOne = new JTextField(10);
private JButton enter = new JButton("Enter");
public Enter(JFrame frame) {
super(frame, "Welcome", true);
this.setLayout(new FlowLayout());
enter.addActionListener(this);
txtOne.addActionListener(this);
add(txtOne);
add(enter);
pack();
setLocationRelativeTo(null);
// this has to be done last
// setVisible(true);
}
public String getTxtOneText() {
return txtOne.getText();
}
public void actionPerformed(ActionEvent e) {
setVisible(false);
}
}
class Main extends JFrame {
ActualGame actualGame;
public Main(ActualGame actualGame) {
super("Main");
this.actualGame = actualGame;
add(actualGame);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class ActualGame extends JPanel {
private JLabel lblOne = new JLabel();
public ActualGame(String text) {
lblOne.setText(text);
setLayout(new FlowLayout());
add(lblOne);
}
public void setLblOneText(String text) {
lblOne.setText(text);
}
}
Try to make ActualGam as a underclass of Enter
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
public class Enter extends JFrame implements ActionListener {
private String one = "";
private JTextField txtOne = new JTextField();
public Enter() {
this.setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Welcome");
setSize(200, 130);
setVisible(true);
setResizable(false);
setLocationRelativeTo(null);
add(txtOne);
enter.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
Main main = new Main();
this.setVisible(false);
one = txtOne.getText();
}
class ActualGame extends JPanel{
private JLabel lblOne = new JLabel(one);
public ActualGame() {
setLayout(new FlowLayout());
Enter.this.add(lblOne);
}
}
}

Categories