0

Everything except the Button is showing up in the window. IS there something i am missing? This is the first time using Buttons and am not sure what's going wrong. It might be a formatting issue. Can somebody tell me if there is an issue with setLocation() and setSize()?

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.TextField;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class HashString extends JPanel {


    public static void hashString() {
    }

    public void window() {
    JLabel label1 = new JLabel(
            "Enter Your Strings separated by a comma, below. ");
    label1.setHorizontalAlignment(JLabel.CENTER);
    label1.setFont(new Font("Times New Roman", Font.BOLD, 12));
    label1.setVerticalAlignment(JLabel.TOP);

    JTextField field = new JTextField(50);
    field.setVisible(true);
    field.setText("Enter Strings Here");
    field.setSize(300, 251);
    field.setHorizontalAlignment(JTextField.CENTER);
    field.setLocation(135, 60);

    Button btn = new Button("Enter These Values");
    btn.setLocation(240 ,420);
    btn.setSize(100, 100);
    btn.setVisible(true);
    btn.setFont(new Font("Times new roman",Font.BOLD,12));

    JFrame frame = new JFrame("Test1");
    frame.add(new HashString());
    frame.add(btn);
    frame.setVisible(true);
    frame.add(field);
    frame.setLocationRelativeTo(null);
    frame.add(label1);
    frame.setSize(600, 450);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

James_D
  • 201,275
  • 16
  • 291
  • 322
Armando Xhimanki
  • 115
  • 1
  • 11

1 Answers1

2

I think you should change how you've structured the program. Instead of putting each position with a manual position inside the JFrame, you should simply use a layout manager, like BorderLayout, which can easily be implemented here.

Also, you should always address everything from the EventQueue and not any other threads. Furthermore, it is inadvisable to mix and match AWT components like Button and Swing components like JButton. While it is much better than it was before – like in Java 1.6 – but it still can turn up some issues.

import java.awt.BorderLayout;
import java.awt.EventQueue;
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;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class HashString {   // It seems that this isn't a JPanel. Rather, it is an application.

    JFrame frame;

    public void initialise() {

        frame = new JFrame("Test 1");   // You can create the frame of the application here and set title

        frame.setLocation(200, 200);
        frame.setSize(300, 300);

        JPanel contentPanel = new JPanel();     // To allow a border to be set, I've declared a content panel inside the
                                                // frame.
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));    // This sets a border to make everything look nice
        contentPanel.setLayout(new BorderLayout(5, 5)); // This creates the BorderLayout, which manages the layout of
                                                        // the components easily
        frame.setContentPane(contentPanel);

        JLabel instructionsLabel = new JLabel("Enter Your Strings separated by a comma, below. ");
        instructionsLabel.setFont(new Font("Times New Roman", Font.BOLD, 12));
        contentPanel.add(instructionsLabel, BorderLayout.NORTH);    // BorderLayout.NORTH tells the layout manager where
                                                                    // to put the component.

        JTextField txtField = new JTextField();
        txtField.setText("Enter Strings Here");
        contentPanel.add(txtField, BorderLayout.CENTER);

        JButton btn = new JButton("Enter These Values");
        btn.setFont(new Font("Times new roman", Font.BOLD, 12));
        btn.addActionListener(new ActionListener() {
            @Override public void actionPerformed(ActionEvent e) {
                // Call whatever method that you want to call when this is relevant. Set textField and other variables
                // here. You can do things like 'txtField.setText( methodOperationOnString ( txtField.getText() ) )' or
                // something of the like.
            }
        });
        contentPanel.add(btn, BorderLayout.SOUTH);

        frame.setVisible(true);

    }

    public static void main(String[] args) {
        // This tells it to create the entire thing on the GUI thread
        EventQueue.invokeLater(new Runnable() {
            @Override public void run() {
                HashString b = new HashString();
                b.initialise();
            }
        });
    }
}
ifly6
  • 5,003
  • 2
  • 24
  • 47
  • 1
    WOW! That is really helpful! I am used to doing everything manually – Armando Xhimanki Jul 27 '16 at 23:24
  • If your main method is in another class – change `public void initialise()` to `public HashString()`. Then it'll construct automatically when you invoke 'new HashString()`. You can also just remove the `main` if so. – ifly6 Jul 27 '16 at 23:31
  • Don't mix JavaFx and Swing. This is a Swing application. Don't try to initialise it from here. If you did so, (and you're in Eclipse), you need to remove the run configuration. Click the arrow next to the `Play button > Run Configurations > HashString` and click delete on that run config. – ifly6 Jul 27 '16 at 23:38
  • thank you x1000000 – Armando Xhimanki Jul 27 '16 at 23:42
  • Oh yea, this is helpful. It's the Oracle tutorial on all the stock LayoutManagers in Java: https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html – ifly6 Jul 27 '16 at 23:48