简体   繁体   中英

How do I create a working GUI without JPanels and layouts

I took a pre-ap computer science course in high school, and I have decided I want to start programming for fun again but I have a problem. I can't remember how to create a GUI. I tried to search online how to create one but every single source keeps showing jpanels and layouts which we never learned. I know I'm probably doing the "wrong" way But it's the only way I know how to program GUI's. I tried to figure out JPanels but it left me even more confused than I already was. Before I left for the summer I took one picture of my code to match up the colors if I ever started coding at home. And I have been trying to get My GUi running off of this code segment to no avial.

import javax.swing.*;
public class FirstGUI extends JFrame implements ActionListener
{
    private JButton myButton;
    private JButton myButton2;
    private JButton myButton3;
    private JLabel myLabel;
    private JTextfield myField;
    private int x =0;

    public FirstGUI()
    {
        Contianer contentPane = getContentPane();
        contentPane.setLayout(null);
        contentPane.setBackground(Color.white);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setTitle("My first GUI Experiment");
        setSize(500,500);
        setResisable(false);
        setLocation(150,150);

        myButton = new JButton("3");
        myButton.setBounds(0,0,50,50);
        myButton.setBorder(BorderFactory.createLineBorder(Color.blue));
        contentPane.add(myButton);

        myLabel = new JLabel();
        myButton.setBounds(100,100,300,50);
        myButton.setBorder(BorderFactory.createLineBorder(Color.blue));
        contentPane.add(myButton);

        myButton = new JButton("3");
        myButton.setBounds(0,0,50,50);

So can any one help me by showing me what else I need to complete this code. I know this was a working project with no errors in it and I also know that I need a method for the action listener, But after that I know nothing else.

If this question has already been asked I apologize but I'm new to programming and I only know a tiny bit of the grammar needed to probably easily find the answer to this problem.

Why don't you consider using an IDE like Netbeans to help you design the UI. Create a JPanel form and then use the drag and drop functionality to generate the relevant code.

there is a easy way to create a GUI in Java rather than using Java Swing that use Jframes, It's called JavaFX, it comes in Java SE 8 and It's really easy to use with the Scene Builder , first of all you can use an IDE like NetBeans or IntelliJ IDEA and create an JavaFX project, the IDE is going to support you in your coding journey and after you have already familiarized with JavaFX you can code without an IDE.

In JavaFX you will work with FXML that is an XML-based user interface markup language created by Oracle Corporation for defining the user interface of a JavaFX application. You are going to use View - Controller programing, all the FXML files are your views that are going to work with Java files that are going to be your controllers. Here a tutorial that can be useful in your journey

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM