Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: create class and add jtext and jbutton in the panel

  1. #1

    Default create class and add jtext and jbutton in the panel

    i use this code but don't show jtext and jbutton
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class Datepannel extends JPanel {
     
        JButton choosdate = new JButton();
        JTextField datetext = new JTextField();
        JPanel panel = new JPanel();
     
        public void paintComponent() {
            setLayout(new FlowLayout());
            add(panel);
            panel.setPreferredSize(new Dimension(200, 50));
            panel.setLayout(new FlowLayout());
            panel.setVisible(true);
            choosdate.setText("okokok");
            choosdate.setVisible(true);
            panel.add(choosdate);
            panel.add(datetext);
     
        }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,311
    Thanks
    54
    Thanked 2,635 Times in 2,585 Posts

    Default Re: create class and add jtext and jbutton in the panel

    The simple code does not need a paintComponent method. Remove that method.
    It does need a JFrame and a main method.
    From my last post:
    create a small, simple program for learning how:
    The test program would have a JFrame and main method.
    Then create a JPanel that contains some components like a textfield and button.
    Add that panel to the frame.
    Then add a listener to the button that would add some text to the textfield.
    When the program executes, it would show the textfield and the button
    When the button is pressed, some text would be added to the textfield.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3

    Default Re: create class and add jtext and jbutton in the panel

    dont wok again
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class Datepannel extends JPanel {
     
        JButton choosdate = new JButton();
        JTextField datetext = new JTextField();
        JPanel panel = new JPanel();
     
        public void JFrame() {
     
            setLayout(new FlowLayout());
     
            panel.setPreferredSize(new Dimension(50, 50));
            panel.setLayout(new FlowLayout());
            panel.setVisible(true);
            choosdate.setText("okokok");
            choosdate.setVisible(true);
            panel.add(choosdate);
            panel.add(datetext);
            add(panel);
     
        }
     
        public static void main(String[] args) {
            JFrame a = new JFrame();
     
            a.setPreferredSize(new Dimension(400, 300));
            a.pack();
            a.setLocationRelativeTo(null);
            a.setVisible(true);
     
     
    } //End of main
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,311
    Thanks
    54
    Thanked 2,635 Times in 2,585 Posts

    Default Re: create class and add jtext and jbutton in the panel

    dont wok again
    What does it do?
    The main class creates a JFrame but doesn't add anything to it.
    It would be better if the code to create the JFrame is in the constructor for the class.
    Also create the the JPanel there. Then Create the components and add them to the panel
    and then add the panel to the JFrame.

    There should NOT be a method named JFrame. That is a poor name because it is the name of a class.

    See the code in the NewJFrame class
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5

    Default Re: create class and add jtext and jbutton in the panel

    I do not understand why you without any examples
    Like school teachers, you try to teach me by explaining
    Please provide a sample code instead of a description
    Each person has their own way of learning. Some people understand all the details by reading a book
    And some people do not understand until they see something or example
    Unfortunately, in my questions you always you have explain why I have to use a code.( and don't say what is that code)
    While I know the reason for using the code, I do not know how to use that code or where to use that Or what code should I write at all?
    And don't understanding this without seeing a complete example
    And it is not possible without complexity And without explanation and just seeing the code makes it possible to understand it
    My problem is not code analysis, not knowing the code itself
    If you have never seen a picture of the ocean, and even if I have told you about the ocean for years, you will never have a good understanding of the ocean until you see it up close, even if the rivers and pools
    Last edited by cnmeysam; May 2nd, 2021 at 05:36 AM.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,311
    Thanks
    54
    Thanked 2,635 Times in 2,585 Posts

    Default Re: create class and add jtext and jbutton in the panel

    Look at samples on this forum.
    Ask Google for some samples.
    Look through the java tutorials: https://docs.oracle.com/javase/tutor...ybigindex.html

    Look at this part of the tutorial: https://docs.oracle.com/javase/tutor...ing/index.html

    The code you have posted has all the examples in it mixed in with a lot of other things.
    Start with the NewJFrame class and add to it.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7

    Default Re: create class and add jtext and jbutton in the panel

    Why do you think I ask questions from forums like yours instead of searching Oracle?
    Fools like company oracle Who claim to be open source and public
    There are only liars who play with politics
    Of course, it is not sanctioned for the government but for the people and we can't enter to oracle web site
    We are only allowed to use their website if we use VPN Or when we leave the country
    I wish you were writing just a piece of code instead of all this citation and typing the description
    Sorry to waste your time
    Thanks for what you taught me
    Good luck

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,311
    Thanks
    54
    Thanked 2,635 Times in 2,585 Posts

    Default Re: create class and add jtext and jbutton in the panel

    I think there are examples of code that uses JFrames on this forum. Use the Search box at the top right of the page.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How i can create a Panel in the same
    By websit in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 6th, 2013, 09:53 AM
  2. Replies: 9
    Last Post: December 1st, 2012, 02:16 PM
  3. Create a JText field upon button click
    By jo15765 in forum AWT / Java Swing
    Replies: 3
    Last Post: May 28th, 2012, 07:06 PM
  4. problem of creating matrix with JButton or Panel
    By ms_ceng in forum AWT / Java Swing
    Replies: 3
    Last Post: December 16th, 2011, 04:53 PM
  5. Adding a Panel from a different class
    By DudeJericho in forum What's Wrong With My Code?
    Replies: 9
    Last Post: April 25th, 2011, 07:28 AM