Execute a JFrame program - java

how to execute this program?
// Resolve class BorderLayout
import java.awt.*;
// Resolve class JFrame and JButton
import javax.swing.*;
// Definition of class FrameWithBorderLayout
public class FrameWithBorderLayout extends JFrame {// Attribute
private JButton buttonEast; // The east button
private JButton buttonSouth; // The south button
private JButton buttonWest; // The west button
private JButton buttonNorth; // The north button
private JButton buttonCenter; // The center button
// Constructor
public FrameWithBorderLayout() {
// Call super class constructor with a title
super("Frame With Multiple Buttons");
// Create JButton objects
buttonEast = new JButton("East");
buttonSouth = new JButton("South");
buttonWest = new JButton("West");
buttonNorth = new JButton("North");
buttonCenter = new JButton("Center");
// Add the JButton objects
add(buttonEast, BorderLayout.EAST);
add(buttonSouth, BorderLayout.SOUTH);
add(buttonWest, BorderLayout.WEST);
add(buttonNorth, BorderLayout.NORTH);
add(buttonCenter, BorderLayout.CENTER);
// Set when the close button is clicked, the application exits
setDefaultCloseOperation(EXIT_ON_CLOSE);
// Reorganize the embedded components
pack();
}
}
---------------------------------current source-------------------------------------
// Resolve class BorderLayout
import java.awt.*;
// Resolve class JFrame and JButton
import javax.swing.*;
// Definition of class FrameWithBorderLayout
public class test extends JFrame {
// Attribute
private JButton buttonEast; // The east button
private JButton buttonSouth; // The south button
private JButton buttonWest; // The west button
private JButton buttonNorth; // The north button
private JButton buttonCenter; // The center button
// Constructor
public test() {
// Call super class constructor with a title
super("Frame With Multiple Buttons");
// Create JButton objects
buttonEast = new JButton("East");
buttonSouth = new JButton("South");
buttonWest = new JButton("West");
buttonNorth = new JButton("North");
buttonCenter = new JButton("Center");
// Add the JButton objects
add(buttonEast, BorderLayout.EAST);
add(buttonSouth, BorderLayout.SOUTH);
add(buttonWest, BorderLayout.WEST);
add(buttonNorth, BorderLayout.NORTH);
add(buttonCenter, BorderLayout.CENTER);
// Set when the close button is clicked, the application exits
setDefaultCloseOperation(EXIT_ON_CLOSE);
// Reorganize the embedded components
pack();
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
FrameWithBorderLayout frame = new FrameWithBorderLayout();
frame.setVisible(true);
}
});
}
}

Every Java program starts from a main method:
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
FrameWithBorderLayout frame = new FrameWithBorderLayout();
frame.setVisible(true);
}
});
}
Add this to your frame class.

FrameWithBorderLayout frameTest = new FrameWithBorderLayout();
frameTest.setVisible(true);

Related

Unable to put buttons on frame?

I'm using currently Intellij to create a GUI with 3 buttons on the top of the main frame. I'm fairly new to GUI's and still learning as I go. When I try to run the code I get an error telling me that I cannot have the main method as static but if I remove the static I get an error saying I need it to be static. Is there a way to easily avoid this?
package CA3;
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent;
public class Main extends JFrame{
public static void main(String[] args) {
// Attach window listener
addWindowListener(new WindowCloser()); // Just in-case it's needed
// Adding first customer panel
JPanel pCustomerL = new JPanel();
pCustomerL.setBounds(0,0,750,750);
pCustomerL.setBackground(Color.BLUE);
// Adding second customer panel
JPanel pCustomerR = new JPanel();
pCustomerR.setBounds(750,0,750,750);
pCustomerR.setBackground(Color.BLACK);
// Adding first invoice panel
JPanel pInvoiceL = new JPanel();
pInvoiceL.setBounds(0,0,750,750);
pInvoiceL.setBackground(Color.BLUE);
// Adding second product panel
JPanel pInvoiceR = new JPanel();
pInvoiceR.setBounds(750,0,750,750);
pInvoiceR.setBackground(Color.BLACK);
// Button Listener
ButtonListener listener = new ButtonListener();
// Adding "Customer" Button
JButton b = new JButton("Customer");
b.addActionListener(listener);
add(b);
b.setBounds(1300,10,150,35);
// Adding "Product" Button
b = new JButton("Product");
b.addActionListener(listener);
add(b);
b.setBounds(1150,10,150,35);
// Adding "Invoice" Button
b = new JButton("Invoice");
b.addActionListener(listener);
add(b);
b.setBounds(1000,10,150,35);
// Adding first product panel
JPanel pProductL = new JPanel();
pProductL.setBounds(0,0,750,750);
//pProductL.setBackground(Color.BLUE);
// Adding second product panel
JPanel pProductR = new JPanel();
pProductR.setBounds(750,0,750,750);
//pProductR.setBackground(Color.BLACK);
// Frame
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
frame.setSize(1500,750);
frame.setVisible(true);
// Add panels
frame.add(pCustomerL);
frame.add(pCustomerR);
frame.add(pProductL);
frame.add(pProductR);
frame.add(pInvoiceL);
frame.add(pInvoiceR);
// Customer Panel Settings
pCustomerL.setVisible(true);
pCustomerR.setVisible(true);
// Product Settings
pProductL.setVisible(false);
pProductR.setVisible(false);
// Invoice settings
pInvoiceL.setVisible(false);
pInvoiceR.setVisible(false);
}
// Listener for buttons
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent evt) {
String buttonLabel = evt.getActionCommand();
}
}
// Listener for window
class WindowCloser extends WindowAdapter {
public void windowClosing(WindowEvent evt) { System.exit(0);}
}
}
You have to move your code inside the main to another method not-static or you can put it on a Constructor. The errors are because you are using normal methods like addWindowsListener which inherits from JFrame. And you also have to remove:
JFrame frame = new JFrame();
That's not necessary because your class already inherits from JFrame so your code must be like this:
public class Main extends JFrame
{
public Main(){
// Attach window listener
addWindowListener(new WindowCloser()); // Just in-case it's needed
// Adding first customer panel
JPanel pCustomerL = new JPanel();
pCustomerL.setBounds(0,0,750,750);
pCustomerL.setBackground(Color.BLUE);
// Adding second customer panel
JPanel pCustomerR = new JPanel();
pCustomerR.setBounds(750,0,750,750);
pCustomerR.setBackground(Color.BLACK);
// Adding first invoice panel
JPanel pInvoiceL = new JPanel();
pInvoiceL.setBounds(0,0,750,750);
pInvoiceL.setBackground(Color.BLUE);
// Adding second product panel
JPanel pInvoiceR = new JPanel();
pInvoiceR.setBounds(750,0,750,750);
pInvoiceR.setBackground(Color.BLACK);
// Button Listener
ButtonListener listener = new ButtonListener();
// Adding "Customer" Button
JButton b = new JButton("Customer");
b.addActionListener(listener);
add(b);
b.setBounds(1300,10,150,35);
// Adding "Product" Button
b = new JButton("Product");
b.addActionListener(listener);
add(b);
b.setBounds(1150,10,150,35);
// Adding "Invoice" Button
b = new JButton("Invoice");
b.addActionListener(listener);
add(b);
b.setBounds(1000,10,150,35);
// Adding first product panel
JPanel pProductL = new JPanel();
pProductL.setBounds(0,0,750,750);
//pProductL.setBackground(Color.BLUE);
// Adding second product panel
JPanel pProductR = new JPanel();
pProductR.setBounds(750,0,750,750);
//pProductR.setBackground(Color.BLACK);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);
this.setSize(1500,750);
this.add(pCustomerL);
this.add(pCustomerR);
this.add(pProductL);
this.add(pProductR);
this.add(pInvoiceL);
this.add(pInvoiceR);
// Customer Panel Settings
pCustomerL.setVisible(true);
pCustomerR.setVisible(true);
// Product Settings
pProductL.setVisible(false);
pProductR.setVisible(false);
// Invoice settings
pInvoiceL.setVisible(false);
pInvoiceR.setVisible(false);
this.setVisible(true);
}
// Listener for buttons
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent evt) {
String buttonLabel = evt.getActionCommand();
}
}
// Listener for window
class WindowCloser extends WindowAdapter {
public void windowClosing(WindowEvent evt) { System.exit(0);}
}
public static void main(String[] args) {
new Main();
}
}

How to make button in one class affect text area in another?

Please help me to understand how this works. I'm having difficulties to understand how, for example, JButton in one class can alter text in JTextArea that is in another class of a same package. I've made a simple app just to ask a question here, I need this for a bigger school project where I need to implement this to work with multiple classes.
When I put everything in the same class it works but I need it in separate classes.
Here is the simple code.
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class Button extends JPanel {
private JButton button;
private Panel panel;
public Button() {
button = new JButton("BUTTON");
panel = new Panel();
add(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JButton clicked = (JButton) e.getSource();
String input = clicked.getText();
panel.setTextArea(input);
//System.out.println(input);
}
});
}
}
class Panel extends JPanel {
private JTextArea textArea;
public Panel() {
setLayout(new BorderLayout());
textArea = new JTextArea();
add(textArea, BorderLayout.CENTER);
}
public JTextArea getTextArea() {
return textArea;
}
void setTextArea(String text) {
this.textArea.setText(text);
}
}
public class Java extends JFrame {
private Button dugme;
private JFrame frame;
private Panel panel;
public Java() {
frame = new JFrame();
dugme = new Button();
panel = new Panel();
//super("test");
frame.setLayout(new BorderLayout());
frame.setTitle("test");
frame.setSize(300, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.add(dugme, BorderLayout.NORTH);
frame.add(panel, BorderLayout.CENTER);
}
public static void main(String[] args) {
Java app = new Java();
}
}
I want action listener to alter the text in the panel, sys-out works so the listener listens the button but I can't make it to alter the text in text area.
As already mentioned by #XtremeBaumer you have two different instances of Panel class. You need to remove the secode one.
public class Button extends JPanel {
private JButton button;
private Panel panel;
public Button(Panel panel) { // we need already created instance of panel here.
this.panel = panel;
button = new JButton("BUTTON");
// panel = new Panel(); <-- this line must be deleted.
// ...
}
}
public class Java extends JFrame {
private Button dugme;
private JFrame frame;
private Panel panel;
public Java(){
frame = new JFrame();
panel = new Panel();
dugme = new Button(panel);
// ...
}
}
Please also replace the line
add(textArea, BorderLayout.CENTER);
by
add(new JScrollPane(textArea), BorderLayout.CENTER);
This allows you to get the scrool bars when text goes larger than the text ara size.
Here is your reworked example
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class Button extends JPanel {
private JButton button;
private Panel panel;
public Button(Panel panel) {
this.panel = panel;
button = new JButton("BUTTON");
add(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JButton clicked = (JButton) e.getSource();
String input = clicked.getText();
panel.setTextArea(input);
//System.out.println(input);
}
});
}
}
class Panel extends JPanel {
private JTextArea textArea;
public Panel() {
setLayout(new BorderLayout());
textArea = new JTextArea();
add(new JScrollPane(textArea), BorderLayout.CENTER);
}
public JTextArea getTextArea() {
return textArea;
}
void setTextArea(String text) {
this.textArea.setText(text);
}
}
public class Java extends JFrame {
private Button dugme;
private JFrame frame;
private Panel panel;
public Java() {
frame = new JFrame();
panel = new Panel();
dugme = new Button(panel);
//super("test");
frame.setLayout(new BorderLayout());
frame.setTitle("test");
frame.setSize(300, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.add(dugme, BorderLayout.NORTH);
frame.add(panel, BorderLayout.CENTER);
}
public static void main(String[] args) {
Java app = new Java();
}
}

Why launching does it random?

When i run this program sometimes shows me all buttons, but sometimes only 2 or 3 or 4 or 5 or even just 1.. why is that??
I really do not get it. There should always be 6 buttons, but it doesnt show them. Is there any logical reason?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class testnet
{
public static void main (String[] args)
{
JFrame frame = new JFrame("Knjigarna");
frame.setVisible(true);
frame.setSize(800,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.add(panel);
JButton button1 = new JButton("Prikazi vse");
panel.add(button1);
button1.addActionListener (new Action1());
JButton button2 = new JButton("Prikazi knjigo");
panel.add(button2);
button2.addActionListener (new Action2());
JButton button3 = new JButton("Dodaj knjigo");
panel.add(button3);
button3.addActionListener (new Action3());
JButton button4 = new JButton("Brisi knjigo");
panel.add(button4);
button4.addActionListener (new Action4());
JButton button5 = new JButton("Uredi knjigo");
panel.add(button5);
button5.addActionListener (new Action5());
JButton button6 = new JButton("Izhod");
panel.add(button6);
button6.addActionListener (new Action6());
}
static class Action1 implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
JFrame frame2 = new JFrame("Pikaz vseh knjig");
frame2.setVisible(true);
frame2.setSize(500,800);
JLabel label = new JLabel("Seznam vseh knjig:");
JPanel panel = new JPanel();
JTextField text1=new JTextField("Naslov: ");
JTextField text2=new JTextField("Avtor: ");
frame2.add(panel);
panel.add(label);
panel.add(text1);
panel.add(text2);
}
}
static class Action2 implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
JFrame frame3 = new JFrame("Prikaz knjige");
frame3.setVisible(true);
frame3.setSize(600,300);
JLabel label = new JLabel("Vpisi naslov knjige:");
JPanel panel = new JPanel();
frame3.add(panel);
panel.add(label);
}
}
static class Action3 implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
JFrame frame4 = new JFrame("Dodajanje knjige");
frame4.setVisible(true);
frame4.setSize(600,300);
JLabel label = new JLabel("Vpisi podtke o knjigi");
JPanel panel = new JPanel();
frame4.add(panel);
panel.add(label);
}
}
static class Action4 implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
JFrame frame5 = new JFrame("Brisanje knjige");
frame5.setVisible(true);
frame5.setSize(600,300);
JLabel label = new JLabel("Vpisi naslov knjige, ki jo zelis brisati");
JPanel panel = new JPanel();
frame5.add(panel);
panel.add(label);
}
}
static class Action5 implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
JFrame frame6 = new JFrame("Urejanje knjige");
frame6.setVisible(true);
frame6.setSize(600,300);
JLabel label = new JLabel("Vpisi naslov knjige, ki jo zelis urejati");
JPanel panel = new JPanel();
frame6.add(panel);
panel.add(label);
}
}
static class Action6 implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
System.exit(0);
}
}
}
try something with layout. JFrame and or remove managed by inside with a content pane. content pane default layout is BorderLayout. so you need to try border layout stuff.
or you can try this code in you main method
frame.setLayout(new FlowLayout());
this will add component by one by one.
for more about layout you can get in here
This is just a fix and not really an explanation of why the problem is occurring.
Call frame.revalidate() after adding all the buttons.
From the Java Docs,
public Component add(Component comp)
This method
changes layout-related information, and therefore, invalidates the
component hierarchy. If the container has already been displayed, the
hierarchy must be validated thereafter in order to display the added
component.

change JLabel of a panel depending on Jbutton of another panel in the same Jframe

I have constructed a class for the JPanel with several JButtons.Inside this class I want to construct another JPanel with JLabels that will change depending on the actionPerformed on the JButtons of the first JPanel.Finally, I want to add these 2 panels on the same Jframe. Can all these be done within the class of the first Panel?Otherwise, which is a better approach for this problem?
Yes, you can. One way you could accomplish this is with anonymous inner classes (saves keystrokes):
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;
public class Foo {
JLabel one;
JLabel two;
public static void main(String[] args) {
(new Foo()).go();
}
public void go() {
JFrame frame = new JFrame("Test");
// Panel with buttons
JPanel buttonPanel = new JPanel();
JButton changeOne = new JButton("Change One");
changeOne.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
one.setText("New text for one");
}
}
buttonPanel.add(changeOne);
JButton changeTwo = new JButton("Change Two");
changeTwo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
two.setText("New text for two");
}
}
buttonPanel.add(changeTwo);
frame.add(buttonPanel, BorderLayout.NORTH);
// Panel with labels
JPanel labelPanel = new JLabel();
one = new JLabel("One");
labelPanel.add(one);
two = new JLabel("Two");
labelPanel.add(two);
// Set up the frame
frame.add(labelPanel, BorderLayout.SOUTH);
frame.setBounds(50, 50, 500, 500);
frame.setDefaultCloseAction(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

Why is my text field not appearing?

I have to create a text field, a text area, and two buttons, but I am stuck on the text field because I cant get it to appear when I run my program. My JFrame keeps appearing empty.
public class SentanceBuilder extends JFrame {
private JTextField textField = new JTextField(50);
private JTextArea textArea = new JTextArea(200,200);
private JButton Submit = new JButton();
private JButton Cancel = new JButton();
public SentanceBuilder(){
textField.setVisible(true);
textField.setLocation(50, 50);
this.textField();
this.setSize(400, 300);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public void textField(){
String textContent = textField.getText();
}
}
You never add the textField variable to a container such as a JPanel that is held by the top-level window, such as a JFrame. In fact, in your code, you add nothing to your JFrame!
If this were my GUI, I'd
Create my JTextField
Create a main JPanel to hold my components.
Add it and other components to the main JPanel via the JPanel's add(...) method.
Add the main JPanel to the JFrame's contentPane via the JFrame's add(...) method.
Call pack() and then setVisible(true) on the JFrame, but only after adding all components to it, not before.
Read the Swing tutorials since this beats guessing every time. You can get links to the tutorials at the Swing Tag Info link.
e.g.,
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class MyFoo extends JFrame {
private static final long serialVersionUID = 1L;
private JTextField textField = new JTextField(10);
private JButton button = new JButton("Foo Button");
public MyFoo() {
super("My JFrame");
// so Java will end when GUI is closed
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// code to be called when button is pushed
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// TODO code that runs when button is pushed.
}
});
// panel to hold everything
JPanel mainPanel = new JPanel();
// add all to the panel
mainPanel.add(new JLabel("Text Field:"));
mainPanel.add(textField);
mainPanel.add(button);
// add the panel to the main GUI
add(mainPanel);
}
// start up code
private static void createAndShowGui() {
JFrame mainFrame = new MyFoo();
mainFrame.pack();
mainFrame.setLocationRelativeTo(null);
mainFrame.setVisible(true);
}
public static void main(String[] args) {
// call start up code in a Swing thread-safe way
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

Categories